zoj3628 Treasure Hunt III (dp)

Treasure Hunt III

Time Limit: 2 Seconds      Memory Limit: 65536 KB

Alice is exploring the wonderland once again and she finds a strange house. In the house, there are n rooms which form a circle so that each room is connected to its two neighbour room. For exmaple, if Alice is in room 2, then she can go into room 1 or room 3, and if she is in room 1, she can go into room 2 or room n.

In each room, there may exists some gold coins which is very valuable for Alice's wonderland trip. Every time when alice move into a room, she can choose to take the gold in the room. However, at the same time, the gold in the 2 neighbour room will disappear. For example, if Alice took the gold in room 2, and there are still gold in room 1 or room 3, then all these gold will disappear.

It costs Alice 1 second to move from one room to another neighbour room. Taking gold doesn't cost any time. Since the house is going to collapse, Alice must leave the house in Tseconds. At first Alice is in room S, and she can leave the house from any room.

Please help Alice calculate the maximum gold she can collect in T seconds.

Input

The input contains multiple test cases ( no more than 250 test cases ).
In each test case, first there are 3 integers, nTS ( 1 <= nTS <=50000 ). The index of Rooms starts from 1, and S is in the range of 1 to n.
Then there is a line containing n non-negtive integers with the ith integer indicating the number of gold in the ith room. All these integers are no more than 10^9.

Output

For each case, output one line with an integer which is the maximum gold Alice can collect.

Sample Input

4 1 2
3 4 5 6

Sample Output

5

Hint

In the sample, Alice has just one second, and she was in room 2 at second 0, if she took the 4 gold in room 2, she cannot get any gold in the next second, but if she just move to room 3 at second 1, then she can take the 5 gold and leave the house.

 

题意:在一个环形房子里,每个房间都有一定的财富值,取走一个房间的财富,相邻的两个房间就会变成空的,告诉你起始的房间,总时间,求获得的价值最大。

分析:分两个方向顺时针方向和逆时针方向dp,d[i][0] 表示第 i 个位置不取, d[i][0] 表示第 i 个位置取了,于是有

d[i][0]=max(d[i-1][0],d[i-1][1])  d[i][1]=d[i-1][0]+a[i];

枚举初始位置的两个状态。

 

View Code
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #define MAXN 50010
 5 
 6 using namespace std;
 7 
 8 long long d1[MAXN][2],d2[MAXN][2],a[MAXN];
 9 
10 long long max(long long A,long long B)
11 {
12     return A>B?A:B;
13 }
14 
15 int main()
16 {
17     int n,t,s,i,j;
18 #ifndef ONLINE_JUDGE
19     freopen("in.txt","r",stdin);
20 #endif
21     while(scanf("%d%d%d",&n,&t,&s)==3)
22     {
23         s--;
24         for(i=0; i<n; i++) scanf("%lld",&a[i]);
25 
26         long long ans1;
27         memset(d1,0,sizeof(d1));
28         memset(d2,0,sizeof(d2));
29         d1[0][1]=a[s];
30         d1[0][0]=d2[0][0]=d2[0][1]=0;
31         d1[1][0]=d1[1][1]=a[s];
32         for(i=2; i<=n-2; i++)
33         {
34             d1[i][0]=max(d1[i-1][0],d1[i-1][1]);
35             d1[i][1]=d1[i-1][0]+a[(s+i)%n];
36         }
37         d2[1][0]=d2[1][1]=0;
38         for(i=2; i<=n-2; i++)
39         {
40             d2[i][0]=max(d2[i-1][0],d2[i-1][1]);
41             d2[i][1]=d2[i-1][0]+a[(n+s-i)%n];
42         }
43         d1[i][0]=d1[i][1]=max(d1[i-1][0],d1[i-1][1]);
44         d2[i][0]=d2[i][1]=max(d2[i-1][0],d2[i-1][1]);
45         if(t>n-1) t=n-1;
46         ans1=max(max(d1[t][0],d1[t][1]),max(d2[t][0],d2[t][1]));
47         for(i=0; i<n; i++)
48         {
49             if(t-i>=0&&(t-i)%2==0)
50             {
51                 j=(t-i)/2;
52                 ans1=max(ans1,max(d1[i][0],d1[i][1])+max(d2[j][0],d2[j][1]));
53                 ans1=max(ans1,max(d2[i][0],d2[i][1])+max(d1[j][0],d1[j][1]));
54             }
55 
56         }
57 
58         long long  ans2;
59         memset(d1,0,sizeof(d1));
60         memset(d2,0,sizeof(d2));
61         d1[0][0]=d1[0][1]=d2[0][0]=d2[0][1]=0;
62         for(i=1; i<=n-1; i++)
63         {
64             d1[i][0]=max(d1[i-1][0],d1[i-1][1]);
65             d1[i][1]=d1[i-1][0]+a[(s+i)%n];
66         }
67         for(i=1; i<=n-1; i++)
68         {
69             d2[i][0]=max(d2[i-1][0],d2[i-1][1]);
70             d2[i][1]=d2[i-1][0]+a[(n+s-i)%n];
71         }
72         if(t>n-1) t=n-1;
73         ans2=max(max(d1[t][0],d1[t][1]),max(d2[t][0],d2[t][1]));
74         for(i=0; i<n; i++)
75         {
76             if(t-i>=0&&(t-i)%2==0) //这里因为 t-i>0 WA 不知道多少次了 
77             {
78                 j=(t-i)/2;
79                 ans2=max(ans2,max(d1[i][0],d1[i][1])+max(d2[j][0],d2[j][1]));
80                 ans2=max(ans2,max(d2[i][0],d2[i][1])+max(d1[j][0],d1[j][1]));
81             }
82 
83         }
84         printf("%lld\n",max(ans1,ans2));
85     }
86     return 0;
87 }
posted @ 2012-08-04 23:15  mtry  阅读(310)  评论(0)    收藏  举报