luogu4181 [USACO18JAN]Rental Service (贪心)
我们要出租的话,一定是出租产奶量最少的牛
那我们就看出租多少头牛(其他的卖奶)的时候答案最大就可以了。
(注意N有可能小于R)
1 #include<bits/stdc++.h> 2 #define ll long long 3 #define pa pair<int,int> 4 using namespace std; 5 const int maxn=100010; 6 7 inline ll rd(){ 8 ll x=0;char c=getchar(); 9 while(c<'0'||c>'9') c=getchar(); 10 while(c>='0'&&c<='9') x=x*10+c-'0',c=getchar(); 11 return x; 12 } 13 14 int N,M,R,C[maxn],S[maxn]; 15 pa PQ[maxn]; 16 ll ans,tmp,tmp2; 17 18 inline bool cmp(int a,int b){return a>b;} 19 inline bool cmp2(pa a,pa b){return a>b;} 20 21 int main(){ 22 int i,j,k; 23 N=rd(),M=rd(),R=rd(); 24 for(i=1;i<=N;i++) C[i]=rd(); 25 for(i=1;i<=M;i++) PQ[i].second=rd(),PQ[i].first=rd(); 26 for(i=1;i<=R;i++) S[i]=rd(); 27 sort(C+1,C+N+1,cmp);sort(PQ+1,PQ+M+1,cmp2);sort(S+1,S+R+1,cmp); 28 R=min(N,R);for(i=1;i<=R;i++) tmp2+=S[i]; 29 for(i=0,j=1;i<=N;i++){ 30 for(;j<=M&&PQ[j].second<C[i];j++) 31 tmp+=1LL*PQ[j].first*PQ[j].second,C[i]-=PQ[j].second; 32 tmp+=1LL*PQ[j].first*C[i],PQ[j].second-=C[i]; 33 ans=max(ans,tmp+tmp2);tmp2-=S[N-i]; 34 }printf("%lld\n",ans); 35 }