///MEMENTO MEMO, MEMENTO LONG LONG #include <bits/stdc++.h> #define DEBUG if(0) #define COUT cout << "\e[36m" #define ENDL "\e[39m" << endl #define VAR(v) " [\e[32m" << #v << "\e[36m=\e[91m" << v << "\e[36m] " #define MP make_pair #define ST first #define ND second using namespace std; const int BN = 3; typedef long long LL; typedef array<unsigned int, BN> Bottles; typedef unsigned long long ix_t; Bottles capacities; Bottles bits; vector<int> ans; int found_ans = 0; void calc_bits(Bottles caps) { for (int i = 0; i < BN; ++i) { if(caps[i] == 0) { bits[i] = 1; } while(caps[i] > 0) { bits[i]++; caps[i] /=2; } } } ix_t index(Bottles b) { ix_t ret = 0; ret += b[0]; ret <<= bits[1]; ret += b[1]; return ret; } //vector<bool> vis; set<ix_t> vis; void acknowledge(Bottles b, int t) { vis.insert(index(b)); for(auto el : b) { if(ans[el] == -1) { found_ans++; ans[el] = t; } } } void bfs(Bottles start) { queue<pair<Bottles, int>> que; que.push(MP(start, 0)); acknowledge(start, 0); while(!que.empty()) { Bottles curr = que.front().first; int time = que.front().second; que.pop(); for (int i = 0; i < BN; ++i) { for (int j = 0; j < BN; ++j) { if(i != j) { Bottles after = curr; int sum = curr[i] + curr[j]; after[i] = min(curr[i] + curr[j], capacities[i]); after[j] = sum - after[i]; if(!vis.count(index(after))) { acknowledge(after, time+1); if(found_ans == ans.size()) { return; } que.push(MP(after, time+1)); } } } } } } ostream& operator<<(ostream& out, Bottles b) { out << "[" << b[0] << ", " << b[1] << ", " << b[2] << "] "; return out; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); Bottles start; for (int i = 0; i < BN; ++i) { cin >> capacities[i]; } for (int i = 0; i < BN; ++i) { cin >> start[i]; } ans.resize(capacities[2]+1, -1); calc_bits(capacities); // vis.resize(index(capacities)+1, false); // DEBUG COUT << bits << endl << index(start) << endl <<index(capacities) << endl; found_ans += max(0, (int)((int)capacities[2] - (int)start[0] - (int)start[1] -(int)start[2])); bfs(start); for(auto el : ans) { cout << el << " "; } cout << endl; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | ///MEMENTO MEMO, MEMENTO LONG LONG #include <bits/stdc++.h> #define DEBUG if(0) #define COUT cout << "\e[36m" #define ENDL "\e[39m" << endl #define VAR(v) " [\e[32m" << #v << "\e[36m=\e[91m" << v << "\e[36m] " #define MP make_pair #define ST first #define ND second using namespace std; const int BN = 3; typedef long long LL; typedef array<unsigned int, BN> Bottles; typedef unsigned long long ix_t; Bottles capacities; Bottles bits; vector<int> ans; int found_ans = 0; void calc_bits(Bottles caps) { for (int i = 0; i < BN; ++i) { if(caps[i] == 0) { bits[i] = 1; } while(caps[i] > 0) { bits[i]++; caps[i] /=2; } } } ix_t index(Bottles b) { ix_t ret = 0; ret += b[0]; ret <<= bits[1]; ret += b[1]; return ret; } //vector<bool> vis; set<ix_t> vis; void acknowledge(Bottles b, int t) { vis.insert(index(b)); for(auto el : b) { if(ans[el] == -1) { found_ans++; ans[el] = t; } } } void bfs(Bottles start) { queue<pair<Bottles, int>> que; que.push(MP(start, 0)); acknowledge(start, 0); while(!que.empty()) { Bottles curr = que.front().first; int time = que.front().second; que.pop(); for (int i = 0; i < BN; ++i) { for (int j = 0; j < BN; ++j) { if(i != j) { Bottles after = curr; int sum = curr[i] + curr[j]; after[i] = min(curr[i] + curr[j], capacities[i]); after[j] = sum - after[i]; if(!vis.count(index(after))) { acknowledge(after, time+1); if(found_ans == ans.size()) { return; } que.push(MP(after, time+1)); } } } } } } ostream& operator<<(ostream& out, Bottles b) { out << "[" << b[0] << ", " << b[1] << ", " << b[2] << "] "; return out; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); Bottles start; for (int i = 0; i < BN; ++i) { cin >> capacities[i]; } for (int i = 0; i < BN; ++i) { cin >> start[i]; } ans.resize(capacities[2]+1, -1); calc_bits(capacities); // vis.resize(index(capacities)+1, false); // DEBUG COUT << bits << endl << index(start) << endl <<index(capacities) << endl; found_ans += max(0, (int)((int)capacities[2] - (int)start[0] - (int)start[1] -(int)start[2])); bfs(start); for(auto el : ans) { cout << el << " "; } cout << endl; } |