codevs 1138

题意:略

解法:看代码(这是c++)

#include<stdio.h>
#include<algorithm>
#define L long long
#define N 200010
using namespace std;
int w[N],v[N],l[N],r[N],n,m;
L s1[N],s2[N],ans=1ll<<62,s;
bool ok(int x,L sum=0){
	for(int i=1;i<=n;++i)
		s1[i]=s1[i-1]+(w[i]>=x),
		s2[i]=s2[i-1]+(w[i]>=x)*v[i];
	for(int i=1;i<=m;++i)
		sum+=(s2[r[i]]-s2[l[i]-1])*(s1[r[i]]-s1[l[i]-1]);
	ans=min(ans,abs(sum-s));
	if(sum<s) return 1;
	else return 0;
}
int main(){
	scanf("%d%d%lld",&n,&m,&s);
	for(int i=1;i<=n;++i) scanf("%d%d",w+i,v+i);
	for(int i=1;i<=m;++i) scanf("%d%d",l+i,r+i);
	int _l=0,_r=1<<30,_m;
	while(_l<=_r){
		_m=(_l+_r)>>1;
		if(ok(_m)) _r=_m-1;
		else _l=_m+1;
	}
	printf("%lld\n",ans);
}


posted @ 2016-09-14 14:03  扩展的灰(Extended_Ash)  阅读(96)  评论(0编辑  收藏  举报