UVa 1511 - Soju (思维)
来源:互联网 发布:淘宝一键好复制怎么用 编辑:程序博客网 时间:2024/04/30 04:23
题意
从两个部分的点中找出最近的距离。
思路
题目的关键是
「Note that all points of I have smaller -coordinates than any point of P.」这句话。
一开始想到最近点对去了。。
化简一下公式。
因为题目保证第一部分的点的横坐标一定小于第二部分。
然后分类讨论。当
我们可以想到,如果我们把全部的点混合起来,按照y从小到大排序。
如果当前的点是part1,那么
所以,我们一边维护
所以当我们过一遍数组之后,就有了一个暂时的答案ans。
为什么是暂时的?
因为还要考虑
同理,当
可以维护
重新扫描一遍数组。
两部分合起来就是最终的答案。
感觉这种题目平时无聊的时候做做也挺有意思的。。
代码
#include <stack>#include <stdio.h>#include <list>#include <cassert>#include <set>#include <iostream>#include <string>#include <vector>#include <queue>#include <functional>#include <cstring>#include <algorithm>#include <cctype>#include <string>#include <map>#include <cmath>using namespace std;#define LL long long#define ULL unsigned long long#define SZ(x) (int)x.size()#define Lowbit(x) ((x) & (-x))#define MP(a, b) make_pair(a, b)#define MS(arr, num) memset(arr, num, sizeof(arr))#define PB push_back#define X first#define Y second#define ROP freopen("input.txt", "r", stdin);#define MID(a, b) (a + ((b - a) >> 1))#define LC rt << 1, l, mid#define RC rt << 1|1, mid + 1, r#define LRT rt << 1#define RRT rt << 1|1const double PI = acos(-1.0);const int INF = 0x3f3f3f3f;const double eps = 1e-8;const int MAXN = 1e5 + 10;const int MOD = 9901;const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };int cases = 0;typedef pair<int, int> pii;struct POINT{ int x, y, type; bool operator < (const POINT &a) const { return y < a.y; }};vector<POINT> p;int main(){ //ROP; int T; scanf("%d", &T); while (T--) { p.clear(); int n1, n2; scanf("%d", &n1); for (int i = 0; i < n1; i++) { int x, y; scanf("%d%d", &x, &y); p.PB((POINT){x, y, 0}); } scanf("%d", &n2); for (int i = 0; i < n2; i++) { int x, y; scanf("%d%d", &x, &y); p.PB((POINT){x, y, 1}); } sort(p.begin(), p.end()); int tmpMin = INF, ans = INF; for (int i = 0; i < SZ(p); i++) { if (!p[i].type) ans = min(ans, tmpMin+p[i].y-p[i].x); else tmpMin = min(tmpMin, p[i].x-p[i].y); } int tmpMax = -INF; for (int i = 0; i < SZ(p); i++) { if (p[i].type) ans = min(ans, p[i].x+p[i].y-tmpMax); else tmpMax = max(tmpMax, p[i].x+p[i].y); } printf("%d\n", ans); } return 0;}
0 0
- UVa 1511 - Soju (思维)
- uva 1511 - Soju(贪心)
- UVA 1511 Soju(贪心)
- Uva 1511 Soju
- UVa 1511 - Soju
- Soju uvalive5845
- uva 1388 - Graveyard(思维)
- UVA 10881 思维
- UVA 10970 (思维,贪心)
- UVA 3708 Graveyard(思维)
- UVALive - 5848 Soju 贪心
- UVALive - 5848 Soju
- UVA 11489 博弈思维题
- uva 10970 Big Chocolate 思维
- UVA 6855 Banks 思维题
- UVa -- 11300 Spreading the Wealth 思维好题啊!【思维】
- uva 253 - Cube painting 思维问题
- uva 11300 - Spreading the Wealth(思维)
- ngClass
- ROS之mocap_optitrack
- 如何把列表转内容转至拼音首字母归档排序
- ExtJS初探:在项目中使用ExtJS
- 【.Net码农】WPF界面设计—撸大师
- UVa 1511 - Soju (思维)
- C++中const用法总结
- C++文件操作详解(ifstream、ofstream、fstream)
- 论C语言的malloc,calloc,new,realloc,alloca的机制和区别
- ubuntu配置android开发所需环境变量
- 【.Net码农】WPF界面—仿360安全卫士9.0界面
- 尹成老师,关于链表的一些操作(C)
- BPMN使用工具
- bootstrap分页