[BZOJ5099]Pionek

Description

\(n\) (\(n\le 2\times 10 ^5\)) 个向量,现在你在 \((0,0)\) ,选择一些向量使你走的最远。

Solution

自己的想法:按极角排序后双指针 \(l, r\) 扫,若选择 \(r + 1\) 向量走的更远就 r++ ,否则 l++ ,用 \([l,r]\) 的向量和与答案 \(chkmax\)

这样是错的,虽然答案最后一定是一段连续的区间,但这个并不满足局部最优,所以可能 \(r\) 指针需要舍弃一些不优的右移而到一个更好的位置。

题解首先按极角排序,然后答案一定是某一个半平面,即选一根过原点的x轴,x正半轴的向量都选,也是双指针扫一遍即可。

code

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <fstream>

typedef long long LL;
typedef unsigned long long uLL;

#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end()
#define MP(x, y) std::make_pair(x, y)
#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#define GO cerr << "GO" << endl;

using namespace std;

inline void proc_status()
{
    ifstream t("/proc/self/status");
    cerr << string(istreambuf_iterator<char>(t), istreambuf_iterator<char>()) << endl;
}

template<class T> inline T read() 
{
    register T x = 0; register int f = 1; register char c;
    while (!isdigit(c = getchar())) if (c == '-') f = -1;
    while (x = (x << 1) + (x << 3) + (c xor 48), isdigit(c = getchar()));
    return x * f;
}

template<typename T> inline bool chkmin(T &a, T b) { return a > b ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }

const int maxN = (int) 2e5;
const double PI = acos(-1);

struct Vector
{
    int x, y;
    double angle;
    
    Vector(int x = 0, int y = 0) : x(x), y(y) { angle = atan2(y, x); }
    
    bool operator < (const Vector& B) const { return angle < B.angle; }

    Vector operator + (const Vector& B) const { return Vector(x + B.x, y + B.y); }
    Vector operator - (const Vector& B) const { return Vector(x - B.x, y - B.y); }
} ;

int n;
Vector vec[maxN * 2 + 2];

void Input()
{
    n = read<int>();
    for (int i = 1; i <= n; ++i)
    {
        int x = read<int>(), y = read<int>();
        vec[i] = Vector(x, y);
    }
}

void Init()
{
    sort(vec + 1, vec + 1 + n);
    for (int i = 1; i <= n; ++i) vec[i + n] = vec[i], vec[i + n].angle += 2 * PI;
    n <<= 1;
}

LL dis(Vector cur) { return (LL)cur.x * cur.x + (LL)cur.y * cur.y; }

void Solve()
{
    LL ans(0);
    Vector cur(0, 0);

    for (int i = 1, j = 1; j <= n / 2; ++j)
    {
        while (i < n + j and vec[i].angle - vec[j].angle < PI)
            cur = cur + vec[i++], chkmax(ans, dis(cur));
        cur = cur - vec[j];
        chkmax(ans, dis(cur));
    }

    printf("%lld\n", ans);
}

int main() 
{
#ifndef ONLINE_JUDGE
    freopen("BZOJ5099.in", "r", stdin);
    freopen("BZOJ5099.out", "w", stdout);
#endif

    Input();

    Init();

    Solve();

    return 0;
}

转载于:https://www.cnblogs.com/cnyali-Tea/p/11495807.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值