Submission #524249

# Submission time Handle Problem Language Result Execution time Memory
524249 2022-02-08T21:31:16 Z macktvz Burza (COCI16_burza) C++17
0 / 160
549 ms 524292 KB
#include <bits/stdc++.h>
using namespace std;

// can we split B into disjoint subsets s.t. there is at least one subset per Ai
// for multiple A, need to find subsets that satisfy each A and & to 0
// dp[MASK] = TRUE if 

int main() {
    int n,m; cin >> n >> m;
    vector<int> A(n),B(m); 
    for(auto &a : A) cin >> a;
    for(auto &b : B) cin >> b;
    vector<vector<int>> S(n); // with some sum there are only sqrt(sum) different possible values
    for(int mask = 0; mask < (1<<m); mask++) {
        int s = 0;
        for(int i = 0; i < m; i++) {
            if (mask&(1<<i)) s += B[i];
        }
        for(int i = 0; i < n; i++) {
            if (s == A[i]) S[i].push_back(mask);
        }
    }
    vector<vector<int>> dp((1 << n));
    for(int i = 0; i < n; i++) {
        dp[1<<i]=S[i];
    }
    for(int mask = 0; mask < (1 << n); mask++) {
        if (__builtin_popcount(mask) <= 1) continue;
        int ch;
        for(int i = 0; i < n; i++) { // find first bit set
            if (mask&(1<<i)) {
                ch = i;
                break;
            }
        }
        for(int j : dp[mask^(1<<ch)]) {
            for(int i : S[ch]) {
                if (j&i == 0) {
                    dp[mask].push_back(j|i);
                }
            }
        }
    }
    int sz = (int)dp[(1<<n)-1].size();
    cout << (sz == 0 ? "NO" : "YES") << '\n';
}

Compilation message

burza.cpp: In function 'int main()':
burza.cpp:38:25: warning: suggest parentheses around comparison in operand of '&' [-Wparentheses]
   38 |                 if (j&i == 0) {
      |                       ~~^~~~
burza.cpp:37:29: warning: 'ch' may be used uninitialized in this function [-Wmaybe-uninitialized]
   37 |             for(int i : S[ch]) {
      |                             ^
# Verdict Execution time Memory Grader output
1 Runtime error 549 ms 524288 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 236 ms 524292 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 29 ms 416 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 540 ms 524288 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 30 ms 3484 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 31 ms 548 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 35 ms 428 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 9 ms 3532 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 266 ms 399864 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 15 ms 580 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -