P5444 [APIO2019]奇怪装置 [数学+贪心]

抄一下别人的证明,不想写了。

证明了这个,就非常好做了。

我们发现 % G 之后你只需要做一下数轴覆盖的问题就够了,注意判断一下 l > r 的情况,这题就做完了。

// by Isaunoya
#include<bits/stdc++.h>
#define int long long
using namespace std;
struct io {
	char buf[1 << 27 | 3], *s;
	int f;
	io() { f = 0, buf[fread(s = buf, 1, 1 << 27, stdin)] = '\n'; }
	io& operator >> (int&x) {
		for(x = f = 0; !isdigit(*s); ++s) f |= *s  == '-';
		while(isdigit(*s)) x = x * 10 + (*s++ ^ 48);
		return x = f ? -x : x, *this;
	}
};

int n, a, b;

const int maxn = 2e6 + 62;
struct Pair {
	int x, y;
} qaq[maxn];

bool cmp(Pair x, Pair y) {
	return x.x < y.x;
}
signed main() {
#ifdef LOCAL
	freopen("testdata.in", "r", stdin);
#endif
	io in;
	in >> n >> a >> b;
	int g = a / __gcd(a, b + 1) * b;
	int cnt = 0;
	while(n --) {
		int l, r;
		in >> l >> r;
		if(r - l + 1 >= g) {
			cout << g << '\n';
			return 0;
		} else {
			l %= g, r %= g;
			if(l > r) {
				l ^= r ^= l ^= r;
				qaq[++ cnt] = { 0, l };
				qaq[++ cnt] = { r, g - 1 };
			} else {
				qaq[++ cnt] = { l, r };
			}
		}
	}
	sort(qaq + 1, qaq + cnt + 1, cmp);
	qaq[++ cnt] = { g + 1, g + 114514 };
	int ans = 0, nowl, nowr;
	nowl = qaq[1].x, nowr = qaq[1].y;
	for(int i = 2 ; i <= cnt ; i ++) {
		if(nowr < qaq[i].x) {
			ans += nowr - nowl + 1;
			nowl = qaq[i].x;
			nowr = qaq[i].y;
		} else {
			nowr = max(nowr, qaq[i].y);
		}
	}
	cout << ans << '\n';
	return 0;
}
posted @ 2020-05-02 18:03  _Isaunoya  阅读(214)  评论(0编辑  收藏  举报