HDU 6127 Hard challenge(级角排序)

来源:互联网 发布:淘宝u站怎么进入 编辑:程序博客网 时间:2024/06/06 02:43


Hard challenge

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


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
 

Recommend
liuyiding
 

Statistic | Submit | Discuss | Note

平面直角坐标系上有nn个整点,第ii个点有一个点权val_ivali,坐标为(x_i,y_i)(xi,yi),其中不存在任意两点连成的直线经过原点。这些整点两两之间连有一条线段,线段的权值为其两端点的权值之积。你需要作一条过原点而不过任意一个给定整点的直线,使得和这条直线相交的线段的权值和最大。1\leq n\leq5\times10^4,1\leq val_i\leq10^4,|x_i|,|y_i|\leq10^91n5×104,1vali104,xi,yi109

对于一条直线,线段权值和实际上就等于其两边点权和的乘积,所以把所有点按极角排个序,然后扫一圈就好了。

以为一开始肯定要有一个直线,不妨设为x轴或者y轴,如果设为y轴的话,刚好利用了atan只能处理 -90 ~ 90的角的性质,如果以x轴的话,则要把3象限对称到1象限, 4象限对称到2象限, 排序之后, 点不变,只是点的标号变了, 模拟直线的旋转,期间不断更新就好了

y轴为中心线代码:

#include<set>#include<map>#include<cmath>#include<ctime>#include<cstdio>#include<cassert>#include<cstdlib>#include<cstring>#include<algorithm>using namespace std;const double PI=acos(-1);typedef long long ll;#define maxn 200005struct Node{double x,y,ang;int v;Node(){}Node(double x,double y,int v):x(x),y(y),v(v){}void input(){    scanf("%lf%lf%d", &x, &y, &v);    ang = atan(y/x);    }bool operator < (const Node &p)const{    return ang < p.ang;    }}p[maxn];int n, T, v[maxn];double x[maxn],y[maxn];ll sumL,sumR,ans;int main(){    scanf("%d", &T);while (T--)    {        scanf("%d", &n);        for(int i = 1; i <= n; i++)            p[i].input();        sort(p+1,p+n+1);        sumL = sumR = ans = 0;        for(int i = 1; i <= n; i++)        {            if(p[i].x > 0)                sumL += p[i].v;            else                sumR += p[i].v;        }        ans = sumL*sumR;        for(int i = 1; i <= n; i++)        {            if (p[i].x > 0)                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);    }    return 0;}

x轴为中心线代码:

#include<bits/stdc++.h>#define N 50006using namespace std;const double pi=acos(-1.0);struct point{    long long x,y,v;    bool flag;}a[N];bool cmp(point x,point y){    double xx,yy;    xx=atan2((double)x.y,(double)x.x);    yy=atan2((double)y.y,(double)y.x);    return xx<yy;}int main(){    int t,n;    long long sum1=0,sum2=0;    scanf("%d",&t);    while(t--)    {        sum1=0,sum2=0;        scanf("%d",&n);        for(int i=0;i<n;i++){        scanf("%lld%lld%lld",&a[i].x,&a[i].y,&a[i].v);        if(a[i].y<0)        {            sum1+=a[i].v;            a[i].x*=-1;            a[i].y*=-1;            a[i].flag=false;        }        else a[i].flag=true,sum2+=a[i].v;        }        sort(a,a+n,cmp);        long long ans=sum1*sum2;        for(int i=0;i<n;i++)        {//            cout<<a[i].x<<" "<<a[i].y<<endl;            if(a[i].flag)            {                sum2-=a[i].v;                sum1+=a[i].v;            }            else            {                sum1-=a[i].v;                sum2+=a[i].v;            }            ans=max(ans,sum1*sum2);        }        cout<<ans<<endl;    }    return 0;}


原创粉丝点击