hdu6127Hard challenge(极角排序)

来源:互联网 发布:2016国内旅游数据 编辑:程序博客网 时间:2024/06/05 07:28

Hard challenge

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


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

一些点在平面上,问如何让通过源点的线能经过最多的点点连成的边?
例子:(1,1)=1,(1,-1,)=1两个点在平面上,从原点出发,做垂直他们的连线线段(权值为点点乘积)就有了1。
这一类的题都可看做极角排序题,先对所以的点做极角排序(与x轴的夹角),然后对整个平面的点做与x轴的夹角计算,超过180度的额外计算
+2*PI,最后统计穿过的点点的边权值,计算出最大的的穿过量。

/**   by z_guibin*/#include <cstdio>#include <cstring>#include <algorithm>#include <vector>#include <queue>#include <cmath>#include <iostream>#define N 50010const double PI=acos(-1);using namespace std;struct node{    int vla;    double arg;}p[2*N];long long sum[2*N];int cmp(node a,node b){    return a.arg<b.arg;}int main(){    int t,n;    scanf("%d",&t);    while(t--)    {        long long tot=0;        scanf("%d",&n);        for(int i=1;i<=n;i++)        {            int x,y;            scanf("%d %d %d",&x,&y,&p[i].vla);            tot+=p[i].vla;            p[i].arg=atan2(y,x);        }        sort(p+1,p+n+1,cmp);        long long ans=0;        for(int i=1;i<=n;i++)        {            p[i+n]=p[i];            p[i+n].arg+=2*PI;        }        for(int i=1;i<=2*n;i++)        {            sum[i]=sum[i-1]+p[i].vla;        }        int st=1;        for(int i=1;i<=n;i++)        {            while(p[st].arg-p[i].arg<PI)            {                st++;            }            long long x=sum[st-1]-sum[i-1];            ans=max(ans,x*(tot-x));        }        printf("%lld\n",ans);    }    return 0;}
再贴一个额外的代码

#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;}
来自http://blog.csdn.net/WYK1823376647/article/details/77236911