HDU 6127 Hard challenge 计算几何 极角排序

来源:互联网 发布:淘宝交易指数 构成 编辑:程序博客网 时间:2024/06/07 16:44


Hard challenge

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


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(1T5), denoting the number of test cases.
For each test case:
The first line contains a positive integer n(1n5×104).
The next n lines, the ith line contains three integers xi,yi,vali(|xi|,|yi|109,1vali104).
 

Output
For each test case:
A single line contains a nonnegative integer, denoting the answer.
 

Sample Input
221 1 11 -1 131 1 11 -1 10-1 0 100
 

Sample Output
11100
 

Source
2017 Multi-University Training Contest - Team 7

本题就是要在给定的点和权值的基础上 在坐标系中画一条直线 
然后求这个直线所穿过的边权最大是多少
边权=两边的点权之积求和

仔细想想其实这道题 相当于把坐标系中的点分为两部分 然后对分别把两边的点求和再乘起来
因为 假设 在两边 分别有点权为a1a2和b1b2的点
那么 其边权为a1*b1+a2*b2 + a2*b1+a1*b2 = (a1+a2)*(b1*b2)

所以我们 按照极角排序然后遍历求解 一开始用atan2无线wa 
后来改用atan后ac 
所以以后一个不行的时候换换另一个
现在还是不太明白为什么一定要按照极角排序扫 或是一定要按照atan排序扫才能AC
#include<bits/stdc++.h>using namespace std;#define rep(i,a,b) for(int i=(a);i<=(b);i++)const double eps = 1e-9;typedef long long ll;int n;struct node{    double x,y;    int v;    double ang;    bool operator<( node &ne)const{        return ang<ne.ang;    }}p[500005];void solve(){    sort(p+1,p+1+n);    ll sumL = 0,sumR=0,ans=0;    rep(i,1,n)        if(p[i].x>eps)sumL+=p[i].v;        else sumR+=p[i].v;    ans = sumL*sumR;    rep(i,1,n){        if(p[i].x>eps)sumL-=p[i].v,sumR+=p[i].v;        else sumL+=p[i].v,sumR-=p[i].v;        ans = max(ans,sumL*sumR);    }    printf("%lld\n",ans);}int main(){    int t;    scanf("%d",&t);    while(t--){        scanf("%d",&n);        rep(i,1,n)        {            scanf("%lf%lf%d",&p[i].x,&p[i].y,&p[i].v);            p[i].ang = atan2(p[i].y,p[i].x);        }        solve();    }    return 0;}


 

原创粉丝点击