lightoj1217_简单dp

题目链接:http://lightoj.com/volume_showproblem.php?problem=1217

1217 - Neighbor House (II)
Time Limit: 2 second(s) Memory Limit: 32 MB

A soap company wants to advertise their product in a local area. In this area, there are n houses and the houses are placed in circular fashion, such that house 1 has two neighbors: house 2 and n. House 5 has two neighbors: house 4 and 6. House n has two neighbors, house n-1 and 1.

Now the soap company has an estimation of the number of soaps they can sell on each house. But for their advertising policy, if they sell soaps to a house, they can't sell soaps to its two neighboring houses. No your task is to find the maximum number of estimated soaps they can sell in that area.

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case starts with a line containing an integer n (2 ≤ n ≤ 1000). The next line contains n space separated integers, where the ith integer denotes the estimated number of soaps that can be sold to the ith house. Each of these integers will lie in the range [1, 1000].

Output

For each case, print the case number and the maximum number of estimated soaps that can be sold in that area.

Sample Input

Output for Sample Input

3

2

10 100

3

10 2 11

4

8 9 2 8

Case 1: 100

Case 2: 11

Case 3: 17

 

 1 #include <algorithm>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <cstdio>
 6 #include <vector>
 7 #include <ctime>
 8 #include <queue>
 9 #include <list>
10 #include <set>
11 #include <map>
12 using namespace std;
13 #define INF 0x3f3f3f3f
14 typedef long long LL;
15 
16 int a[1005], dp[1005][2];
17 int main()
18 {
19     int t, n;
20     scanf("%d", &t);
21     for(int ca = 1; ca <= t; ca++)
22     {
23         scanf("%d", &n);
24         int res = 0;
25         for(int i = 1; i <= n; i++){
26             scanf("%d", &a[i]);
27             res = max(res, a[i]);
28         }
29         memset(dp, 0, sizeof(dp));
30         dp[2][0] = a[1];//第一栋楼卖
31         for(int i = 3; i <= n; i++)
32         {
33             dp[i][0] = max(dp[i][0], max(dp[i-1][1], dp[i-1][0]));
34             if(i == n)
35                 break;
36             dp[i][1] = max(dp[i][1], dp[i-1][0]+a[i]);
37         }
38         res = max(res, dp[n][0]);
39         memset(dp, 0, sizeof(dp));
40         dp[2][1] = a[2];//第一栋楼不卖
41         for(int i = 3; i <= n; i++)
42         {
43             dp[i][0] = max(dp[i][0], max(dp[i-1][1], dp[i-1][0]));
44             dp[i][1] = max(dp[i][1], dp[i-1][0]+a[i]);
45         }
46         res = max(res, max(dp[n][0], dp[n][1]));
47         printf("Case %d: %d\n", ca, res);
48     }
49     return 0;
50 }

 

posted @ 2016-10-09 21:29  海无泪  阅读(165)  评论(0编辑  收藏  举报