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. nrf51822 RAM不足分析

    之前了解过STM32 的内存分配问题,对于蓝牙芯片51822的内存分配问题把项目中,遇到了.bss和.data部分超了的问题,这其实就是声明的变量和stask 及 heap的大小总和超出了单片机的RA ...

  2. 判断是否有重复,判断字符串是否有重复汉字【c#】

    string corn = "公司"; int n = 0; if (tbCorporateName.Text.IndexOf(corn) > -1) { string co ...

  3. Spark(九)【RDD的分区和自定义Partitioner】

    目录 spark的分区 一. Hash分区 二. Ranger分区 三. 自定义Partitioner 案例 spark的分区 ​ Spark目前支持Hash分区和Range分区,用户也可以自定义分区 ...

  4. GO 总章

    GO 学习资源 go 代理 GO 语言结构 GO 数字运算 GO 时间处理 GO 定时器 GO 异常处理 go recover让崩溃的程序继续执行 GO Exit Fatal panic GO 通过进 ...

  5. Can we use function on left side of an expression in C and C++?

    In C, it might not be possible to have function names on left side of an expression, but it's possib ...

  6. 【编程思想】【设计模式】【行为模式Behavioral】模板模式Template

    Python转载版 https://github.com/faif/python-patterns/blob/master/behavioral/template.py #!/usr/bin/env ...

  7. Linux服务器---xopps

    XOOPS XOOPS是一款用php制作的开源网站管理系统,可用于构建各种网络站点. 1.下载XOOPS软件(https://xoops.org/) 2.将XOOPS软件中的htdocs文件夹拷贝到a ...

  8. spring jdbc 配置数据源连接数据库

    概述 在XML配置数据源,并将数据源注册到JDBC模板 JDBC模板关联业务增删改查 在XML配置数据源 <?xml version="1.0" encoding=" ...

  9. File类及常用操作方法

    import java.io.File; import java.io.IOException; public class file { public static void main(String[ ...

  10. my40_MySQL锁概述之意向锁

    本文在锁概述的基础上,通常实验举例,详细地介绍了意向锁的原理. 锁范围  全局锁(global lock)表锁(table lock)行锁 (row lock) ROW LOCK的粒度LOCK_REC ...