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 | 
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)的更多相关文章
- Game of Life I & II
		According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellul ... 
- [LintCode] House Robber II 打家劫舍之二
		After robbing those houses on that street, the thief has found himself a new place for his thievery ... 
- 198. House Robber,213. House Robber II
		198. House Robber Total Accepted: 45873 Total Submissions: 142855 Difficulty: Easy You are a profess ... 
- [LeetCode]House Robber II (二次dp)
		213. House Robber II Total Accepted: 24216 Total Submissions: 80632 Difficulty: Medium Note: Thi ... 
- LeetCode之“动态规划”:House Robber && House Robber II
		House Robber题目链接 House Robber II题目链接 1. House Robber 题目要求: You are a professional robber planning to ... 
- leetcode日记  HouseRobber I II
		House Robber I You are a professional robber planning to rob houses along a street. Each house has a ... 
- 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 ... 
- House Robber I & II & III
		House Robber You are a professional robber planning to rob houses along a street. Each house has a c ... 
- 【LeetCode】213. House Robber II
		House Robber II Note: This is an extension of House Robber. After robbing those houses on that stree ... 
随机推荐
- mongDB进阶
			Mongo进阶 聚合 聚合操作将来自多个文档的值组合在一起,并且可以对分组数据执行各种操作以返回单个结果. 文档进入多阶段管道,将文档转换为聚合结果 聚合管道 例子: 第一阶段:过滤,$match 第 ... 
- nodejs-os模块
			JavaScript 标准参考教程(alpha) 草稿二:Node.js os模块 GitHub TOP os模块 来自<JavaScript 标准参考教程(alpha)>,by 阮一峰 ... 
- 自定义控件CustomAlertView
			[记录][完整代码最下] 效果如下: 可行性分析: 由于系统自带的UIAlertView样式简单,只有两种样式,想要理想的样式就要自定义控件了 文件名取为:CustomAlertView 创建文件如下 ... 
- linux安装redis报错
			问题:You need tcl 8.5 or newer in order to run the Redis test 解决办法: wget http://downloads.sourceforge. ... 
- Can references refer to invalid location in C++?
			在C++中,引用比指针更加的安全,一方面是因为引用咋定义时必须进行初始化,另一方面是引用一旦被初始化就无法使其与其他对象相关联. 但是,在使用引用的地方仍然会有一些例外. (1)Reference t ... 
- 深入 char
			深入 char * ,char ** ,char a[ ] ,char *a[] 内核分类: c语言 2013-02-23 15:34 15176人阅读 评论(8) 收藏 举报Charcharchar ... 
- 【Python】【Module】time
			#_*_coding:utf-8_*_ __author__ = 'Alex Li' import time # print(time.clock()) #返回处理器时间,3.3开始已废弃 , 改成了 ... 
- React  传值  组件传值  之间的关系
			react 组件相互之间的传值: 传值分父级组件传值给子组件 子组件传值给父组件 平级组件.没有嵌套的组件相互传值 1.父组件向子组件传值 父组件通过属性的形式来向子组件传值,子组件通过pr ... 
- 云原生应用管理,像管理手机APP一样管理企业应用
			我们在使用智能手机的时候,手机APP从应用市场一键安装,安装好即点即用,当有新版本一键升级,如果不想用了长按图标删除,整个过程非常简单,小朋友都能熟练掌握.而对于企业应用,由于结构复杂.可用性要求高. ... 
- opencv学习(四)——轨迹栏作为调色板
			轨迹栏作为调色板 在这里,我们将创建一个简单的应用程序,以显示指定的颜色.有一个显示颜色的窗口,以及三个用于指定B.G.R颜色的轨迹栏.滑动轨迹栏,并相应地更改窗口颜色.默认情况下,初始颜色将设置为黑 ... 
