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. 『与善仁』Appium基础 — 16、APPium基础操作API

    目录 1.前置代码 2.安装和卸载APP 3.判断APP是否已安装 4.关闭APP软件和关闭驱动对象 5.发送文件到手机和获取手机中的文件 6.获取当前屏幕内元素结构(重点) 7.脚本内启动其他APP ...

  2. MybatisPlus的CRUD及拓展

    创建一个简单的MybatisPlus项目在上一篇博客:MybatisPlus入门程序 一.CRUD 1. select 1.1 查找全部用户 //查 @Test public void select( ...

  3. 学习java的第十四天

    一.今日收获 1.完成了手册第二章没有验证完成的例题 2.预习了第三章的算法以及for语句与if语句的用法 二.今日难题 1.验证上出现问题,没有那么仔细. 2.第二章还有没有完全理解的问题 三.明日 ...

  4. Shell 打印文件的最后5行

    目录 Shell 打印文件的最后5行 题解-awk 题解-tail Shell 打印文件的最后5行 经常查看日志的时候,会从文件的末尾往前查看,于是请你写一个 bash脚本以输出一个文本文件 nowc ...

  5. express系列(1)概述

    在 Node.js 出现之前,前后端的开发必须使用不同的语言进行.为此你需要学习多种的语言和框架.有了 Node.js 之后,你就可以使用一门语言在前后端开发中自由切换,这是最吸引人的地方. 什么是 ...

  6. Ecshop 后台导出订单Excel时, 内存溢出的解决方法

    今天继续跟大家分享一下,在我配置Ecshop时的问题. 今天的问题是在后台想要导出订单列表Excel时出现的内存溢出.错误提示如下 问题:  Fatal error: Allowed memory s ...

  7. Android Bitmap 全面解析(二)加载多张图片的缓存处理

    一般少量图片是很少出现OOM异常的,除非单张图片过~大~ 那么就可以用教程一里面的方法了通常应用场景是listview列表加载多张图片,为了提高效率一般要缓存一部分图片,这样方便再次查看时能快速显示~ ...

  8. Output of C++ Program | Set 4

    Difficulty Level: Rookie Predict the output of below C++ programs. Question 1 1 #include<iostream ...

  9. Oracle 创建 md5 加密函数

    使用 Oracle 的 utl_raw.DBMS_OBFUSCATION_TOOLKIT 可以获取 md5 加密字符串: select utl_raw.cast_to_raw(DBMS_OBFUSCA ...

  10. 【Linux】【Services】【Docker】应用

    1. Docker应用: 镜像:包含了启动Docker容器所需要的文件系统层级及其内容:基于UnionFS采用分层结构实现: bootfs,rootfs registry:保存docker镜像及镜像层 ...