[OJ#39]左手右手
2024-09-03 15:25:11
[OJ#39]左手右手
试题描述
有 n 个人,每个人左右手上各写着一个整数。对于编号为 a 的人和编号为 b 的人, a 对 b 的好感度等于 a 左手上写的数字乘 b 右手上写的数字,a 和 b 的好感度差异等于 a 对 b 的好感度与 b 对 a 的好感度之差的绝对值。
现在,你要从这 n 个人中找出两个人使得他们的好感度差异最大。
输入
第一行一个整数 n。
接下来 n 行,每行两个整数 ai,bi,分别表示第 i 个人左右手上写的数字。
输出
输出一个整数表示好感度差异的最大值。
输入示例
- -
-
输出示例
数据规模及约定
2≤n≤105,−109≤ai,bi≤109
题解
首先我们可以发现两个人 i, j 的好感度差异就是两个向量 (ai, bi) 和 (aj, bj) 的叉积的绝对值。那么要让这个值最大,就是要选择两个向量使得它们围成的三角形面积最大。
我们不妨先枚举其中一个向量 x,可以发现与它叉积绝对值最大的向量 y 一定在凸包上。我们做平行于向量 x 的直线,显然如果向量 y 的终点在直线上,这条直线离原点越远越好,所以一定是在凸包上的。
所以我们先处理出凸包,然后把所有向量按照极角排序,用旋转卡壳的方法去做就可以 O(n) 了。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <cmath>
using namespace std; int read() {
int x = 0, f = 1; char c = getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
return x * f;
} #define maxn 100010
#define LL long long struct Vec {
LL x, y;
Vec() {}
Vec(LL _, LL __): x(_), y(__) {} Vec operator - (const Vec& t) const { return Vec(x - t.x, y - t.y); }
LL operator ^ (const Vec& t) const { return x * t.y - y * t.x; } bool operator < (const Vec& t) const { return x != t.x ? x < t.x : y < t.y; }
} ps[maxn], poly[maxn];
int cntp, S[maxn], top; bool cmp(Vec a, Vec b) {
return atan2((double)a.y, (double)a.x) > atan2((double)b.y, (double)b.x);
} #define nxt(x) (x + 1) % cntp
#define pre(x) (x + cntp - 1) % cntp int main() {
int n = read();
for(int i = 1; i <= n; i++) {
int a = read(), b = read();
ps[i] = Vec(a, b);
} sort(ps + 1, ps + n + 1);
S[top = 1] = 1;
for(int i = 2; i <= n; i++) {
while(top > 1 && (ps[S[top]] - ps[S[top-1]] ^ ps[i] - ps[S[top]]) >= 0) top--;
S[++top] = i;
}
for(int i = 1; i <= top; i++) poly[cntp++] = ps[S[i]];
int upend = cntp - 1;
S[top = 1] = 1;
for(int i = 2; i <= n; i++) {
while(top > 1 && (ps[S[top]] - ps[S[top-1]] ^ ps[i] - ps[S[top]]) <= 0) top--;
S[++top] = i;
}
for(int i = top - 1; i > 1; i--) poly[cntp++] = ps[S[i]]; sort(ps + 1, ps + n + 1, cmp);
int l = 0, r = upend; LL ans = 0;
for(int i = 1; i <= n; i++) {
while(!((ps[i] ^ poly[nxt(l)] - poly[l]) > 0 ^ (ps[i] ^ poly[l] - poly[pre(l)]) > 0)) l = nxt(l);
while(!((ps[i] ^ poly[nxt(r)] - poly[r]) > 0 ^ (ps[i] ^ poly[r] - poly[pre(r)]) > 0)) r = nxt(r);
ans = max(ans, max(abs(ps[i] ^ poly[l]), abs(ps[i] ^ poly[r])));
}
printf("%lld\n", ans); return 0;
}
最新文章
- RxJava基本流程和lift源码分析
- 通过IP连接网上打印机(转载)
- PHP output_buffering 你了解多少
- EBS报表输出文件格式控制
- 有关TCP和UDP 粘包 消息保护边界
- Tomcat架构(二)
- CodeForces 478B 第六周比赛B题
- Fire Net--hdu1045
- jQuery如何实现点击页面获得当前点击元素
- mysql 通过测试&#39;for update&#39;,深入了解行锁、表锁、索引
- [20171031]rman xxx Failure.txt
- 2015年蓝桥杯省赛A组c++第3题
- 【详解】ThreadPoolExecutor源码阅读(一)
- emouse思·;睿&mdash;评论与观点整理之四
- Web Service 简介
- 第一個shell腳本
- Python开发一个多并发的FTP SERVER
- Maven 入门篇(下)
- oracle以逗号分隔查询结果列表
- SSM+Redis+Shiro+Maven框架搭建及集成应用