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@
  • 相关阅读:
    《Linux C一站式学习》第七章 结构体
    《Linux C一站式学习》第三章 简单的函数
    《Windows程序设计》第一章 起步
    CSS在线课程学习笔记
    《Windows程序设计》第三章 窗口和消息
    window.open与window.showModalDialog中主页面与从页面间的通信(原创) 中庸
    php文件实现将大文件导入到mysql数据库中
    我为何爱读代码?你为何也应当爱?
    解决phpMyAdmin导入mysql数据库超过2M的问题
    对linux交换分区swap的一些认识总结
  • 原文地址:https://www.cnblogs.com/zzuli2sjy/p/5503451.html
Copyright © 2011-2022 走看看