codeforces Educational Codeforces Round 9 E - Thief in a Shop

Posted notnight

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了codeforces Educational Codeforces Round 9 E - Thief in a Shop相关的知识,希望对你有一定的参考价值。

E - Thief in a Shop

题目大意:给你n ( n <= 1000)个物品每个物品的价值为ai (ai <= 1000),你只能恰好取k个物品,问你能组成哪些价值。

 

思路:我们很容易能够想到dp[ i ][ j ]表示取i次j是否存在,但是复杂度1e12肯定不行。

我们将ai排序,每个值都减去a[1]然后再用dp[ i ]表示到达i这个值最少需要取几次,只需要1e9就能完成,

我们扫一遍dp数组,如果dp[ i ]  <= k 则说明 i + k * a[1]是能取到的。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define pii pair<int,int>
#define piii pair<int, pair<int,int> >

using namespace std;

const int N = 1e3 + 10;
const int M = 10 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-6;

int dp[N * N], a[N], n, k;
int main() {
    memset(dp, inf, sizeof(dp));
    scanf("%d%d", &n, &k);
    for(int i = 1; i <= n; i++)
        scanf("%d", &a[i]);

    sort(a + 1, a + n + 1);

    for(int i = 2; i <= n; i++)
        a[i] -= a[1];

    dp[0] = 0;

    for(int i = 0; i <= 1000000; i++) {
        for(int j = 2; j <= n; j++) {
            if(a[j] + i > 1000000) continue;
            dp[i + a[j]] = min(dp[i + a[j]], dp[i] + 1);
        }
    }

    for(int i = 0; i <= 1000000; i++) {
        if(dp[i] <= k) {
            printf("%d ", i + a[1] * k);
        }
    }
    puts("");
    return 0;
}
/*
*/

 

以上是关于codeforces Educational Codeforces Round 9 E - Thief in a Shop的主要内容,如果未能解决你的问题,请参考以下文章

Educational Codeforces Round 37 B Tea Queue

Codeforces Educational Codeforces Round 67

Educational Codeforces Round 7 B

Educational Codeforces Round 7 A

Educational Codeforces Round 7

Codeforces Educational Codeforces Round 54 题解