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 ithhouse. 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


PROBLEM SETTER: JANE ALAM JAN
思路:dp;
左一遍dp,右一边dp然后取最大,状态转移方程dp[i]=max(max(dp[i],max(dp[j])+ans[i]),dp[i-1])(j<=i-2);
dp[i]表示前i个点的取法中的最大值,当在第i个点有两种决策,取或不去。
 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 }

1217 - Neighbor House (II)的更多相关文章

  1. Game of Life I & II

    According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellul ...

  2. [LintCode] House Robber II 打家劫舍之二

    After robbing those houses on that street, the thief has found himself a new place for his thievery ...

  3. 198. House Robber,213. House Robber II

    198. House Robber Total Accepted: 45873 Total Submissions: 142855 Difficulty: Easy You are a profess ...

  4. [LeetCode]House Robber II (二次dp)

    213. House Robber II     Total Accepted: 24216 Total Submissions: 80632 Difficulty: Medium Note: Thi ...

  5. LeetCode之“动态规划”:House Robber && House Robber II

    House Robber题目链接 House Robber II题目链接 1. House Robber 题目要求: You are a professional robber planning to ...

  6. leetcode日记 HouseRobber I II

    House Robber I You are a professional robber planning to rob houses along a street. Each house has a ...

  7. Number of Islands I & II

    Given a boolean 2D matrix, find the number of islands. Notice 0 is represented as the sea, 1 is repr ...

  8. House Robber I & II & III

    House Robber You are a professional robber planning to rob houses along a street. Each house has a c ...

  9. 【LeetCode】213. House Robber II

    House Robber II Note: This is an extension of House Robber. After robbing those houses on that stree ...

随机推荐

  1. adult

    adult是adolescere (grow up)的过去分词. egg - embryo [胚胎] - foetus [就要出生的胎儿] - toddler [刚会走路] - adolescent ...

  2. Vue相关,Vue JSX

    JSX简介 JSX是一种Javascript的语法扩展,JSX = Javascript + XML,即在Javascript里面写XML,因为JSX的这个特性,所以他即具备了Javascript的灵 ...

  3. 【♪♪♪】网易云音乐mp3真实地址

    参考别人的博客,得到下面的地址,填上ID号即可,后缀的[.mp3]不用输入 http://music.163.com/song/media/outer/url?id= 例如 最终,合并地址为 http ...

  4. vim中搜索指定单词(不加前后缀)

    \< : 搜索内容作为单词开头 \> : 搜索内容作为单词结尾 一起用即为将搜索内容指定为whole word e.g. : word_suffix word 如果用/word来搜索则两个 ...

  5. Android 开源框架Universal-Image-Loader加载https图片

    解决方案就是 需要 android https HttpsURLConnection 这个类忽略证书 1,找到 Universal-Image-Loader的library依赖包下面com.nostr ...

  6. android 防止R被混淆,R类反射混淆,找不到资源ID

    在Proguard.cfg中添加 -keep class **.R$* { *;   }

  7. c3p0的使用步骤

    //1.导入c3p0的连个包,和mysql的驱动包//2.配置c3p0.xml的配置文件 <c3p0-config> <!-- 使用默认的配置读取连接池对象 --> <d ...

  8. 淘宝网购物车jquery源码和网易新用户注册页面表单验证的练习

    淘宝网购物车源码: <html lang="en"> <head> <meta charset="UTF-8"> <t ...

  9. [BUUCTF]REVERSE——[MRCTF2020]Transform

    [MRCTF2020]Transform 附件 步骤: 例行检查,64位程序,无壳 64位ida载入,找到关键函数 一开始让我们输入一个长度为33位的字符串,之后使用数组dword_40F040打乱了 ...

  10. Sql Server、Oracle、MySQL 日期格式化函数处理

    目录 Sql Server Oracle MySql Sql Server CONVERT ( '取数长度' , '时间' , '类型') 查询对应时间: 2021-03-17T19:18:18.00 ...