标签:
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; #define maxn 100050 int d_l[maxn],d_r[maxn],l,n,k,t,x,a,L,R; long long tot_l[maxn],tot_r[maxn],ans,tmp; int main(){ scanf("%d",&t); while(t--){ L=R=0; scanf("%d%d%d",&l,&n,&k); for(int i=0;i<n;i++){ scanf("%d%d",&x,&a); for(int j=0;j<a;j++){ if(x*2<l) d_l[++L]=x; else d_r[++R]=l-x; } } sort(d_l+1,d_l+L+1); sort(d_r+1,d_r+R+1); for(int i=1;i<=L;i++){ if(i<=k) tot_l[i]=d_l[i]; else tot_l[i]=tot_l[i-k]+d_l[i]; } for(int i=1;i<=R;i++){ if(i<=k) tot_r[i]=d_r[i]; else tot_r[i]=tot_r[i-k]+d_r[i]; } ans=(tot_l[L]+tot_r[R])*2; for(int i=0;i<=k;i++){ tmp=(tot_l[L-i]+tot_r[max(0,R-(k-i))])*2; ans=min(ans,l+tmp); } printf("%I64d\n",ans); } return 0; }
标签:
原文地址:http://www.cnblogs.com/names-yc/p/4703051.html