CodeForces - 1408D Searchlights(思维)

There are nn robbers at coordinates (a1,b1)(a1,b1), (a2,b2)(a2,b2), ..., (an,bn)(an,bn) and mm searchlight at coordinates (c1,d1)(c1,d1), (c2,d2)(c2,d2), ..., (cm,dm)(cm,dm).

In one move you can move each robber to the right (increase aiai of each robber by one) or move each robber up (increase bibi of each robber by one). Note that you should either increase all aiai or all bibi, you can't increase aiai for some points and bibi for some other points.

Searchlight jj can see a robber ii if ai≤cjai≤cj and bi≤djbi≤dj.

A configuration of robbers is safe if no searchlight can see a robber (i.e. if there is no pair i,ji,j such that searchlight jj can see a robber ii).

What is the minimum number of moves you need to perform to reach a safe configuration?

Input

The first line of input contains two integers nn and mm (1≤n,m≤20001≤n,m≤2000): the number of robbers and the number of searchlight.

Each of the next nn lines contains two integers aiai, bibi (0≤ai,bi≤1060≤ai,bi≤106), coordinates of robbers.

Each of the next mm lines contains two integers cici, didi (0≤ci,di≤1060≤ci,di≤106), coordinates of searchlights.

Output

Print one integer: the minimum number of moves you need to perform to reach a safe configuration.

Examples

Input

1 1
0 0
2 3

Output

3

Input

2 3
1 6
6 1
10 1
1 10
7 7

Output

4

Input

1 2
0 0
0 0
0 0

Output

1

Input

7 3
0 8
3 8
2 7
0 10
5 5
7 0
3 5
6 6
3 11
11 5

Output

6

Note

In the first test, you can move each robber to the right three times. After that there will be one robber in the coordinates (3,0)(3,0).

The configuration of the robbers is safe, because the only searchlight can't see the robber, because it is in the coordinates (2,3)(2,3) and 3>23>2.

In the second test, you can move each robber to the right two times and two times up. After that robbers will be in the coordinates (3,8)(3,8), (8,3)(8,3).

It's easy the see that the configuration of the robbers is safe.

It can be proved that you can't reach a safe configuration using no more than 33 moves.

题意:

有 n 个强盗和 m 个监控,坐标为(a, b)的监控可监控的范围为x <= a && y <= b,所有强盗只能集体向上移动或集体向右移动,问最少移动几步使得所有强盗都不在探测范围中。

思路:

把横纵坐标拆开,枚举横向步数,记录每个横向步数可以出去的前提下,最大的纵向步数,然后取个横向步数 + 纵向步数的最小值。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int N = 2e3 + 10;
const int M = 1e6 + 10;

int a[N], b[N], c[N], d[N];
int x[M];

int main() {
    int n, m;
    scanf("%d%d", &n, &m);
    for(int i = 0; i < M; ++i) x[i] = 0;
    for(int i = 1; i <= n; ++i) scanf("%d%d", &a[i], &b[i]);
    for(int i = 1; i <= m; ++i) scanf("%d%d", &c[i], &d[i]);
    for(int i = 1; i <= n; ++i) {
        for(int j = 1; j <= m; ++j) {
            if(a[i] <= c[j] && b[i] <= d[j])
                x[c[j] - a[i]] = max(x[c[j] - a[i]], d[j] - b[i] + 1);
        }
    }
    int maxx = 0, ans = inf;
    for(int i = M - 1; i >= 0; --i) {
        maxx = max(maxx, x[i]);
        ans = min(ans, maxx + i);
    }
    printf("%d\n", ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43871207/article/details/108982988