比赛 防止颓废的小练习v0.15 评测结果 AAAAAAAAAAAAAAAAAAAA
题目名称 聪明的质监员 最终得分 100
用户昵称 GROWL GOOD BOYส็ 运行时间 0.454 s
代码语言 C++ 内存使用 7.94 MiB
提交时间 2016-10-17 20:08:19
显示代码纯文本
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
 
typedef long long LL;
 
const   int maxn=200010;
 
LL tot1[maxn],tot2[maxn];
int N,M;
 
LL S;
 
struct haha
{
       int l,r;
}ask[maxn];
 
struct node
{
       LL w,v;
 
}A[maxn];
 
LL MIN(LL a,LL b)
{
          if(a<b)return a;
          else return b;
}
LL MAX(LL a,LL b)
{if(a>b)return a;else return b;}
 
LL ABS( LL x)
{if(x<0)return -x;else return x;}
 
LL check(LL W)
{
    
    LL Ans1=0ll,Ans2=0ll;
    //memset(tot1,0,sizeof(tot1));
    //memset(tot2,0,sizeof(tot2));
    tot1[0]=tot2[0]=0;
    for(int i=1;i<=N;i++)
    {
        tot1[i]=tot1[i-1];
        tot2[i]=tot2[i-1];
        if(A[i].w>=W)tot1[i]++,tot2[i]+=A[i].v;    
    }
    for(int i=1;i<=M;i++)
    {       
         Ans1+=(tot1[ask[i].r]-tot1[ask[i].l-1])*(tot2[ask[i].r]-tot2[ask[i].l-1]);
    }  
    //printf("%lld\n",Ans1);
    return Ans1;     
}
 
int main()
{
    freopen("qc.in","r",stdin);
   freopen("qc.out","w",stdout);
    scanf("%d%d%lld",&N,&M,&S);
    LL l=1ll,r=0ll;
    for(int i=1;i<=N;i++)
    {scanf("%lld%lld",&A[i].w,&A[i].v);r=MAX(A[i].w,r);}
    for(int i=1;i<=M;i++)scanf("%d%d",&ask[i].l,&ask[i].r);
    LL Ans=(1ll<<62);
    l=0ll;//r=1000000000ll;
    while(l<=r)
    {
     LL mid=(l+r)>>1;
     LL ha=check(mid);
     Ans=MIN(Ans,ABS(S-ha));
     if(ha>=S)l=mid+1;
     else r=mid-1;
    }
    //Ans=MIN(MIN(Ans,check(l)),check(l+1));
    printf("%lld",Ans);
    
    fclose(stdin);
    fclose(stdout);
    return 0;
}
/*10 10 1475400
23954 25180
18805 2701
17195 5663
7044 13659
8139 30927
19774 25516
7472 4572
5999 6166
1185 13621
10414 26521
2 10
4 7
5 8
1 6
2 7
1 3
2 7
3 4
1 6
1 10*/