zoukankan      html  css  js  c++  java
  • 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)] = '
    '; }
    	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 << '
    ';
    			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 << '
    ';
    	return 0;
    }
    
  • 相关阅读:
    Java命名规范
    用U盘安装系统2
    将Sublime Text3添加到右键菜单中
    Git版本控制使用介绍
    Sublime Text各种插件使用方法
    安装Sublime Text 3插件
    Sublime Text修改显示图标
    卸载Visual Studio Code后删除右键Open with Code…
    做最好的自己(Be Your Personal Best)
    Eclipse全屏及插件下载
  • 原文地址:https://www.cnblogs.com/Isaunoya/p/12818920.html
Copyright © 2011-2022 走看看