HDU 6127 Hard challenge【极角排序】

本篇介绍了一道算法题目,目标是在平面上给定若干带权值的点,通过绘制一条不过这些点的直线,使得该直线穿越尽可能多的线段,每条线段的权值为其两端点的权值乘积。文章详细解释了解题思路,并提供了一个基于C++的实现代码。
摘要由CSDN通过智能技术生成

题目来戳呀

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

Source

2017 Multi-University Training Contest - Team 7

题意

给出n个点的坐标以及此点的价值,任意两点能连成一条线段,线段的权值是连点的价值乘积。
让你做一条过原点且不过整数点的直线,希望这条直线经过的线段的权值之和最大,输出这个最大权值之和。

想法

这里写图片描述
记答案为ans
ans=p1*(p3+p4)+p2*(p3+p4)=(p1+p2)*(p3+p4)
直线以左的区间所有的点的价值之和×直线以右的区间所有的点的价值之和

我们初始化直线从y轴开始,将所有的点进行极角排序。
从小到大扫一遍排序后的点(相当于是直线逆时针旋转变换),若本来在左区间,就赶到右区间;本来在右区间,就赶到左区间,更新左右区间的值。
最后输出最大值即可。

其实题解就是那一个公式~配上图很好理解的吧(^__^)
代码很简单的辣~

#include<bits/stdc++.h>
using namespace std;
const int maxn=5e4+10;
const double PI=acos(-1.0);
struct point
{
    int x,y,val;
    double ang;
}p[maxn];
bool cmp(point a,point b)
{
    return a.ang<b.ang;
}
int main()
{
    int t,n;
    long long lsum,rsum,ans;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=0;i<n;++i)
        {
            scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].val);
            if(p[i].x==0)///x=0不能做分母 要特殊判断
            {
                if(p[i].y>0)
                    p[i].ang=PI/2.0;
                else
                    p[i].ang=-PI/2.0;
            }
            else
            {
                p[i].ang=atan(p[i].y*1.0/p[i].x);
            }
        }
        sort(p,p+n,cmp);///极角排序
        lsum=0,rsum=0;///初始化左右区间的值
        for(int i=0;i<n;++i)
        {
            if(p[i].x>=0)
                rsum+=p[i].val;
            else
                lsum+=p[i].val;
        }
        ans=lsum*rsum;
        for(int i=0;i<n;++i)///更换区间
        {
            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;
}

ps:真的是最近代码敲的最顺的了,1A(^o^)/~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值