「SOL」E-Lite (Ural Championship 2013)
为什么这数据能水到可以枚举角度 ac 啊
# 题面
给你 \(n\) 个平面向量 \((x_i,y_i)\),对于每个 \(k=1\sim n\),求「从给出的 \(n\) 个向量中不重复地选择 \(k\) 个,\(k\) 个向量的和的模长最大是多少」。
数据规模:\(n\le1000\)。
# 解析
这种「选择 \(k\) 个」的题目,我们之前往往会从 DP 考虑,或者贪心求解。但是我们发现向量并不满足局部最优就是全局最优。
于是这道题我们换一个思路,不从选的过程考虑,而从选的结果 —— 也就是答案的角度考虑。
如果我们知道了答案为 \(\mathbf{v}\),那么一定是由在 \(\mathbf{v}\) 方向上投影最大的 \(k\) 个向量组成的。于是我们可以尝试「旋转」答案向量的方向,然后贪心地选取向量。
虽然数据水,离散地枚举答案向量角度可以 ac,但是角度毕竟是连续的,这种做法不是很靠谱(但是很难卡掉)。
连续的枚举一般考虑枚举临界点。不妨设我们逆时针旋转答案向量 \(\mathbf{v}\),记 id[i] 表示当前在 \(\mathbf{v}\) 方向上投影从大到小第 \(i\) 个向量是哪一个,同理定义 rnk[i] 表示 \(i\) 向量的排名(rnk[id[i]] = i)。
我们发现只有 id 发生变化 —— 也即两个向量的相对投影大小改变时,答案才会改变。设 \(\mathbf{u,v}\) 为两个方向不同的向量:

于是一对向量会产生两个临界点,总共会有 \(\mathcal{O}(n^2)\) 个临界。将它们极角排序过后逆时针扫一遍。
每经过一个临界点,就会有 rank 相邻的两个向量的 rank 发生交换(记为 rnk, rnk + 1)。扫描时,维护当前 rank,当 rnk, rnk + 1 交换时,只会改变前 rnk 个和前 rnk + 1 个向量的和,\(\mathcal{O}(1)\) 更新答案即可。
唯一的麻烦点是给出的 \(n\) 个向量可能重叠……我的处理是把重叠的向量看成一个,记录一下个数。只能自己意会一下或者看一看代码了。
# 源代码
/*Lucky_Glass*/
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cassert>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long double ldouble;
const int N = 1005;
const ldouble EPS = 1e-12;
#define con(typ) const typ &
#define sec second
#define fir first
template<class typ> typ iAbs(con(typ) key) {return key < 0 ? -key : key;}
template<class typ> int sgn(con(typ) key) {
if ( iAbs(key) <= EPS ) return 0;
return key < 0 ? -1 : 1;
}
struct Vector {
ldouble x, y;
Vector() {}
Vector(con(ldouble) _x, con(ldouble) _y) : x(_x), y(_y) {}
ldouble len() const {return x * x + y * y;}
Vector operator - (con(Vector) p) const {
return Vector(x - p.x, y - p.y);
}
Vector operator + (con(Vector) p) const {
return Vector(x + p.x, y + p.y);
}
friend ldouble dot(con(Vector) p, con(Vector) q) {
return p.x * q.x + p.y * q.y;
}
Vector operator -() const {return Vector(-x, -y);}
bool operator != (con(Vector) p) const {
return sgn(x - p.x) || sgn(y - p.y);
}
bool operator < (con(Vector) p) const {
if ( sgn(x - p.x) ) return sgn(x - p.x) < 0;
return sgn(y - p.y) < 0;
}
Vector cwise90() const {return Vector(y, -x);}
} sum[N];
struct Data {
Vector v; int cnt;
Data() {}
Data(con(Vector) _v, con(int) _c) : v(_v), cnt(_c) {}
} dat[N];
int nn, n, ndv;
pair<int, int> inp[N];
int cnt[N], rnk[N];
ldouble ans[N];
struct Divi {
int a, b;
ldouble ang;
Divi() {}
Divi(con(int) _a, con(int) _b, con(ldouble) _ang)
: a(_a), b(_b), ang(_ang) {}
bool operator == (con(Divi) p) const {return !sgn(ang - p.ang);}
static bool cmpAng(con(Divi) p, con(Divi) q) {return sgn(p.ang - q.ang) < 0;}
static bool cmpID(con(Divi) p, con(Divi) q) {
if ( rnk[p.a] != rnk[q.a] ) return rnk[p.a] < rnk[q.a];
return rnk[p.b] < rnk[q.b];
}
} dv[N * N];
void init() {
sum[0] = Vector(0, 0);
for (int i = 1, tmp = 0; i <= n; i++) {
for (int j = 1; j <= dat[i].cnt; j++) {
tmp++;
sum[tmp] = sum[tmp - 1] + dat[i].v;
ans[tmp] = sum[tmp].len();
}
rnk[i] = i, cnt[i] = tmp;
}
}
// q is better than p then
void done(con(int) p, con(int) q) {
// assert( rnk[p] == rnk[q] - 1 );
int tmp = cnt[rnk[p] - 1];
for (int i = 1; i <= dat[q].cnt; i++) {
tmp++;
sum[tmp] = sum[tmp - 1] + dat[q].v;
ans[tmp] = max(ans[tmp], sum[tmp].len());
}
cnt[rnk[p]] = tmp;
for (int i = 1; i <= dat[p].cnt; i++) {
tmp++;
sum[tmp] = sum[tmp - 1] + dat[p].v;
ans[tmp] = max(ans[tmp], sum[tmp].len());
}
swap(rnk[p], rnk[q]);
}
int main() {
scanf("%d", &nn);
for (int i = 1; i <= nn; i++)
scanf("%d%d", &inp[i].fir, &inp[i].sec);
sort(inp + 1, inp + 1 + nn);
for (int i = 1; i <= nn;) {
int j = i;
while ( j <= nn && inp[i] == inp[j] ) j++;
dat[++n] = Data(Vector(inp[i].fir, inp[i].sec), j - i);
inp[n] = inp[i];
i = j;
}
for (int i = 1; i <= n; i++)
for (int j = i + 1; j <= n; j++) {
double k1 = atan2(inp[j].fir - inp[i].fir, inp[i].sec - inp[j].sec),
k2 = atan2(inp[i].fir - inp[j].fir, inp[j].sec - inp[i].sec);
dv[++ndv] = Divi(j, i, k1);
dv[++ndv] = Divi(i, j, k2);
}
sort(dv + 1, dv + 1 + ndv, Divi::cmpAng);
init();
for (int i = 1; i <= ndv; ) {
int j = i;
while ( j <= ndv && dv[i] == dv[j] ) j++;
sort(dv + i, dv + j, Divi::cmpID);
while ( i < j ) {
done(dv[i].a, dv[i].b);
i++;
}
}
for (int i = 1; i <= nn; i++)
printf("%.8f\n", (double)sqrt(ans[i]));
return 0;
}
THE END
Thanks for reading!
「SOL」E-Lite (Ural Championship 2013)的更多相关文章
- loj#2013. 「SCOI2016」幸运数字 点分治/线性基
题目链接 loj#2013. 「SCOI2016」幸运数字 题解 和树上路径有管...点分治吧 把询问挂到点上 求出重心后,求出重心到每个点路径上的数的线性基 对于重心为lca的合并寻味,否则标记下传 ...
- loj #2013. 「SCOI2016」幸运数字
#2013. 「SCOI2016」幸运数字 题目描述 A 国共有 n nn 座城市,这些城市由 n−1 n - 1n−1 条道路相连,使得任意两座城市可以互达,且路径唯一.每座城市都有一个幸运数字,以 ...
- AC日记——「SCOI2016」幸运数字 LiBreOJ 2013
「SCOI2016」幸运数字 思路: 线性基: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 20005 # ...
- FileUpload控件「批次上传 / 多档案同时上传」的范例--以「流水号」产生「变量名称」
原文出處 http://www.dotblogs.com.tw/mis2000lab/archive/2013/08/19/multiple_fileupload_asp_net_20130819. ...
- 一本通1648【例 1】「NOIP2011」计算系数
1648: [例 1]「NOIP2011」计算系数 时间限制: 1000 ms 内存限制: 524288 KB [题目描述] 给定一个多项式 (ax+by)k ,请求出多项式展开后 x ...
- 「SCOI2016」背单词
「SCOI2016」背单词 Lweb 面对如山的英语单词,陷入了深深的沉思,「我怎么样才能快点学完,然后去玩三国杀呢?」.这时候睿智的凤老师从远处飘来,他送给了 Lweb 一本计划册和一大缸泡椒,然后 ...
- loj2009. 「SCOI2015」小凸玩密室
「SCOI2015」小凸玩密室 小凸和小方相约玩密室逃脱,这个密室是一棵有 $ n $ 个节点的完全二叉树,每个节点有一个灯泡.点亮所有灯泡即可逃出密室.每个灯泡有个权值 $ A_i $,每条边也有个 ...
- 「译」JUnit 5 系列:条件测试
原文地址:http://blog.codefx.org/libraries/junit-5-conditions/ 原文日期:08, May, 2016 译文首发:Linesh 的博客:「译」JUni ...
- 「译」JUnit 5 系列:扩展模型(Extension Model)
原文地址:http://blog.codefx.org/design/architecture/junit-5-extension-model/ 原文日期:11, Apr, 2016 译文首发:Lin ...
- JavaScript OOP 之「创建对象」
工厂模式 工厂模式是软件工程领域一种广为人知的设计模式,这种模式抽象了创建具体对象的过程.工厂模式虽然解决了创建多个相似对象的问题,但却没有解决对象识别的问题. function createPers ...
随机推荐
- Kubernetes--管理Pod对象的容器(3)
共享节点的网络名称空间 同一个Pod对象的各容器均运行于一个独立的.隔离的Network名称空间中,共享同一个网络协议栈及相关的网络设备.也有一些特殊的Pod对象需要运行于所在节点的名称空间中,执行系 ...
- my.ini
[client] #客户端设置,即客户端默认的连接参数 # socket = /data/mysqldata/3306/mysql.sock #用于本地连接的socket套接字 # 默认连接端口 po ...
- 分享: 第二性 合卷本 竖本.PDF
书本详情 第二性台版 作者: 西蒙.德.波娃(Simone de Beauvoir)出版社: 貓頭鷹原作名: Le Deuxième Sexe译者: 邱瑞鑾出版年: 2013-10页数: 1136装帧 ...
- pip国内镜像永久
一.Windows下永久设置pip镜像使用:# windows系统使用cmd(最好使用管理员权限运行)快速设置 pip install pip -U # 升级pip到最新版本 pip config s ...
- redis-creating server tcp listening socket 127.0.0.1:6379: bind No error
安装redis,出现如下错误: 解决方法: 步骤1:命令行下输入:redis-cli.exe(注意不是redis-server.exe) 然后输入:shutdown exit 退出后输入.\redis ...
- 手机、ESP32单片机、MQTT服务器连接实验
实验目的:通过自己制作的手机APP,连接MQTT服务器,获取ESP32单片机的温湿度信息,控制ESP32单片机上的LED开关. 实验准备: 1.硬件:ESP32开发板(型号不限,esp01s也可以). ...
- Python-闭包(Closure)
一.认识闭包 有时候需要在函数外部得到函数内部的局部变量,但是由于Python作用域的关系,这一点是无法实现的. def f(): n = 22 print(n) #NameError:name 'n ...
- c++文件中,头文件与实现文件该写什么内容
1. 参考https://www.cnblogs.com/fenghuan/p/4794514.html
- kubeadm安装
目录: 环境准备 所有节点安装docker 部署K8s集群 部署Dashboard 安装Harbor私有仓库 安装 master(2C/4G,cpu核心数要求大于2) 192.168.63.100 d ...
- const char* str和const char str[]的区别
首先,字符串常量是存储在flash中的.假设字符串常量在flash中的地址是0x8003fb8. 第一种方式,str等价于str的内存单元的地址,str的内存单元存储着字符串常量的地址 第二种方式,s ...