Treasure Hunt II - ZOJ 3627 想法题

来源:互联网 发布:小清新调色思路 知乎 编辑:程序博客网 时间:2024/05/16 08:36

Treasure Hunt II

Time Limit: 2 Seconds      Memory Limit: 65536 KB

There are n cities(12, ... ,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 np 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<=1000001<=p<=n0<=vi<=1000000<=M<=1000000<=T<=100000)

Output

Output the how many gold coins they can collect at most.

Sample Input

6 31 2 3 3 5 42 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);  }}



0 0
原创粉丝点击