Hard challenge
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 1216 Accepted Submission(s): 502
Problem Description
There are
n
points on the plane, and the
i
th points has a value
vali
, and its coordinate is
(xi,yi)
. It is guaranteed that no two points have the same coordinate, and no two points makes the line which passes them also passes the origin point. For every two points, there is a segment connecting them, and the segment has a value which equals the product of the values of the two points. Now HazelFan want to draw a line throgh the origin point but not through any given points, and he define the score is the sum of the values of all segments that the line crosses. Please tell him the maximum score.
Input
The first line contains a positive integer
T(1≤T≤5)
, denoting the number of test cases.
For each test case:
The first line contains a positive integer n(1≤n≤5×104) .
The next n lines, the i th line contains three integers xi,yi,vali(|xi|,|yi|≤109,1≤vali≤104) .
For each test case:
The first line contains a positive integer n(1≤n≤5×104) .
The next n lines, the i th line contains three integers xi,yi,vali(|xi|,|yi|≤109,1≤vali≤104) .
Output
For each test case:
A single line contains a nonnegative integer, denoting the answer.
A single line contains a nonnegative integer, denoting the answer.
Sample Input
2 2 1 1 1 1 -1 1 3 1 1 1 1 -1 10 -1 0 100
Sample Output
1 1100
Source
解题思路:把所有点按照极角逆时针排个序就行,然后从第一个点开始扫就行。
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 50000 + 10;
const double eps = 1e-9;
struct Point{
LL x, y;
LL value;
Point(LL _x = 0, LL _y = 0){
x = _x;
y = _y;
value = 0;
}
}P[maxn];
typedef Point Vector;
Vector operator +(Vector A, Vector B) {return Vector(A.x + B.x, A.y + B.y);}
Vector operator -(Vector A, Vector B) {return Vector(A.x - B.x, A.y - B.y);}
Vector operator *(Vector A, double p) {return Vector(A.x * p, A.y * p);}
Vector operator /(Vector A, double p) {return Vector(A.x / p, A.y / p);}
bool dcmp(double x)
{
if(fabs(x) < eps) return 0;
else if(x > eps) return 1;
else return -1;
}
LL Cross(Vector A, Vector B)
{
return A.x * B.y - A.y * B.x;
}
int judge(Point px)//判断象限
{
LL x = px.x;
LL y = px.y;
if(x >= 0 && y >= 0) return 1;
else if(x <= 0 && y >= 0) return 2;
else if(x <= 0 && y <= 0) return 3;
else return 4;
}
bool cmpAngle(Point p1, Point p2)//极角排序
{
if(judge(p1) == judge(p2))
{
return Cross(p1, p2) > 0;
}
return judge(p1) < judge(p2);
}
int n;
int main()
{
int T;
//freopen("C:\\Users\\creator\\Desktop\\in1.txt","r",stdin) ;
//freopen("C:\\Users\\creator\\Desktop\\out.txt","w",stdout) ;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
LL sum = 0;
for(int i = 1; i <= n; i++)
{
scanf("%lld%lld%lld", &P[i].x, &P[i].y, &P[i].value);
sum += P[i].value;
}
sort(P + 1, P + n + 1, cmpAngle);
P[0].value = 0;
LL Max = 0;
LL sum1 = 0;
int l = 1, r = 1;
for(int i = 1; i <= n; i++)
{
l = i;
if(r <= l)
{
r = l;
sum1 = P[r].value;
}
else
{
sum1 -= P[i - 1].value;
}
for(int j = r + 1; j%n != i; j++)
{
int ans = j % n;
if(ans == 0) ans = n;
if(Cross(P[i], P[ans]) >= 0)
{
sum1 += P[ans].value;
r = j;
}
else
{
break;
}
}
Max = max(Max, sum1 * (sum - sum1));
}
printf("%lld\n", Max);
}
return 0;
}