CodeForces - 364B Free Market (Dp+贪心思想结合 )

9 篇文章 0 订阅
3 篇文章 0 订阅

John Doe has recently found a "Free Market" in his city — that is the place where you can exchange some of your possessions for other things for free.

John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.

For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x).

During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.


Input

The first line contains two space-separated integers n, d (1 ≤ n ≤ 50, 1 ≤ d ≤ 104) — the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 ≤ ci ≤ 104).

Output

Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.

Examples
Input
3 2
1 3 10
Output
4 3
Input
3 5
1 2 3
Output
6 2
Input
10 10000
10000 9999 1 10000 10000 10000 1 2 3 4
Output
50010 6
Note

In the first sample John can act like this:

  • Take the first item (1 - 0 ≤ 2).
  • Exchange the first item for the second one (3 - 1 ≤ 2).
  • Take the first item (1 - 0 ≤ 2).

题目大意:

(1)有n种不同的物体,每种物体对应各自的价值Ci。

(2)John发现有一个自由市场可以交换物品,交换规则为:拿出去交换的物品集合A与交换回来的物品集合B之间不能有相同的物体(即A,B交集为空),可以拿空集交换。

(3)John还是要脸的,给出一个John的廉耻值d,每次交换后所得利润不能超过d(即s(A)-s(B)< =d),且每天只能交换一次。

(4)求出这样交换之后所可能获得的最大价值和所花费的最小天数。

解题大意:

拿到题目,感受到一股贪心的气息。

要求天数最少,那么每天需要在额度d内是价值增量尽可能地大。可以用一个数组预处理来记录每种状态是否可能达到。达到记为1,默认置0。之后用循环模拟,一轮代表一天,每次数组下标从该天所可能取到的最大值开始(前一天的价值+d)向前倒退直到值为1,该天的价值即为下标。若倒退完之后仍未找到值为1的下标,则退出循环,输出答案。

#include <iostream>
#include <string>
#include <climits>
#include <cstdio>
#include <vector>
using namespace std;
const int maxn=100000+5;
int dp[51*10001];
int c[55];
int main(){			//by bibibi CSDN 
	int n,d,sum=0;
	cin>>n>>d;
	dp[0]=1;
	for(int i=0;i<n;i++){
		int x;
		cin>>x;	
		for(int j=(sum+=x);j>=x;j--){
			if(dp[j-x]) dp[j]=1;
		}
	}
	int w=0,day=0;
	while(1){
		int j=w+d;
		while(!dp[j] && j>w) j--;
		if(j==w) break;
		w=j;day++;	
	}
	cout<<w<<" "<<day<<endl;
}


引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值