【BZOJ1271】[BeiJingWc2008]秦腾与教学评估【二分】【神题】

【题目链接】

神题!

利用奇数只有一个的性质,二分位置,算前缀和,如果前缀和是奇数,那么答案一定在前面,否则有可能在右边。

注意二分时候有可能超int,要开LL。

/* Pigonometry */
#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;

typedef long long LL;

const int maxn = 200005;

int n;

struct _data {
	int s, e, d;
} p[maxn];

inline int iread() {
	int f = 1; LL x = 0; char ch = getchar();
	for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;
	for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
	return f * x;
}

inline int calc(int x) {
	int cnt = 0;
	for(int i = 1; i <= n; i++) if(p[i].s <= x)
		cnt += (min(x, p[i].e) - p[i].s) / p[i].d + 1;
	return cnt;
}

int main() {
	for(int T = iread(); T; T--) {
		LL l = 0, r = 0;
		n = iread();
		for(int i = 1; i <= n; i++) {
			int s = iread(), e = iread(), d = iread();
			p[i] = (_data){s, e, d};
			r = max(r, (LL)e);
		}
		LL upb = r;

		while(l <= r) {
			LL mid = l + r >> 1;
			if(calc(mid) & 1) r = mid - 1;
			else l = mid + 1;
		}

		printf(l > upb ? "Poor QIN Teng:(\n" : "%lld %d\n", l, calc(l) - calc(l - 1));
	}
	return 0;
}


你可能感兴趣的:(【BZOJ1271】[BeiJingWc2008]秦腾与教学评估【二分】【神题】)