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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章