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;
}

最新文章

  1. 旺财速啃H5框架之Bootstrap(一)
  2. Struts2 验证码图片实例
  3. jQuery的基本用法:
  4. nginx 配置rewrite 笔记
  5. java.lang.NoClassDefFoundError: Could not initialize class ......
  6. apache的开源项目-模板引擎(Velocity)(转)
  7. 1067: spark.components:NavigatorContent 类型值的隐式强制指令的目标是非相关类型 String
  8. 在CDlinux下编译安装无线网卡驱动
  9. Laravel框架使用的一些注意细节(一)
  10. hdu 5225 Tom and permutation(回溯)
  11. 【HDU4622】Reincarnation(后缀自动机)
  12. springboot~内嵌redis的使用
  13. Hive记录-Hive常用命令操作
  14. python学习之老男孩python全栈第九期_day005知识点总结
  15. 【代码审计】iCMS_v7.0.7 admincp.app.php页面存在SQL注入漏洞分析
  16. angularJS1笔记-(6)-自定义过滤器
  17. CF600E Lomsat gelral 【线段树合并】
  18. 常用的经典jquery代码[转]
  19. Python函数-闭包的概念
  20. 一个性能较好的JVM参数配置(转)

热门文章

  1. 如何获得带转义的json内容
  2. Centos7.5源码安装nginx-1.16.0
  3. PHP简单的爬虫–原型
  4. CF506E Mr. Kitayuta&#39;s Gift
  5. vscode-函数注释插件-正则插件
  6. js学习之BOM和DOM
  7. Eclipse Debug模式的开启与关闭问题简析_java - JAVA
  8. SpringCloud学习系列-Eureka服务注册与发现(3)
  9. JavaScript算数运算符和一元运算符
  10. [springboot jpa] [bug] Could not open JPA EntityManager for transaction