HDU6127Hard challenge

Hard challenge

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 1305    Accepted Submission(s): 557


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(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
2 2 1 1 1 1 -1 1 3 1 1 1 1 -1 10 -1 0 100
 

 

Sample Output
1 1100
 

 

Source
/*
* @Author: lyuc
* @Date:   2017-08-15 15:31:46
* @Last Modified by:   lyuc
* @Last Modified time: 2017-08-17 09:01:32
*/
/*
 题意:给你n个点,每个点都有权值,并且每两个点之间都有一条线,这条线的权值就是端点的权值之积,问你从过原点画一条直线,    
     使得穿过直线的权值之和最大

 思路:这里有一个推论,假如有四个已经按照极角排好序的点,a,b,c,d 有条直线在 b c之间,那么两两之间直线权值和为:
     ac+ad+bc+bd=a*(c+d)+b*(c+d)=(a+b)*(c+d);  这样题目就出来了,首先将所有的点按照极角排序,然后遍历所有的点
     ,比较得到的每个点
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>

#define LL long long
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define MAXN 50005

using namespace std;

struct Point{
    int x,y;
    LL val;
    double cur;
}point[MAXN];
int t,n;
LL lres,rres;
LL res;

inline bool cmp(Point a,Point b){
    return a.cur<b.cur;
}

inline void init(){
    lres=0;
    rres=0;
    res=0;
}

int main(){ 
    // freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
    scanf("%d",&t);
    while(t--){
        init();
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            scanf("%d%d%lld",&point[i].x,&point[i].y,&point[i].val);
            if(point[i].x==0){//如果在y轴
                if(point[i].y>=0)
                    point[i].cur=pi/2;
                else
                    point[i].cur=-pi/2;
            }else{
                point[i].cur=atan(point[i].y*1.0/point[i].x);
            }
            if(point[i].x>=0){
                rres+=point[i].val;
            }else{
                lres+=point[i].val;
            }
        }
        sort(point,point+n,cmp);
        res=lres*rres;
        for(int i=0;i<n;i++){
            if(point[i].x>=0){
                rres-=point[i].val;
                lres+=point[i].val;
            }else{
                lres-=point[i].val;
                rres+=point[i].val;                
            }
            res=max(res,lres*rres);
        }
        printf("%lld\n",res);
    }
    return 0;
}

 

posted @ 2017-08-17 09:03  勿忘初心0924  阅读(208)  评论(0编辑  收藏  举报