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;
}
发布了105 篇原创文章 · 获赞 15 · 访问量 3万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章