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. 巩固javaweb第十一天

    巩固内容: HTML <script> 元素 <script>标签用于加载脚本文件,如: JavaScript. <script> 元素在以后的章节中会详细描述. ...

  2. ctfshow WEB入门 信息收集 1-20

    web1 题目:开发注释未及时删除 查看页面源代码即可 web2 题目:js把鼠标右键和f12屏蔽了 方法一: 禁用JavaScript 方法二: url前面加上view-source: web3 题 ...

  3. A Child's History of England.45

    To forgive these unworthy princes was only to afford them breathing-time for new faithlessness. They ...

  4. innodb和myisam对比及索引原理区别

    InnoDB和MyISAM是很多人在使用MySQL时最常用的两个表类型,这两个表类型各有优劣,5.7之后就不一样了 1.事务和外键 InnoDB具有事务,支持4个事务隔离级别,回滚,崩溃修复能力和多版 ...

  5. android 跳到应用市场给软件评分

    1 String packetName = this.getPackageName(); 2 Uri uri = Uri.parse("market://details?id=" ...

  6. 【spring AOP】AspectJProxyFactory

    AspectJProxyFactory,可能大家对这个比较陌生,但是@Aspect这个注解大家应该很熟悉吧,通过这个注解在spring环境中实现aop特别的方便. 而AspectJProxyFacto ...

  7. 使用 IntelliJ IDEA 远程调试 Tomcat

    一.本地 Remote Server 配置 添加一个Remote Server 如下图所示 1. 复制JVM配置参数,第二步有用 2. 填入远程tomcat主机的IP地址和想开启的调试端口(自定义) ...

  8. Map集合的认识和理解

    java.util.Map(k,v)集合* Map的特点:* 1.Map集合是一个双列集合,一个元素包含两个值(一个是key,一个是Value)* 2.Map集合中的元素,key和value的类型可以 ...

  9. arcgis api for js回调函数如何等待同步

    arcgis js开发往往会遇到同步异步的问题,有可能在上一步使用了arcgis js模块回调函数,下一步需要用上一步回调函数的结果,但是因为JavaScript是异步执行的,它并不会等待上一步的回调 ...

  10. Java中的循环结构进阶

    循环结构进阶 学习本章用到的单词 triangle:三角形 circle:圆形 diamond:钻石 password:密码 row:行.排列 二重循环结构 简单的说:二重循环就是一个循环体内又包含另 ...