Light oj 1217 - Neighbor House (II)(dp)

本文详细介绍了如何解决1217-NeighborHouse(II)问题,包括输入输出规范、算法思路及代码实现,旨在帮助读者理解和解决类似问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 5has 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







#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<map>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define bug printf("hihi\n")

#define eps 1e-12

typedef long long ll;
using namespace std;
#define N 1005

int a[N];
int n;
int dp[2][N][2];

int main()
{
    int i,j,t,ca=0;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(i=1;i<=n;i++)
            scanf("%d",&a[i]);
        memset(dp,0,sizeof(dp));

        dp[0][1][0]=0;
        dp[1][1][1]=a[1];

        for(int i=2;i<=n;i++)
        {
            dp[0][i][1]=a[i]+dp[0][i-1][0];
            dp[0][i][0]=max(dp[0][i-1][1],dp[0][i-1][0]);

            dp[1][i][1]=a[i]+dp[1][i-1][0];
            dp[1][i][0]=max(dp[1][i-1][1],dp[1][i-1][0]);
        }
        dp[1][n][1]-=a[n];

         int ans=0;
         ans=max(dp[0][n][0],dp[0][n][1]);
         ans=max(ans,dp[1][n][0]);
         ans=max(ans,dp[1][n][1]);
         printf("Case %d: %d\n",++ca,ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值