There are n cities(1, 2, ... ,n) forming a line on the wonderland. city i and city i+1 are adjacent and their distance is 1. Each city has many gold coins. Now, Alice and her friend Bob make a team to go treasure hunting. They starts at city p, and they want to get as many gold coins as possible in T days. Each day Alice and Bob can move to adjacent city or just stay at the place, and their action is independent. While as a team, their max distance can't exceed M.
Input
The input contains multiple cases.
The first line of each case are two integers n, p as above.
The following line contain n interger,"v1 v2 ... vn" indicate the gold coins in city i.
The next line is M, T.
(1<=n<=100000, 1<=p<=n, 0<=vi<=100000, 0<=M<=100000, 0<=T<=100000)
Output
Output the how many gold coins they can collect at most.
Sample Input
6 3 1 2 3 3 5 4 2 1
Sample Output
8
Hint
At day 1: Alice move to city 2, Bob move to city 4.
They can always get the gold coins of the starting city, even if T=0
题意:有n堆金币,两个人一开始在p位置,每个人每天可以移动一个位置,然后两个人的距离不能超过M,问在T天内最多得到的金币是多少。
思路:首先先把两个人张开,能张多大张多大,然后在这种情况下,考虑遍历两边的情况,假设左边走了a步,右边走了b步,那么需要的天数就是2*(a+b)-max(a,b)。然后考虑特殊情况。
AC代码如下:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
long long num[100010],sum[100010];
int n;
long long solve(int l,int r)
{ if(l<=0)
l=1;
if(r>=n)
r=n;
return sum[r]-sum[l-1];
}
long long solve2(int l,int r,int t)
{ long long ans;
int i;
if(t==0)
{ return solve(l,r);
}
if(l<=1)
{ r+=t;
return solve(1,r);
}
if(r>=n)
{ l-=t;
return solve(l,n);
}
else
{ ans=0;
for(i=0;i<=t/2;i++)
ans=max(ans,solve(l-i,r+t-i*2));
for(i=0;i<=t/2;i++)
ans=max(ans,solve(l-t+i*2,r+i));
return ans;
}
}
int main()
{ int i,j,k,l,r,m,t,a,b;
long long ans;
sum[0]=0;
while(~scanf("%d%d",&n,&k))
{ l=k;r=k;
for(i=1;i<=n;i++)
{ scanf("%lld",&num[i]);
sum[i]=sum[i-1]+num[i];
}
scanf("%d%d",&m,&t);
if(t<=m/2)
{ l-=t;
r+=t;
printf("%lld\n",solve(l,r));
continue;
}
l-=m/2;
r+=m/2;
t-=m/2;
if(t==0)
{ printf("%lld\n",solve(l,r));
continue;
}
if(m%2==0)
ans=solve2(l,r,t);
else
ans=max(solve2(l-1,r,t-1),solve2(l,r+1,t-1));
printf("%lld\n",ans);
}
}