Hard challenge
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 1093 Accepted Submission(s): 450
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, thei 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
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
题目大意:
平面直角坐标系上有
n
个整点,第
解题思路:
经过推导之后会发现:这条经过原点的直线
l
将所有的点分为两部分(直线上与直线下),最终的答案就是两部分的和的乘积,发现这个性质之后,将所有点按照斜率(极角)排序,然后扫一遍进行判断就行了。需要注意的是,我们可以预处理出从
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 5e4+5;
const LL MOD = 1e9+7;
struct Point{
LL x, y, v;
double ang;
}a[MAXN];
int cmp(Point A, Point B){
return A.ang < B.ang;
}
int check(Point A, Point B){//>=0:up <0:down
return 1.0*(A.x*B.y-A.y*B.x)*A.x >= 0;
}
int main(){
//freopen("C:/Users/yaonie/Desktop/in.txt", "r", stdin);
//freopen("C:/Users/yaonie/Desktop/out.txt", "w", stdout);
int T; scanf("%d",&T);
while(T--){
int n; scanf("%d", &n);
LL sumv = 0;
for(int i=0; i<n; i++){
scanf("%lld%lld%lld",&a[i].x,&a[i].y,&a[i].v);
if(a[i].x != 0) a[i].ang = 1.0*a[i].y/a[i].x;
else a[i].ang = MOD;
sumv += a[i].v;
}
sort(a, a+n, cmp);
LL sum1 = a[0].v, sum2 = 0;//sum1:up sum2:down
int cnt = 0;
for(int i=1; i<n; i++) if(check(a[0], a[i])) sum1 += a[i].v;
sum2 = sumv - sum1;
LL ans = sum1*sum2;
if(a[0].x > 0) sum1 -= a[0].v;
for(int i=1; i<n; i++){
sum2 = sumv - sum1;
ans = max(ans, sum1*sum2);
if(a[i].x > 0) sum1 -= a[i].v;
if(a[i].x < 0) sum1 += a[i].v;
sum2 = sumv - sum1;
ans = max(ans, sum1*sum2);
}
printf("%lld\n",ans);
}
return 0;
}