Submission #524251

# Submission time Handle Problem Language Result Execution time Memory
524251 2022-02-08T21:36:11 Z macktvz Burza (COCI16_burza) C++17
0 / 160
546 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);
  	vector<int> 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:39:25: warning: suggest parentheses around comparison in operand of '&' [-Wparentheses]
   39 |                 if (j&i == 0) {
      |                       ~~^~~~
burza.cpp:38:29: warning: 'ch' may be used uninitialized in this function [-Wmaybe-uninitialized]
   38 |             for(int i : S[ch]) {
      |                             ^
# Verdict Execution time Memory Grader output
1 Runtime error 546 ms 524288 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 234 ms 524292 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 31 ms 428 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 513 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 3504 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 32 ms 540 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 30 ms 420 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 13 ms 3520 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 261 ms 399772 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 17 ms 560 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -