hard challenge

来源:互联网 发布:cf自慰刷枪源码 编辑:程序博客网 时间:2024/06/07 20:34

Description

There are n points on the plane, and the ith 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 ith 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.

Sample Input

2
2
1 1 1
1 -1 1
3
1 1 1
1 -1 10
-1 0 100

Sample Output

1
1100

Hint

题意

n个点 每个点都对应一个val
任两点之间的价值为这两点的值的积
要求找一条从原点出发的直线 求这直线每边任意两个点的价值的积的和最大

题解:

对斜率排序 x为负值的放左边 保证从右边斜率最小开始更新最大值

AC代码

#include<stdio.h>#include<algorithm>using namespace std;typedef long long LL;const int MAXN = 1e5;struct node{    double k;    int x,val;}sp[MAXN];bool cmp(node a,node b){    return a.k<b.k;}int main(){    int t;    scanf("%d",&t);    while (t--){        int n;        scanf("%d",&n);        int x,y,c;        int s1,s2;        s1 = s2 = 0;        for (int i = 0;i < n; ++i){            scanf("%d%d%d",&x,&y,&c);            if (x==0) sp[i].k = (y<0?-1e10:1e10);            else sp[i].k = (double)y/(double)x;            sp[i].x = x;            sp[i].val = c;            if (x>=0) s1+=c;            else s2+=c;        }        LL ans = s1*s2;        sort(sp,sp+n,cmp);        for (int i = 0;i < n; ++i){            if (sp[i].x>=0){                s1-=sp[i].val;                s2+=sp[i].val;            }else {                s1+=sp[i].val;                s2-=sp[i].val;            }            ans = max(ans,(LL)s1*s2);        }        printf("%lld\n",ans);    }    return 0;}
原创粉丝点击