Description
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
***********************************************************************************************************************************************************
题意:两人在有限的距离内和时间内,走一定的格数,求走的格数权值之和最大
***********************************************************************************************************************************************************
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<string> 3 #include<cstring> 4 #include<cmath> 5 #include<cstdio> 6 #include<queue> 7 #define LL long long 8 using namespace std; 9 LL sum[100001]; 10 LL val[100001],ans; 11 int n,m,i,j,k,p,t; 12 //该函数由右端决定前段, 13 //两人向左向右走,然后走到相差m时, 14 //再决定同时向左还是向右走, 15 //比较求出最大值,这一过程需要枚举 16 LL find(LL*sum,int p) 17 { 18 LL ans=val[p]; 19 int rt,et; 20 for(rt=min(n,p+t);rt>=p;rt--) 21 { 22 int le=max(1,rt-m); 23 le=max(le,p-t); 24 int rest_t=t-max(p-le,rt-p); 25 int l1=max(1,le-rest_t); 26 int r1=min(n,rt+rest_t); 27 ans=max(ans,max(sum[rt]-sum[l1-1],sum[r1]-sum[le-1])); 28 } 29 return ans; 30 } 31 int main() 32 { 33 while(scanf("%d %d",&n,&p)!=EOF) 34 { 35 sum[0]=0; 36 ans=0; 37 for(i=1;i<=n;i++) 38 { 39 scanf("%lld",&val[i]); 40 sum[i]=sum[i-1]+val[i]; 41 } 42 scanf("%d %d",&m,&t); 43 LL ans=find(sum,p); 44 for(i=1;i<=n/2;i++)//注意此处一定一定要转换,不然状态不全 45 swap(val[i],val[n-i+1]); 46 sum[0]=0; 47 for(i=1;i<=n;i++) 48 sum[i]=sum[i-1]+val[i]; 49 ans=max(ans,find(sum,n-p+1)); 50 printf("%lld ",ans); 51 } 52 return 0; 53 }