2017 Multi-University Training Contest

来源:互联网 发布:做网站必备软件 编辑:程序博客网 时间:2024/04/24 21:30

题目:

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 integerT(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
题意:

给出一些点,任意两点不在同一张直线上,每个点都有自己的价值,任意两点连成的线段的价值为两个点的价值相乘,求一条过原点的直线经过的线段的价值的和的最大值

思路:

假设有三个点,(x1,y1)、(x2,y2)在直线的左边;(x3,y3)在直线的右边,那么直线经过的线段的价值的和为:c1*c3+c2*c3。所以可以推出直线经过的价值的和为直线左边的点的价值和与直线右边的点的价值和的乘积。利用sort,根据每个点的角度进行排序,从小到大进行遍历,逐步更新最大值

code:
#include<bits/stdc++.h>using namespace std;#define pl acos(-1.0)struct node{        int x,y;        __int64 val;        double ang;}q[50005];int n;int cmp(node a,node b){        return a.ang<b.ang;}int main(){        int t,i;        scanf("%d",&t);        while(t--){                scanf("%d",&n);                __int64 lnum=0,rnum=0;                for(i=0;i<n;i++){                        scanf("%d%d%I64d",&q[i].x,&q[i].y,&q[i].val);                        if(!q[i].x){                                if(q[i].y<0) q[i].ang=-pl/2.0;                                else q[i].ang=pl/2.0;                        }                        else q[i].ang=atan(q[i].y*1.0/q[i].x);                        if(q[i].x>=0) rnum+=q[i].val;                        else lnum+=q[i].val;                }                sort(q,q+n,cmp);                __int64 ans=lnum*rnum;                for(i=0;i<n;i++){                        if(q[i].x>=0){                                lnum+=q[i].val;                                rnum-=q[i].val;                        }                        else{                                lnum-=q[i].val;                                rnum+=q[i].val;                        }                        ans=max(ans,lnum*rnum);                }                printf("%I64d\n",ans);        }        return 0;}


原创粉丝点击