2017 Multi-University Training Contest - Team 7 :1008&hdu6127、Hard challenge

题目:

Problem Description
There are n points on the plane, and the i th 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 i th 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
题意:

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

思路:

假设有三个点,(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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值