lightoj1217_简单dp

本文针对一家公司在特定区域内的销售策略问题,提出了一种算法来最大化潜在的销售数量。该算法考虑了房屋之间的环形布局,并确保销售活动不会连续影响相邻的房屋。通过对不同情况的动态规划,最终确定了公司可以实现的最大销售量。

题目链接:http://lightoj.com/volume_showproblem.php?problem=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 ith house. 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

 

 1 #include <algorithm>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <cstdio>
 6 #include <vector>
 7 #include <ctime>
 8 #include <queue>
 9 #include <list>
10 #include <set>
11 #include <map>
12 using namespace std;
13 #define INF 0x3f3f3f3f
14 typedef long long LL;
15 
16 int a[1005], dp[1005][2];
17 int main()
18 {
19     int t, n;
20     scanf("%d", &t);
21     for(int ca = 1; ca <= t; ca++)
22     {
23         scanf("%d", &n);
24         int res = 0;
25         for(int i = 1; i <= n; i++){
26             scanf("%d", &a[i]);
27             res = max(res, a[i]);
28         }
29         memset(dp, 0, sizeof(dp));
30         dp[2][0] = a[1];//第一栋楼卖
31         for(int i = 3; i <= n; i++)
32         {
33             dp[i][0] = max(dp[i][0], max(dp[i-1][1], dp[i-1][0]));
34             if(i == n)
35                 break;
36             dp[i][1] = max(dp[i][1], dp[i-1][0]+a[i]);
37         }
38         res = max(res, dp[n][0]);
39         memset(dp, 0, sizeof(dp));
40         dp[2][1] = a[2];//第一栋楼不卖
41         for(int i = 3; i <= n; i++)
42         {
43             dp[i][0] = max(dp[i][0], max(dp[i-1][1], dp[i-1][0]));
44             dp[i][1] = max(dp[i][1], dp[i-1][0]+a[i]);
45         }
46         res = max(res, max(dp[n][0], dp[n][1]));
47         printf("Case %d: %d\n", ca, res);
48     }
49     return 0;
50 }

 

转载于:https://www.cnblogs.com/luomi/p/5943882.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值