#include<cstdio>
#define max(a,b) (a>b?a:b)
long long abs(long long x)
{
if(x>0)
return x;
else
return (-1)*x;
}
struct node
{
int weight;
int value;
}a[200010],total[200010];
struct note
{
int start;
int end;
}b[200010];
int n,m;
long long w;
long long ans;
int calc(int x)
{
long long sum=0;
for(int i=1;i<=n;i++)
if(a[i].weight >= x)
{
total[i].weight = total[i-1].weight + 1;
total[i].value = total[i-1].value + a[i].value;
}
else
{
total[i].weight = total[i-1].weight;
total[i].value = total[i-1].value ;
}
for(int i=1;i<=m;i++)
sum +=((total[ b[i].end ].weight - total[ b[i].start-1 ].weight)
*(total[ b[i].end ].value - total[ b[i].start-1 ].value));
if(abs(sum - w) < ans)
ans = abs(sum - w);
if(sum > w)
return -1;
else
return 1;
}
int main()
{
freopen("qc.in","r",stdin);
freopen("qc.out","w",stdout);
ans=210000000;
ans=ans*10000;
scanf("%d %d %I64d",&n,&m,&w);
int maxw=0;
for(int i=1;i<=n;i++)
{
scanf("%d %d",&a[i].weight,&a[i].value);
maxw=max(maxw,a[i].weight);
}
for(int i=1;i<=m;i++)
scanf("%d %d",&b[i].start,&b[i].end);
maxw = 1000000;
int begin = 0,end = maxw + 1;
while(end - begin > 1)
{
int mid= (end + begin)/2;
if(calc(mid)==-1)
begin = mid;
else
end = mid;
}
calc(begin);
calc(begin - 1);
calc(begin + 1);
calc(end);
calc(end + 1);
calc(end - 1);
printf("%I64d",ans);
return 0;
}
NOIP 2011 Day2 Problem2
最新推荐文章于 2018-09-08 11:42:00 发布