CF 1408 D. Searchlights

题目链接
思路
已经被大佬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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值