HDU 6127 Hard challenge【几何】

来源:互联网 发布:c语言字符串指针传递 编辑:程序博客网 时间:2024/06/04 23:32

Hard challenge

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


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个点(y轴上的点特判一下)的斜率按从小到大的顺序排序,排序后将n个点扫描一遍就好了。(可以理解为从y轴逆时针扫描),这里有一个技巧,直线左边的点可以与右边的任意一点连成线段,所以线段权值之和就可以变成左边点权值之和乘上右边点权值之和。(x1*y1)+(x1*y2)+(x2*y1)+(x2*y2)=(x1+x2)*(y1+y2)。


#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<queue>#include<stack>#include<vector>#include<map>#include<set>#include<algorithm>using namespace std;#define ll long long#define ms(a,b)  memset(a,b,sizeof(a))const int M=1e5+10;const int MM=2e3+10;const int inf=0x3f3f3f3f;const int mod=1e9+7;;const double eps=1e-8;const double pi=acos(-1.0);int n,m;struct Point{    int x,y;    int val;    double ang;}point[M];bool cmp(Point p1,Point p2){    return p1.ang>p2.ang;}void solve(){    ll sum1=0,sum2=0;    ll ans=0;    sort(point,point+n,cmp);    for(int i=0;i<n;i++){        if(point[i].x>=0)sum1+=point[i].val;        else sum2+=point[i].val;    }    ans=sum1*sum2;    for(int i=0;i<n;i++){      if(point[i].x>=0){            sum1-=point[i].val;            sum2+=point[i].val;      }      else {           sum1+=point[i].val;           sum2-=point[i].val;      }      ans=max(ans,sum1*sum2);    }    printf("%lld\n",ans);}int main(){    int t;    scanf("%d",&t);    while(t--){        scanf("%d",&n);        for(int i=0;i<n;i++){            scanf("%d%d%d",&point[i].x,&point[i].y,&point[i].val);            if(point[i].x==0){                if(point[i].y>=0)point[i].ang=pi/2.0;                else point[i].ang=-pi/2.0;            }            else point[i].ang=atan(point[i].y*1.0/point[i].x);        }        solve();    }    return 0;}


原创粉丝点击