UVa 1511 - Soju (思维)

题意

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

思路

题目的关键是

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|1
const 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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值