HDU 6127 Hard challenge【极角排序】

来源:互联网 发布:jmeter数据库查询结果 编辑:程序博客网 时间:2024/06/07 20:50

题目来戳呀

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

题意

给出n个点的坐标以及此点的价值,任意两点能连成一条线段,线段的权值是连点的价值乘积。
让你做一条过原点且不过整数点的直线,希望这条直线经过的线段的权值之和最大,输出这个最大权值之和。

想法

这里写图片描述
记答案为ans
ans=p1*(p3+p4)+p2*(p3+p4)=(p1+p2)*(p3+p4)
直线以左的区间所有的点的价值之和×直线以右的区间所有的点的价值之和

我们初始化直线从y轴开始,将所有的点进行极角排序。
从小到大扫一遍排序后的点(相当于是直线逆时针旋转变换),若本来在左区间,就赶到右区间;本来在右区间,就赶到左区间,更新左右区间的值。
最后输出最大值即可。

其实题解就是那一个公式~配上图很好理解的吧(^__^)
代码很简单的辣~

#include<bits/stdc++.h>using namespace std;const int maxn=5e4+10;const double PI=acos(-1.0);struct point{    int x,y,val;    double ang;}p[maxn];bool cmp(point a,point b){    return a.ang<b.ang;}int main(){    int t,n;    long long lsum,rsum,ans;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        for(int i=0;i<n;++i)        {            scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].val);            if(p[i].x==0)///x=0不能做分母 要特殊判断            {                if(p[i].y>0)                    p[i].ang=PI/2.0;                else                    p[i].ang=-PI/2.0;            }            else            {                p[i].ang=atan(p[i].y*1.0/p[i].x);            }        }        sort(p,p+n,cmp);///极角排序        lsum=0,rsum=0;///初始化左右区间的值        for(int i=0;i<n;++i)        {            if(p[i].x>=0)                rsum+=p[i].val;            else                lsum+=p[i].val;        }        ans=lsum*rsum;        for(int i=0;i<n;++i)///更换区间        {            if(p[i].x>=0)            {                rsum-=p[i].val;                lsum+=p[i].val;            }            else            {                rsum+=p[i].val;                lsum-=p[i].val;            }            ans=max(ans,lsum*rsum);        }        printf("%lld\n",ans);    }    return 0;}

ps:真的是最近代码敲的最顺的了,1A(^o^)/~

原创粉丝点击