# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
537934 |
2022-03-15T22:20:12 Z |
nicolexxuu |
Bank (IZhO14_bank) |
C++14 |
|
92 ms |
8508 KB |
#include <bits/stdc++.h>
#define MAXN 21
using namespace std;
int N, M;
int a[MAXN], b[MAXN];
int num_people[1 << MAXN], leftover[1 << MAXN];
int main() {
cin >> N >> M;
for(int i = 0; i < N; i++) cin >> a[i];
for(int i = 0; i < M; i++) cin >> b[i];
bool ok = false;
for(int mask = 0; mask < (1 << M); mask++) {
int p = num_people[mask];
// cout << "mask: " << mask << " p: " << p << "leftover: " << leftover[mask] << endl;
ok |= (p == N);
if(mask > 0 && (p == 0 && leftover[mask] == 0)) continue;
for(int note = 0; note < M; note++) {
if((mask & (1 << note)) == 0) {
int new_mask = mask + (1 << note);
if(leftover[mask] + b[note] < a[p]) {
num_people[new_mask] = p;
leftover[new_mask] = leftover[mask] + b[note];
// cout << "new mask: " << new_mask << " p: " << p << "leftover: " << leftover[new_mask] << endl;
} else if(leftover[mask] + b[note] == a[p]) {
num_people[new_mask] = p+1;
leftover[new_mask] = 0;
// cout << "new mask: " << new_mask << " p: " << p+1 << endl;
}
}
}
}
if(ok) cout << "YES" << endl;
else cout << "NO" << endl;
}
// alternate solution ..
//#include <bits/stdc++.h>
//#define MAXN 21
//using namespace std;
//
//int a[MAXN], b[MAXN];
//bool possible[MAXN][1 << MAXN];
//vector<int> combos[2005];
//int N, M;
//
//int main() {
// ios_base::sync_with_stdio(0);
// cin.tie(NULL);
// cin >> N >> M;
// for(int i = 0; i < N; i++) cin >> a[i];
// for(int i = 0; i < M; i++) cin >> b[i];
//
// for(int mask = 0; mask < (1 << M); mask++) {
// int sum = 0;
// for(int i = 0; i < M; i++)
// if(mask & (1 << i)) sum += b[i];
// if(sum <= 1000) combos[sum].push_back(mask);
// }
//
// possible[0][0] = true;
// for(int i = 0; i < N; i++) {
// for(int mask = 0; mask < (1 << M); mask++) {
// if(!possible[i][mask]) continue; // how much does this reduce complexity by?? lol
// for(int combo : combos[a[i]])
// if((combo & mask) == 0) possible[i+1][combo + mask] |= possible[i][mask];
// }
// }
//
// bool res = false;
// for(int i = 0; i < (1 << M); i++) res |= possible[N][i];
// if(res) cout << "YES\n";
// else cout << "NO\n";
//}
//#include <iostream>
//#include <vector>
//
//using std::cout;
//using std::endl;
//using std::vector;
//
//int main() {
// int people_num;
// int note_num;
// std::cin >> people_num >> note_num;
// vector<int> people(people_num);
// vector<int> banknotes(note_num);
// for (int& p : people) {
// std::cin >> p;
// }
// for (int& b : banknotes) {
// std::cin >> b;
// }
//
// vector<int> leftover(1 << note_num, -1);
// vector<int> people_covered(1 << note_num, -1);
// leftover[0] = 0;
// people_covered[0] = 0;
// for (int s = 0; s < (1 << note_num); s++) {
// for (int last = 0; last < note_num; last++) {
// if ((s & (1 << last)) == 0) {
// continue;
// }
// int prev = s & ~(1 << last);
// if (people_covered[prev] == -1) {
// continue;
// }
//
// int new_amt = leftover[prev] + banknotes[last];
// // the salary of the current person we're going to try to pay
// int curr_target = people[people_covered[prev]];
// // if it's still not enough, just increment the leftover pile
// if (new_amt < curr_target) {
// people_covered[s] = people_covered[prev];
// leftover[s] = new_amt;
// }
// /*
// * or it's exactly right, in which case reset the leftover count
// * and increment the covered amount
// */
// else if (new_amt == curr_target) {
// people_covered[s] = people_covered[prev] + 1;
// leftover[s] = 0;
// }
// }
// // we've covered all the people!
// if (people_covered[s] == people_num) {
// cout << "YES" << endl;
// return 0;
// }
// }
// cout << "NO" << endl;
//}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
44 ms |
8492 KB |
Output is correct |
9 |
Correct |
74 ms |
8404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
312 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
44 ms |
8492 KB |
Output is correct |
9 |
Correct |
74 ms |
8404 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
300 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
312 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
66 ms |
8460 KB |
Output is correct |
32 |
Correct |
82 ms |
8480 KB |
Output is correct |
33 |
Correct |
12 ms |
8432 KB |
Output is correct |
34 |
Correct |
4 ms |
2388 KB |
Output is correct |
35 |
Correct |
4 ms |
2616 KB |
Output is correct |
36 |
Correct |
3 ms |
724 KB |
Output is correct |
37 |
Correct |
3 ms |
980 KB |
Output is correct |
38 |
Correct |
2 ms |
340 KB |
Output is correct |
39 |
Correct |
24 ms |
8396 KB |
Output is correct |
40 |
Correct |
3 ms |
432 KB |
Output is correct |
41 |
Correct |
3 ms |
596 KB |
Output is correct |
42 |
Correct |
51 ms |
8476 KB |
Output is correct |
43 |
Correct |
8 ms |
5204 KB |
Output is correct |
44 |
Correct |
3 ms |
340 KB |
Output is correct |
45 |
Correct |
43 ms |
8424 KB |
Output is correct |
46 |
Correct |
23 ms |
8404 KB |
Output is correct |
47 |
Correct |
3 ms |
724 KB |
Output is correct |
48 |
Correct |
20 ms |
8148 KB |
Output is correct |
49 |
Correct |
3 ms |
468 KB |
Output is correct |
50 |
Correct |
74 ms |
8508 KB |
Output is correct |
51 |
Correct |
4 ms |
1108 KB |
Output is correct |
52 |
Correct |
8 ms |
8496 KB |
Output is correct |
53 |
Correct |
92 ms |
8396 KB |
Output is correct |
54 |
Correct |
91 ms |
8400 KB |
Output is correct |
55 |
Correct |
75 ms |
8412 KB |
Output is correct |
56 |
Correct |
78 ms |
8396 KB |
Output is correct |
57 |
Correct |
78 ms |
8508 KB |
Output is correct |
58 |
Correct |
80 ms |
8492 KB |
Output is correct |