zoukankan      html  css  js  c++  java
  • 1217

    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
    ",s,maxx);
    51         }
    52         return 0;
    53 }
    油!油!you@
  • 相关阅读:
    ActiveMQ_Linux安装(一)
    Jenkins_多项目构建(二):使用Maven聚集关系
    Shell命令_for
    C# 读取Excel
    [ORACLE错误]ORA-00001: unique constraint (...) violated并不一定是数据冲突
    [ORACLE错误]ORA-00054:resource busy and acquire with nowait specified解决方法
    [ORACLE错误]oracle 不能更新 PL/SQL 点击“edit data”报“ these query results are not updateable”
    只有 DBA 才能导入由其他 DBA 导出的文件
    Windows系统安装Oracle 11g客户端
    Windows系统安装Oracle 11g数据库
  • 原文地址:https://www.cnblogs.com/zzuli2sjy/p/5503451.html
Copyright © 2011-2022 走看看