hdu6127 Hard challenge

地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6127

题目:

Hard challenge

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


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
2 2 1 1 1 1 -1 1 3 1 1 1 1 -1 10 -1 0 100
 

 

Sample Output
1 1100
 

 

Source
 
思路:极角排序后扫描线
 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 #define MP make_pair
 6 #define PB push_back
 7 typedef long long LL;
 8 typedef pair<int,int> PII;
 9 const double eps=1e-8;
10 const double pi=acos(-1.0);
11 const int K=1e6+7;
12 const int mod=1e9+7;
13 
14 struct Point
15 {
16     LL x,y,v;
17 }pt[K],st;
18 LL cross(const Point &po,const Point &ps,const Point &pe)
19 {
20     return (ps.x-po.x)*(pe.y-po.y)-(pe.x-po.x)*(ps.y-po.y);
21 }
22 bool cmp(const Point &ta,const Point &tb)
23 {
24     return cross(st,ta,tb)>0;
25 }
26 int main(void)
27 {
28     int t,n;cin>>t;
29     while(t--)
30     {
31         int se;
32         LL s1=0,s2=0,ans=0,mx=0;
33         scanf("%d",&n);
34         for(int i=0;i<n;i++)
35         {
36             scanf("%lld%lld%lld",&pt[i].x,&pt[i].y,&pt[i].v);
37             pt[i+n].x=-pt[i].x;
38             pt[i+n].y=-pt[i].y;
39             pt[i+n].v=0;
40             s2+=pt[i].v;
41         }
42         if(n==1) {printf("0\n");continue;}
43         sort(pt,pt+2*n,cmp);
44         s1+=pt[0].v;
45         for(int i=1;i<2*n;i++)
46         if(cross(st,pt[0],pt[i])<0)
47         {
48             se=i-1;break;
49         }
50         else
51             s1+=pt[i].v;
52         s2-=s1;
53         mx=ans=s1*s2;
54         for(int i=1,r=se;i<se;i++)
55         {
56             ans+=-pt[i].v*s2+(s1-pt[i].v)*pt[i].v;
57             s2+=pt[i].v,s1-=pt[i].v;
58             while(r+1<2*n&&cross(st,pt[i],pt[r+1])>=0)
59             {
60                 r++;
61                 ans+=-pt[r].v*s1+(s2-pt[r].v)*pt[r].v;
62                 s1+=pt[r].v,s2-=pt[r].v;
63             }
64             mx=max(mx,ans);
65         }
66         printf("%lld\n",mx);
67     }
68     return 0;
69 }

 

posted @ 2017-08-16 11:52  weeping  阅读(170)  评论(0编辑  收藏  举报