Codeforces Round #394 (Div. 2) D. Dasha and Very Difficult Problem

http://codeforces.com/contest/761/problem/D

c[i] = b[i] - a[i],而且b[]和a[]都属于[L, R]

现在给出a[i]原数组和c[i]的相对大小,要确定b[i]

注意c[]数组中没有重复数。

首先对a[]按照c[]排序,这样最小的a[0]一定选择L作为b[0]

往后每次二分L,R,找到能满足b[i]+a[i]>b[i-1] - a[i-1]的b[i]即可,中间注意纪录a[]、b[]数组的原始顺序。

本以为会超时,没想到过了,二分函数要细致考虑。

#include 
using namespace std;
const int MAXN = 100100;
pair p[MAXN];
pair pb[MAXN];
int tb[MAXN];
bool cmp(pair a, pair b) {
	return a.second < b.second;
}
int main() {
	int n, l, r;
	scanf("%d %d %d", &n, &l, &r);
	for(int i = 0; i < n; i++) {
		scanf("%d", &p[i].second);
		p[i].second = -p[i].second;
	}
	for(int i = 0; i < n; i++) {
		scanf("%d", &p[i].first);
		pb[i].first = p[i].first; //a[],b[]都按照c排序
		pb[i].second = i;		//纪录原始顺序
	}
	sort(p, p + n);
	sort(pb, pb + n);
	pb[0].first = l;
	tb[0] = p[0].second + l;
	for(int i = 1; i < n; i++) {
		int ll = l, rr = r;
		while(ll < rr) {
			int mid = (ll+rr)>>1;
			if(mid+p[i].second>tb[i-1]) rr = mid;
			else ll = mid+1;
		}
		//printf("%d ", rr);
		if(ll+p[i].second<=tb[i-1]) {
			puts("-1");
			return 0;
		}
		tb[i] = ll+p[i].second;
		pb[i].first = ll;
	}
	sort(pb, pb+n, cmp);  //还原顺序输出
	for(int i = 0; i < n; i++) {
		printf("%d", pb[i].first);
		if(i !=  n-1) putchar(' ');
		else putchar('\n');
	}
	return 0;
}


你可能感兴趣的:(cf,二分查找)