HDU 6127 Hard challenge(思维+计算几何)——2017 Multi-University Training Contest - Team 7

39 篇文章 0 订阅
26 篇文章 0 订阅

传送门

Hard challenge

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


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

题目大意:

平面直角坐标系上有 n 个整点,第 i 个点有一个点权 vali ,坐标为 (xi,yi) ,其中不存在任意两点连成的直线经过原点。这些整点两两之间连有一条线段,线段的权值为其两端点的权值之积。你需要作一条过原点而不过任意一个给定整点的直线,使得和这条直线相交的线段的权值和最大。 1n5×104,1vali104,|xi|,|yi|109

解题思路:

经过推导之后会发现:这条经过原点的直线 l 将所有的点分为两部分(直线上与直线下),最终的答案就是两部分的和的乘积,发现这个性质之后,将所有点按照斜率(极角)排序,然后扫一遍进行判断就行了。需要注意的是,我们可以预处理出从 (0,0) 点 到 p0 点这条直线的上( sum1 )下( sum2 )两部分的值,然后剩下的就根据这个初始的 sum1sum2 进行计算,具体可以见代码。
代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 5e4+5;
const LL MOD = 1e9+7;
struct Point{
    LL x, y, v;
    double ang;
}a[MAXN];
int cmp(Point A, Point B){
    return A.ang < B.ang;
}
int check(Point A, Point B){//>=0:up  <0:down
    return 1.0*(A.x*B.y-A.y*B.x)*A.x >= 0;
}
int main(){
    //freopen("C:/Users/yaonie/Desktop/in.txt", "r", stdin);
    //freopen("C:/Users/yaonie/Desktop/out.txt", "w", stdout);
    int T; scanf("%d",&T);
    while(T--){
        int n; scanf("%d", &n);
        LL sumv = 0;
        for(int i=0; i<n; i++){
            scanf("%lld%lld%lld",&a[i].x,&a[i].y,&a[i].v);
            if(a[i].x != 0) a[i].ang = 1.0*a[i].y/a[i].x;
            else a[i].ang = MOD;
            sumv += a[i].v;
        }
        sort(a, a+n, cmp);
        LL sum1 = a[0].v, sum2 = 0;//sum1:up sum2:down
        int cnt = 0;
        for(int i=1; i<n; i++) if(check(a[0], a[i])) sum1 += a[i].v;
        sum2 = sumv - sum1;
        LL ans = sum1*sum2;
        if(a[0].x > 0) sum1 -= a[0].v;
        for(int i=1; i<n; i++){
            sum2 = sumv - sum1;
            ans = max(ans, sum1*sum2);
            if(a[i].x > 0) sum1 -= a[i].v;
            if(a[i].x < 0) sum1 += a[i].v;
            sum2 = sumv - sum1;
            ans = max(ans, sum1*sum2);
        }
        printf("%lld\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值