I. Shopping Spree

ou've won a shopping spree with a very peculiar rule. The items you are allowed to take are in consecutive order, indexed 1 through n. You may select any subset of these items subject to the following constraint:

For each index k of items chosen for the subset at most half of the items with indexes 1 through k may be in the subset

For example, if the item with index 10 is chosen for the subset, then your selected subset can contain at most half of the items with index 1 through 10. Similarly, if the item with index 2 is chosen for the subset, then your selected subset can contain at most half of the items with index 1 through 2. Note that “half” is an integer value so half of 10 and 11 are both 5. The only exception to the constraint is that if the item with index 1 is chosen for the subset, you can select 1 item and not zero (to be fair).

The Problem:

Given a list of the dollar values of items, I1, I2, … In, in the shopping spree, determine the maximum value you can obtain from the shopping spree subject to the above constraint.

The Input:

The first line of the input is a positive integer, n, indicating the number of shopping sprees that your program will have to analyze. Following this will be the descriptions of each shopping spree. Each shopping spree will be described on a single line. The first value on each of these lines will be a single positive integer, s (s ≤ 500), representing the number of items for the shopping spree. The next s space-separated positive integers will be the values for the shopping spree items in dollars, in order. Each of these values will be less than or equal to 106.

The Output:

For each shopping spree, first output the heading “Spree #d: ”, where d is the spree number, starting with 1. Then, print a single integer equal to the maximum value, in dollars, that can be obtained for that shopping spree. Follow the format illustrated in Sample Output.

样例输入复制

2
5
1 2 3 4 5
3
12 2 4

样例输出复制

Spree #1: 9
Spree #2: 12

 

思路:dp f[j][k]表示从前j个数中选k个数,注意一下,取最大的话前n个数必定会取n/2个数。

#include<bits/stdc++.h>

using namespace std;

const int N=550;
int f[N][N];
int a[N];
int main()
{
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		int m;
		cin>>m;
		for(int j=1;j<=m;j++){
			cin>>a[j];
		}
		f[1][1]=a[1];
		for(int j=2;j<=m;j++){
			for(int k=1;k<=j/2;k++){
				f[j][k]=max(f[j-1][k],f[j-1][k-1]+a[j]);
			}
		}

		printf("Spree #%d: ",i);
//		cout<<f[1][1]<<endl;
		cout<<max(f[1][1],f[m][m/2])<<endl; 
	} 
	return 0;
 } 
//2
//5
//1 2 3 4 5
//3
//12 2 4

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值