题目链接
思路
已经被大佬Pecco整理出来了一个套路.
知乎文章链接:https://zhuanlan.zhihu.com/p/268630329
代码
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 2e3 + 10;
const int inf = 0x3f3f3f3f;
const int mod = 998244353;
int a[N], b[N], c[N], d[N];
PII f[N * N], dp[N * N];
void solve() {
int n, m;
scanf("%d%d", &n, &m);
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]);
}
int idx = 0;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
if(a[i] <= c[j] && b[i] <= d[j]) {
f[++idx] = {c[j] - a[i] + 1, d[j] - b[i] + 1};
}
}
}
if(!idx) {
puts("0");
return;
}
sort(f + 1, f + 1 + idx);
int tot = 0;
dp[++tot] = f[idx--];
for(int i = idx; i >= 1; i--) {
if(f[i].second >= dp[tot].second) {
dp[++tot] = f[i];
}
}
if(tot == 1) {
printf("%d\n", min(dp[1].first, dp[1].second));
return;
}
int res = min(dp[1].first, dp[tot].second);
for(int i = 2; i <= tot; i++) {
res = min(res, dp[i].first + dp[i - 1].second);
}
printf("%d\n", res);
}
int main() {
// freopen("in.txt", "r", stdin);
// int t; cin >> t; while(t--)
solve();
return 0;
}