hdu 6127 Hard challenge

http://acm.hdu.edu.cn/showproblem.php?pid=6127
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(1≤T≤5), denoting the number of test cases.
For each test case:
The first line contains a positive integer n(1≤n≤5×104).
The next n lines, the ith line contains three integers xi,yi,vali(|xi|,|yi|≤109,1≤vali≤104).

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

题目大意:给你一些点和点的权值。用线段将所有的点两两连接起来,确定任意两个点的连线不过原点,线段的权重为这两个点的权值相乘,找一条过原点但不过题目给出那些点,这条直线的权重为直线经过所有线段权值的和,求这个直线和的最大值。

解题思路:枚举所有满足条件的直线,选出其中权值最大的值 。现在的任务就是如何枚举出所有的直线。我们将所有的点按照与x轴的角度从小到大排序(角度范围为(-PI/2—PI/2))。然后以y轴为最开始的直线,将点分为 l 和 r 两个部分逆时针转动,每找到一个节点就将这个节点换到另一部分,不断更新最大值的值,最后的值就是结果。

#include <bits/stdc++.h>
#define PI acos(-1.0)
using namespace std;
const int N=5e4+10;
struct Point
{
    int x,y;///点的坐标
    long long val;///点的权值
    double ang;///点和x轴的夹角
    bool operator<(const Point a)const///将角度从小到大排序
    {
        return ang < a.ang;
    }
};
Point p[N];
int main()
{
    int T;scanf("%d",&T);
    while(T--){
        int n;scanf("%d",&n);
        for(int i=0;i<n;i++){
            scanf("%d %d %lld",&p[i].x,&p[i].y,&p[i].val);
            if(p[i].x == 0){
                if(p[i].y>0)    p[i].ang = PI/2.0;///y轴上的点单独处理
                else  p[i].ang = -PI/2.0;
            }
            else    p[i].ang = atan((p[i].y*1.0)/(p[i].x*1.0));
        }
        sort(p,p+n);///将角度排序
        long long int lsum = 0,rsum = 0;
        for(int i=0;i<n;i++){///以y轴为起始直线分别记录左右两边的点的权值和。
            if(p[i].x<0)
                lsum+=p[i].val;
            else
                rsum+=p[i].val;
        }
        long long ans = lsum*rsum;///将左右的权值相乘作为初始值
        for(int i=0;i<n;i++){///每找到一个点就将这个点转换到另一部分,更新ans的值。
            if(p[i].x>0){
                rsum -= p[i].val;
                lsum += p[i].val;
            }
            else{
                rsum += p[i].val;
                lsum -= p[i].val;
            }
            ans = max(ans,lsum*rsum);
        }
        printf("%lld\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值