【HDU 6127 Hard challenge】& 斜率

来源:互联网 发布:剑灵男灵剑士捏脸数据 编辑:程序博客网 时间:2024/06/18 12:32

Hard challenge

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 796 Accepted Submission(s): 321

Problem 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

Source
2017 Multi-University Training Contest - Team 7

题意 : 两两点之间都有一条线段,线段的价值 = 两段点的价值的乘积,找到一条过原点的直线,与直线相交的线段价值和最大

思路: 找到一条过原点的直线把点分成两个部分,两个部分的点价值和的乘积最大,一开始按 x < 0 || x >= 0分成两个部分,按斜率排好序后,旋转,当遇到 x < 0 的点时把它移到右边,反之移到左边

AC代码:

#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>using namespace std;const int MAX = 5e4 + 10;typedef long long LL;struct node{    int v,x,y;    double o;}st[MAX];bool cmp(node i,node j) { return i.o < j.o; }void solve(int n){    LL ans = 0,l = 0,r = 0;    for(int i = 0; i < n; i++)        if(st[i].x < 0) l += st[i].v;        else r += st[i].v;    ans = l * r;    for(int i = 0; i < n; i++){        if(st[i].x < 0) r += st[i].v, l -= st[i].v;        else r -= st[i].v, l += st[i].v;        ans = max(ans,l * r);    }    printf("%lld\n",ans);}int main(){    int T;    scanf("%d",&T);    while(T--){        int n;        scanf("%d",&n);        for(int i = 0; i < n; i++)            scanf("%d %d %d",&st[i].x,&st[i].y,&st[i].v),st[i].o = 1.0 * st[i].y / st[i].x;        sort(st,st + n,cmp);        solve(n);    }    return 0;}
原创粉丝点击