HDU 6127 Hard challenge (极角排序+二分, 2017 Multi-Univ Training Contest 7)

29 篇文章 0 订阅
12 篇文章 0 订阅

Problem

Problem Link

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.

Idea

在已知直线将坐标系分割为两部分时,求任意左上部分点到右下部分点的线段价值和,可以等效地看作是 val()×val() 。当点有序时,利用前缀和维护来求价值和的复杂度为 O(1)

显然由于通过过原点直线分割图形,可以将所有点按照极角排序。通过枚举点 i (x, y) ,假设直线恰好通过点 i ,二分获取最大的在极角排序中小于点 i 关于原点对称点 (-x, -y) (题目保证不会有任意两点的连线过原点)。则直接可以求当前划分所产生的价值和(当然,需要区分 i 点在左上或右下的情况,显然 i 过直线是不会产生价值,即总价值缩水,微偏移直线的斜率即可最大化当前枚举情况的最优。)

当然,不使用二分,利用双指针的做法来获取左上点与右下点的分界也可。

HINT: 由于此题的坐标取值较大,可能出现中间过程爆 int 的可能,最好将坐标 x , y 取 long long 。:joy: 没注意在这个问题上 WA 了两发。

Code

#include<bits/stdc++.h>
using namespace std;
const int N = 50000 + 10;
int T, n, pre[N];
struct point {
    long long x, y, val;
} p[N], ori, np;
double cross(const point &p1, const point &p2, const point &q1, const point &q2) {
    return (q2.y - q1.y)*(p2.x - p1.x) - (q2.x - q1.x)*(p2.y - p1.y);    
}
bool cmp(const point &a, const point &b) 
{
    if (a.y == 0 && b.y == 0 && a.x*b.x <= 0)return a.x>b.x;
    if (a.y == 0 && a.x >= 0 && b.y != 0)return true;
    if (b.y == 0 && b.x >= 0 && a.y != 0)return false;
    if (b.y*a.y <= 0)return a.y>b.y;
    return cross(ori,a,ori,b) > 0 || (cross(ori,a,ori,b) == 0 && a.x < b.x);    
}
bool jug(int idx, int mid) {
    np.x = -p[idx].x;    np.y = -p[idx].y;
    if(cmp(np, p[mid]) == true)    return false;
    return true;
}
int bs(int idx, int l, int r, int ans) {
    int mid;
    while(l <= r) {
        mid = (l+r) / 2;
        if(jug(idx, mid)) {
            l = mid + 1, ans = mid;
        } else {
            r = mid - 1;
        }
    }
    return ans;
}
long long calc(long long part) {
    return part * (pre[n] - part);
}
long long solve()
{
    long long ans = 0;
    for(int i=1, lft, rgt;i<=n;i++)
    {
        if(p[i].y < 0) {
            lft = bs(i, 1, i-1, 0);
            ans = max(ans, calc(pre[i] - pre[lft]));
            ans = max(ans, calc(pre[i-1] - pre[lft]));
        } else {
            rgt = bs(i, i+1, n, i);
            ans = max(ans, calc(pre[rgt] - pre[i]));
            ans = max(ans, calc(pre[rgt] - pre[i-1]));
        }
    }
    return ans;
}
int main()
{
    ori.x = ori.y = 0;
    scanf("%d", &T);
    while(T-- && scanf("%d", &n)!=EOF)
    {
        for(int i=1;i<=n;i++)
            scanf("%lld %lld %lld", &p[i].x, &p[i].y, &p[i].val);
        sort(p+1, p+n+1, cmp);

        for(int i=1;i<=n;i++) {
            pre[i] = pre[i-1] + p[i].val;
        }

        printf("%lld\n", solve());
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值