【ybtoj】【二分】【例题2】防具布置

【例题2】防具布置


=link=

传送门
题目


=解题思路=

S i S_i Si是前 i i i位置的防具个数和

  1. 只有一个位置 x x x有破绽,那么就只有 x x x这个位置是奇数防具, S 1 → S x − 1 S_1\rightarrow S_{x-1} S1Sx1都是偶数, S x S_x Sx位置及以后的位置都是奇数
    二分查找第一个出现奇数的S
  2. S 2147483647 S_{2147483647} S2147483647是偶数时,这组防具没有破绽

=Code=

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;

const int maxn = 2147483647;
struct DT{
    
    
	long long s, e, d;
}a[200100];
long long n, ans, l, r, T, mid, t;

bool cmp(const DT& k, const DT& l) {
    
     return (k.s < l.s); };

long long check(long long x) {
    
    
	long long ans = 0;
	for (int i = 1; i <= n; i++)
		if (a[i].s <= x)
			ans += (min(a[i].e, x) - a[i].s) / a[i].d + 1;//计算防具个数
		else break;//先排序好起始点(a[i].s),可以快一点
	return ans;
}

bool print() {
    
    //判断有没有破绽
	if (check(maxn) % 2 == 0) {
    
    
		printf ("There's no weakness.\n");
		return 1;
	}else return 0;
}

int main() {
    
    
	scanf("%lld", &T);
	while (T--) {
    
    
		scanf("%lld", &n);
		for (int i = 1; i <= n; i++)
			scanf("%lld%lld%lld", &a[i].s, &a[i].e, &a[i].d);
		sort(a + 1, a + 1 + n, cmp);
		if (print())
			continue;
		l = 0, r =  maxn;
		while (l <= r) {
    
    
			mid = (l + r) / 2;
			t = check(mid);
			if (t % 2) {
    
    
				ans = mid;
				r = mid - 1;
			}else l = mid + 1;
		}
		printf("%lld %lld\n", ans, check(ans) - check(ans - 1));
	}
} 

猜你喜欢

转载自blog.csdn.net/qq_39940018/article/details/112018616