比赛 防止颓废的小练习v0.15 评测结果 AAAAAAAAAAAAAAAAAAAA
题目名称 聪明的质监员 最终得分 100
用户昵称 rewine 运行时间 0.583 s
代码语言 C++ 内存使用 3.36 MiB
提交时间 2016-10-17 16:43:18
显示代码纯文本
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;

const int maxn = 200000 + 10;
const long long inf = 1000000;
struct line{
	int w,v;
}e[maxn];

int n,m,maxw,a;
int l[maxn],r[maxn];
long long s,min_ans = inf * inf;


long long calc(int num){
	long long add_w[maxn];
	int add_v[maxn];
	memset(add_w,0,sizeof(add_w));
	memset(add_v,0,sizeof(add_v));
	for (int i = 1;i <= n;++i){
		if (e[i].w >= num){
			add_w[i] = add_w[i - 1] + 1;
			add_v[i] = add_v[i - 1] + e[i].v;
		}
		else{
			add_w[i] = add_w[i - 1];
			add_v[i] = add_v[i - 1];
		}
	}
	long long toto = 0,tott = 0,ans = 0;
	for (int i = 1;i <= m;++i){
		toto = add_v[r[i]] - add_v[l[i] - 1]; 
		tott = add_w[r[i]] - add_w[l[i] - 1];
		ans += toto * tott;
	}
	return ans;
}

int main() {
	freopen("qc.in","r",stdin);
    freopen("qc.out","w",stdout);	
	scanf("%d%d%lld",&n,&m,&s);
	for (int i = 1;i <= n;++i) {
		scanf("%d%d",&e[i].w,&e[i].v);
		maxw = max(e[i].w,maxw);
	}
	for (int i = 1;i <= m;++i) {
		scanf("%d%d",&l[i],&r[i]);
	}
	int left = 0,right = maxw;
	while (left < right){
		long long mid = (left + right) >> 1;
		long long get = calc(mid + 1);
		min_ans = min(abs(get - s),min_ans);
		if (get <= s){
			right = mid;
		}
		else{
			left = mid + 1;
		}
	}
	printf("%lld",min_ans);
	return 0;
}