Cod sursa(job #1709782)

Utilizator team_nameUPB Banu Popa Visan team_name Data 28 mai 2016 13:51:12
Problema Sate2 Scor 0
Compilator cpp Status done
Runda ONIS 2016 - Runda - 2 - ACM ICPC Romanian Programming Contest Marime 1.13 kb
#include <iostream>
#include <algorithm>

using namespace std;

int t, n, m, k;

const int mmax = 90005;
const int nmax = 3005;

int v[nmax];
bool dp[mmax];

int main() {
    freopen("sate2.in", "r", stdin);
    freopen("sate2.out", "w", stdout);

    scanf("%d", &t);

    while (t--) {
        scanf("%d %d %d", &n, &m, &k);

        for (int i = 1; i <= n; ++i) {
            scanf("%d", &v[i]);
        }

        if (m % k) { printf("NU\n"); continue; }

        int iter = 5;
        bool ok = 0;

        while(iter -- && !ok) {
            random_shuffle(v + 1, v + n + 1);

            dp[0] = 1;

            int x = m / k;
            int j = 0;
            for (int i = 1; i <= n && j < k - 1; i++) {
                for (int s = (j+1) * x; s - v[i] >= j * x; s--) {
                    dp[s] = dp[s] || dp[s - v[i]];
                }

                if (dp[(j+1)*x])
                    j++;
            }

            if (j >= k - 1) {
                printf("DA\n");
                ok = 1;
            }

            for (int i = 0; i <= m; ++i)
                 dp[i] = 0;
         }

         if(!ok) printf("NU\n");
    }

    return 0;
}