标签:
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 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 ithhouse. Each of these integers will lie in the range [1, 1000].
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<stdio.h> 2 #include<algorithm> 3 #include<iostream> 4 #include<string.h> 5 #include<queue> 6 #include<math.h> 7 using namespace std; 8 int ans[2000]; 9 int dp[2000]; 10 int main(void) 11 { 12 int i,j,k; 13 scanf("%d",&k); 14 int s; 15 int cnt; 16 for(s=1; s<=k; s++) 17 { 18 19 memset(dp,0,sizeof(dp)); 20 scanf("%d",&cnt); 21 for(j=1; j<=cnt; j++) 22 { 23 scanf("%d",&ans[j]); 24 } int maxx=ans[1]; 25 dp[1]=ans[1]; 26 dp[0]=0; 27 for(i=2; i<=cnt-1; i++) 28 { 29 for(j=0; j<i-1; j++) 30 { 31 dp[i]=max(dp[i],dp[j]+ans[i]); 32 } 33 dp[i]=max(dp[i],dp[i-1]); 34 if(maxx<dp[i]) 35 maxx=dp[i]; 36 } 37 memset(dp,0,sizeof(dp)); 38 dp[cnt]=ans[cnt]; 39 dp[cnt+1]=0; 40 maxx=max(maxx,dp[cnt]); 41 for(i=cnt-1; i>=2; i--) 42 { 43 for(j=cnt+1; j>i+1; j--) 44 { 45 dp[i]=max(dp[i],dp[j]+ans[i]); 46 } 47 dp[i]=max(dp[i],dp[i+1]); 48 maxx=max(maxx,dp[i]); 49 } 50 printf("Case %d: %d\n",s,maxx); 51 } 52 return 0; 53 }
标签:
原文地址:http://www.cnblogs.com/zzuli2sjy/p/5503451.html