2 10 3 2 2 2 8 2 5 1 10 4 1 2 2 8 2 5 1 0 10000
18 26
半环拆成1半,左边的尽可能左走,右边的右走,
现在考虑一下绕圈:
由于2次绕圈可以拆成左右各来回一次,一定不劣,
所以考虑1次绕圈,显然取k个苹果最优
剩下的苹果由dp贪心得到。
dp[i]=dp[i-k] +pos[i] //显然第i次是去pos[i],相当于尽量取远的,剩下的就是dp[i-k]
#include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<functional> #include<iostream> #include<cmath> #include<cctype> #include<ctime> using namespace std; #define For(i,n) for(int i=1;i<=n;i++) #define Fork(i,k,n) for(int i=k;i<=n;i++) #define Rep(i,n) for(int i=0;i<n;i++) #define ForD(i,n) for(int i=n;i;i--) #define RepD(i,n) for(int i=n;i>=0;i--) #define Forp(x) for(int p=pre[x];p;p=next[p]) #define Forpiter(x) for(int &p=iter[x];p;p=next[p]) #define Lson (x<<1) #define Rson ((x<<1)+1) #define MEM(a) memset(a,0,sizeof(a)); #define MEMI(a) memset(a,127,sizeof(a)); #define MEMi(a) memset(a,128,sizeof(a)); #define INF (2139062143) #define F (100000007) #define MAXN (100000+10) typedef long long ll; ll mul(ll a,ll b){return (a*b)%F;} ll add(ll a,ll b){return (a+b)%F;} ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;} void upd(ll &a,ll b){a=(a%F+b%F)%F;} ll dpl[MAXN],dpr[MAXN],L; int n,K,n1,n2; ll al[MAXN],ar[MAXN]; void work(ll *dp,int n,ll *pos) { sort(pos+1,pos+1+n); For(i,n) { if (i<=K) dp[i]=pos[i]; else dp[i]=pos[i]+dp[i-K]; } } int main() { // freopen("D.in","r",stdin); // freopen(".out","w",stdout); int T;cin>>T; while(T--) { cin>>L>>n>>K; n1=n2=0; MEM(al) MEM(ar) MEM(dpl) MEM(dpr) For(i,n) { int xi,ai; scanf("%d%d",&xi,&ai); if (xi<=L/2) while (ai--) al[++n1]=xi; else while (ai--) ar[++n2]=L-xi; } work(dpl,n1,al); work(dpr,n2,ar); ll ans=(dpl[n1]+dpr[n2])*2; Rep(i,K+1) ans=min( 2LL*( dpl[max(n1-i,0)] + dpr[max(n2-(K-i),0)] ) + L , ans ); cout<<ans<<endl; } return 0; }