HDU 6127 Hard challenge

来源:互联网 发布:淘宝男士小脚裤 编辑:程序博客网 时间:2024/06/14 12:57

Hard challenge

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


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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6127

题意:平面上n个点,每个点有一个权值且每个点的坐标都是不同的,已知任意两点确定的直线不经过原点,问原点做一条不经过给定点的直线中,与直线相交的线段的最大权值之和是多少?线段的权值等于它两端点权值的乘积。

解题思路:这道题是昨天多校第七场中的一道AC人数居第三的题目,但是我们队在昨天的比赛中没有做出来,主要原因在我,理解错题意把队友给坑了,如果那时候把题目仔细看清楚再跟他讲的话或许还有希望A,但没这么多如果,所以就老老实实补题吧。其实理解题意再充分利用给定的条件是不难的,只要把n个点(y轴上的点特判一下)的极角(斜率也是一样的)按从小到大的顺序排序,排序后将n个点扫描一遍就好了。(可以理解为从y轴逆时针扫描),这里有一个技巧,直线左边的点可以与右边的任意一点连成线段,所以线段权值之和就可以变成左边点权值之和乘上右边点权值之和。(x1*y1)+(x1*y2)+(x2*y1)+(x2*y2)=(x1+x2)*(y1+y2)。

AC代码:

#include<cstdio>#include<cmath>#include<algorithm>using namespace std;const int MAXN = 50000 + 10;const double PI = acos(-1.0);struct Point{//定义点的结构体 int x,y; //坐标 long long val; //权值 double ang; //点的极角 };Point point[MAXN];int N;bool cmp(Point p1,Point p2) //按点的极角(斜率)升序排序 {return p1.ang<p2.ang;}void solve(){long long lsum=0; //过原点直线的左边区域 long long rsum=0; //过原点直线的右边区域 long long ans; //答案 sort(point,point+N,cmp); //排序 for(int i=0;i<N;i++) //初始时假定直线为y轴 {if(point[i].x>=0) rsum+=point[i].val; //直线右边,即x右半轴 else lsum+=point[i].val; //直线左边,即x的左半轴 }ans=lsum*rsum; //左边权值乘上右边权值 for(int i=0;i<N;i++) //遍历N个点 {if(point[i].x>=0) //当前点如果之前在直线右边,那么现在变为直线左边的点 {rsum-=point[i].val;lsum+=point[i].val;}else //当前点如果之前在直线左边,同理 {rsum+=point[i].val;lsum-=point[i].val;}ans=max(ans,lsum*rsum); //更新ans }printf("%lld\n",ans);}int main(void){int T; //测试组数 scanf("%d",&T);while(T--){scanf("%d",&N);for(int i=0;i<N;i++){scanf("%d%d%lld",&point[i].x,&point[i].y,&point[i].val);if(point[i].x==0) //y轴上的点特判 {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;}


原创粉丝点击