Codeforces 365D 贪心+dp

本文探讨了一个物品交换问题,目标是在遵循特定规则的情况下获得最大价值的物品集合。文章详细介绍了问题背景、约束条件及求解思路,并提供了一段C++代码实现。
D. Free Market
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 nd (1 ≤ n ≤ 501 ≤ 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).


题意:有n个品种的物品,每个物品的价格为c[ i ],每次你可以从你拥有的物品中挑取部分去商店去兑换。兑换有如下限制:

(1)你拿去兑换的物品(不妨设拿去兑换的物品集合为s),都要换成集合s所没有的。比如(a,b)--->(v,a)就是不合法的

(2)你拿去兑换的物品集s1里的价值和cost1与你兑换回来的物品集s2里的价值和cost2需要满足:cost1+d>=cost2

每天你只能去兑换一次。

问通过兑换可以获得的物品集价值和的最大值以及所需的兑换天数


题目链接:http://codeforces.com/contest/365/problem/D

题解:首先背包枚举下所有可能的状态,然后w表示当前可以到达最大的价值,枚举w+1~w+d可行的状态,转移到最大的可行状态,这里有一点值得思考,为什么转移时会满足条件1?因为从一个低状态w到另一个高状态w+i(i>=1&&i<=d),因为每种状态我们都只是每个物品最多放1次所到达的,定义状态1: w = a[p_1] + a[p_2] + ... + a[p_k], 状态2: w + i = a[q_1]+a[q_2] + ... + a[q_k'], 2个相同物品,比如a[p_i] = a[q_j], 违反了规则1, 我们完全可以保持a[p_i]在w状态里不动, 也就是说相同的物品从状态1(低状态)到状态2(高状态)里若存在相同元素,该元素不需调整。


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef long double ld;

#define x0 x0___
#define y0 y0___
#define pb push_back
#define SZ(X) ((int)X.size())
#define mp make_pair
#define Fi first
#define Se second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pli pair<ll,int>
#define pil pair<int,ll>
#define ALL(X) X.begin(),X.end()
#define RALL(X) X.rbegin(),X.rend()
#define rep(i,j,k) for(int i = j;i <= k;i ++)
#define per(i,j,k) for(int i = j;i >= k;i --)
#define mem(a,p) memset(a,p,sizeof(a))


const ll MOD = 1E9 + 7;
ll qmod(ll a,ll b,ll c) {ll res=1;a%=c; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%c;a=a*a%c;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}

template<typename T, typename S>
void upmax(T& a,S b){if(a<b) a=b;}
template<typename T, typename S>
void upmin(T& a,S b){if(a>b) a=b;}
void gettle() {while(1);}
void getre() {int t=0;t/=t;}


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
const int N = 57;

int dp[N * 10000];
int a[N];

int main()
{
    int n;
    int d;
    int s = 0;
    scanf("%d %d", &n, &d);
    dp[0] = 1;
    rep (i, 1, n) {
        scanf("%d", &a[i]);
        s += a[i];
        per (j, s, a[i]) {
            dp[j] |= dp[j - a[i]];
        }
    }

    int w, day;
    w = day = 0;

    while(true) {
        bool fg = 0;
        per (i, w + d, w + 1) {
            if(dp[i]) {
                fg = 1;
                day ++;
                w = i;
                break;
            }
        }
        if(!fg) break;
    }

    return !printf("%d %d\n", w, day);
}




### Codeforces Problem or Contest 1007 Information Codeforces平台上的编号为1007的比赛是Codeforces Round #532 (Div. 2),这是一场针对较低级别选手的编程竞赛[^2]。 #### 比赛概述 该场比赛包含了多个不同难度级别的题目,旨在测试参赛者的算法设计能力和编码技巧。比赛通常持续两到三个小时,在此期间,参与者需解决尽可能多的问题以获得高分。 #### 题目特点 对于这场比赛的具体题目特性,可以参考如下描述: - **时间限制与输入输出**:每道题都有严格的时间和内存限制。例如,在某些情况下,程序需要处理大量的数据并快速给出结果。 - **解法策略**:部分题目可能涉及动态规划、贪心算法或其他高级的数据结构应用。比如有一类问题是通过dp来计算已知两人最终得分推算每次得分的情况[^3]。 #### 示例代码片段 下面是一个简单的C++代码示例,用于展示如何读取输入并在特定条件下打印字符序列: ```cpp #include<bits/stdc++.h> using namespace std; int a[2000010],c[2000010],b[2000010]; char d[2000010]; int main(){ int k,n,ans,flag,tmp; cin >> k >> n; ans = 0; flag = 1; memset(c, 0, sizeof(c)); memset(b, 0, sizeof(b)); for(int i=0;i<n;i++){ cin >> a[i]; d[i]='0'; c[a[i]]++; b[c[a[i]]]++; if(b[c[a[i]]]==k){ d[i]='1'; } cout << d[i]; } cout<<endl; return 0; } ``` 这段代码展示了基本的数组操作以及条件判断逻辑,适用于处理一些基础类型的竞赛问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值