UVa 1511 - Soju (思维)

来源:互联网 发布:淘宝一键好复制怎么用 编辑:程序博客网 时间:2024/04/30 04:23

题意

从两个部分的点中找出最近的距离。

思路

题目的关键是

Note that all points of I have smaller -coordinates than any point of P.」这句话。

一开始想到最近点对去了。。

化简一下公式。

Dis(P1,P2)=|x1x2|+|y1y2|

因为题目保证第一部分的点的横坐标一定小于第二部分。

=x2x1+|y1y2|

然后分类讨论。当y1>=y2,=(x2y2)+(y1x1)

我们可以想到,如果我们把全部的点混合起来,按照y从小到大排序。
如果当前的点是part1,那么y1x1可以立刻算出来,而x2y2可以通过维护前面的part2点的最小值的得来。

所以,我们一边维护x2y2的最小值,一边维护ans。

所以当我们过一遍数组之后,就有了一个暂时的答案ans。

为什么是暂时的?
因为还要考虑y2>y1的情况。

同理,当y2>y1 时, =(x2+y2)(x1+y1).
可以维护x1+y1的最大值来得到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
原创粉丝点击