hdu 6127 计算几何模拟题(旋转扫描线)

来源:互联网 发布:三浦翔平人不好知乎 编辑:程序博客网 时间:2024/05/01 21:38

Hard challenge

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


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


题意:

给出n个点,以及点上的权值,任意两条线可以连线,线上的权值是两个点的权值的乘积

题解:

容易发现,用一条线分割开后,就是两个边的和的乘积了

此时我们用极坐标排序,然后用一条射线进行扫描处理即可

队友的代码,惋惜


#include<math.h>#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define MAXN 50005#define LL long longstruct Point{    LL x,y;    int v;    double rad;    bool operator < (const Point&rhs) const    {        return rad < rhs.rad;    }} p[MAXN];LL sum[MAXN];bool left(Point a, Point b){    return (LL)a.x*b.y - (LL)a.y*b.x >= 0;}int main(){    int n,m,T;    //freopen("in.txt","r",stdin);    scanf("%d",&T);    while(T--)    {        scanf("%d",&n);        for(int i=0;i<n;i++){            scanf("%I64d%I64d%d",&p[i].x,&p[i].y,&p[i].v);            p[i].rad=atan2(p[i].y, p[i].x);        }        sort(p,p+n);        sum[0]=p[0].v;        for(int i=1;i<n;i++)            sum[i]=sum[i-1]+p[i].v;        LL ans=0;        LL  L=0,R=0,cnt=0;///L为分割点,R为扫描线        while(L<n)        {            if(R==L){                R=(R+1)%n;                cnt++;            }            while(R!=L&&left(p[L],p[R]))   ///R不等于L并且在180度之内            {                R=(R+1)%n;                cnt++;            }            cnt--; ///分隔线旋转,原本在分隔线上的点到了右边,所以要减去            LL t1,t2;            int num=L+cnt;            if(num<n)            {                t1=sum[num]-sum[L];                t2=sum[n-1]-t1;                ans=max(ans,t1*t2);            }            else            {                t1=sum[n-1]-sum[L]+sum[num-(n-1)-1];                t2=sum[n-1]-t1;                ans=max(ans,t1*t2);            }            L++;   ///分隔线旋转        }        printf("%I64d\n",ans);    }    return 0;}