HDU 6127 Hard challenge(几何)

本文探讨了一道算法题目,要求在平面上给定点集的情况下,寻找一条通过原点但不经过任何给定点的直线,使得该直线与各点间形成的线段的价值总和最大。文章提供了详细的解题思路及C++实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Hard challenge


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个点,给出每个点的坐标和value,相互连接,形成n*(n-1)/2条线段,每条线段的value等于两端点的value相乘。找出一个过原点的直线,使得与其相交的线段value值和最大。


题解:对于一条直线,线段权值和实际上就等于其两边点权和的乘积。把所有点按斜率排序,从大到小遍历扫描。


#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define ll long long
#define INF 1e9+10
#define MAXN 305
using namespace std;
int n;
struct node
{
    double x;
    double y;
    double k;
    ll v;
}a[50005],b[50005];
int ca;
int cb;
bool cmp(node f,node g)
{
    return f.k>g.k;
}
int main()
{

    int t;
    scanf("%d",&t);

    for(int w=1;w<=t;w++)
    {
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        ca=0;cb=0;
        scanf("%d",&n);
        double x,y;
        ll z;
        ll sum1=0;
        ll sum2=0;
        for(int i=0;i<n;i++)
        {
            scanf("%lf%lf%lld",&x,&y,&z);
            if(x<0||(x==0&&y<0))
            {
                b[cb].x=x;
                b[cb].y=y;
                b[cb].v=z;
                if(x==0&&y<0) b[cb].k=INF;
                else if(x==0&&y>0) b[cb].k=INF;
                else b[cb].k=y/x;
                sum2+=z;
                cb++;
            }
            else
            {
                a[ca].x=x;
                a[ca].y=y;
                a[ca].v=z;
                if(x==0&&y<0) a[ca].k=INF;
                else if(x==0&&y>0) a[ca].k=INF;
                else a[ca].k=y/x;
                sum1+=z;
                ca++;
            }
        }

        ll ans;
        ans=sum1*sum2;
        sort(a,a+ca,cmp);
        sort(b,b+cb,cmp);
        int h=0;
        int l=0;
        while(1)
        {
            if((l==cb||a[h].k>b[l].k)&&h<ca)
               {
                   sum1-=a[h].v;
                   sum2+=a[h].v;
                    h++;
               }
            else if((h==ca||a[h].k<b[l].k)&&l<cb)
              {
                  sum1+=b[l].v;
                  sum2-=b[l].v;
                   l++;
              }
              //cout<<l<<" "<<h<<endl;
              ans=max(ans,sum1*sum2);
              if(l>=cb&&h>=ca) break;
        }
        cout<<ans<<endl;

    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值