Hard challenge(HDU6127)

来源:互联网 发布:java.io.temp 编辑:程序博客网 时间:2024/05/18 20:11

题意:给定N个点,每个点都有一个value,每两个点都连一条线段,这条线段的value是两个端点的value值的乘积,现在划一条经过原点的直线,这条直线经过的左右线段的value值的和即为直线的value,题目要求最大的直线的value


分析:题目可以转化为枚举每一个点,用原点和枚举的点确定的直线将所有点划分为两部分,其实这条直线的value就是直线两边的点的value值的和的乘积,因此这题先对所有点极角排序,然后枚举所有即可。


AC代码:

#include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <map>#include <set>#include <vector>#include <queue>#include <cmath>#include <bitset>using namespace std;const int MAXN = 50005;typedef long long ll;struct point{    ll x, y, value;    bool operator < (const point& ano) const    {        if(y * ano.y < 0) return y > ano.y;        if( (y == 0 && ano.y < 0) || (y < 0 && ano.y == 0)) return y > ano.y;        return x * ano.y - y * ano.x > 0;//正是因为这里有x * y, x,y的数据量为1e9        //一开始用的是int,会溢出,从而WA了好几次    }}ps[MAXN];int main(){    //freopen("1008.in", "r", stdin);    //freopen("out.out", "w", stdout);    int CASE;    scanf("%d", &CASE);    while (CASE--) {        int n;        scanf("%d", &n);        for (int i = 0; i < n; ++i) {            scanf("%lld%lld%lld", &ps[i].x, &ps[i].y, &ps[i].value);        }        sort(ps, ps + n);        //for(int i = 0; i < n; ++i)            //printf("%d %d %d\n", ps[i].x, ps[i].y, ps[i].value);        int pos = -1;        ll s1 = 0LL, s2 = 0LL;        for (int i = 0; i < n; ++i) {            if(ps[i].y >= 0) s1 += ps[i].value;            else            {                if(pos == -1) pos = i;                s2 += ps[i].value;            }        }        if(pos == -1) pos = 0;        ll ans = s1 * s2;        int i = 0;        if(ps[0].y == 0) i += 1, ans = max(ans, (s1 - ps[0].value) * (s2 + ps[0].value));        for( ; i < n; ++i)        {            if(i > 0) s1 -= ps[i - 1].value, s2 += ps[i - 1].value;            while(-ps[pos].y * ps[i].x + ps[pos].x * ps[i].y < 0)            {                s1 += ps[pos].value;                s2 -= ps[pos].value;                pos = (pos + 1) % n;            }            ans = max(ans, max(s1 * s2, (s1 - ps[i].value) * (s2 + ps[i].value)));        }        printf("%lld\n", ans);    }    return 0;}




原创粉丝点击