#include "bits/stdc++.h" // Tomasz Nowak using namespace std; // XIII LO Szczecin using LL = long long; // Poland #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define REP(i, n) FOR(i, 0, (n) - 1) template<class T> int size(T &&x) { return int(x.size()); } template<class A, class B> ostream& operator<<(ostream &out, const pair<A, B> &p) { return out << '(' << p.first << ", " << p.second << ')'; } template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) { out << '{'; for(auto it = x.begin(); it != x.end(); ++it) out << *it << (it == prev(x.end()) ? "" : ", "); return out << '}'; } void dump() {} template<class T, class... Args> void dump(T &&x, Args... args) { cerr << x << "; "; dump(args...); } #ifdef DEBUG struct Nl{~Nl(){cerr << '\n';}}; # define debug(x...) cerr << (strcmp(#x, "") ? #x ": " : ""), dump(x), Nl(), cerr << "" #else # define debug(...) 0 && cerr #endif mt19937_64 rng(0); int rd(int l, int r) { return uniform_int_distribution<int>(l, r)(rng); } // end of templates struct State { vector<int> exec_time; vector<LL> y; LL x = 0; }; bool operator<(const State &a, const State &b) { auto t = [&](const State &s) { return make_tuple(s.x, s.exec_time, s.y); }; return t(a) < t(b); } bool operator==(const State &a, const State &b) { return a.x == b.x and a.exec_time == b.exec_time and a.y == b.y; } ostream& operator<<(ostream &o, State &s) { return o << '[' << s.exec_time << ", " << s.y << ", " << s.x << ']'; } void solve() { int n; cin >> n; vector<vector<pair<char, int>>> instructions(n); int sum_l = 0; for(auto &v : instructions) { int l; cin >> l; sum_l += l; v.resize(l); for(auto &[type, c] : v) { cin >> type; if(type == '+' or type == '-') cin >> c; } } debug(sum_l); debug(instructions); State init_state; init_state.exec_time.resize(n); init_state.y.resize(n); vector<State> curr = {init_state}, nxt; for(int l = 0; l < sum_l; ++l) { sort(curr.begin(), curr.end()); curr.erase(unique(curr.begin(), curr.end()), curr.end()); for(State &s : curr) for(int i = 0; i < n; ++i) if(s.exec_time[i] != size(instructions[i])) { State cp = s; auto [type, c] = instructions[i][cp.exec_time[i]++]; if(type == 'W') cp.y[i] = cp.x; else if(type == 'Z') cp.x = cp.y[i]; else if(type == '+') cp.y[i] += c; else if(type == '-') cp.y[i] -= c; else assert(false); nxt.emplace_back(cp); } curr.clear(); swap(nxt, curr); } sort(curr.begin(), curr.end()); debug(curr); assert(size(curr)); LL min_x = curr.front().x; for(auto &s : curr) min_x = min(min_x, s.x); cout << min_x << '\n'; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while(t --> 0) solve(); }
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 | #include "bits/stdc++.h" // Tomasz Nowak using namespace std; // XIII LO Szczecin using LL = long long; // Poland #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define REP(i, n) FOR(i, 0, (n) - 1) template<class T> int size(T &&x) { return int(x.size()); } template<class A, class B> ostream& operator<<(ostream &out, const pair<A, B> &p) { return out << '(' << p.first << ", " << p.second << ')'; } template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) { out << '{'; for(auto it = x.begin(); it != x.end(); ++it) out << *it << (it == prev(x.end()) ? "" : ", "); return out << '}'; } void dump() {} template<class T, class... Args> void dump(T &&x, Args... args) { cerr << x << "; "; dump(args...); } #ifdef DEBUG struct Nl{~Nl(){cerr << '\n';}}; # define debug(x...) cerr << (strcmp(#x, "") ? #x ": " : ""), dump(x), Nl(), cerr << "" #else # define debug(...) 0 && cerr #endif mt19937_64 rng(0); int rd(int l, int r) { return uniform_int_distribution<int>(l, r)(rng); } // end of templates struct State { vector<int> exec_time; vector<LL> y; LL x = 0; }; bool operator<(const State &a, const State &b) { auto t = [&](const State &s) { return make_tuple(s.x, s.exec_time, s.y); }; return t(a) < t(b); } bool operator==(const State &a, const State &b) { return a.x == b.x and a.exec_time == b.exec_time and a.y == b.y; } ostream& operator<<(ostream &o, State &s) { return o << '[' << s.exec_time << ", " << s.y << ", " << s.x << ']'; } void solve() { int n; cin >> n; vector<vector<pair<char, int>>> instructions(n); int sum_l = 0; for(auto &v : instructions) { int l; cin >> l; sum_l += l; v.resize(l); for(auto &[type, c] : v) { cin >> type; if(type == '+' or type == '-') cin >> c; } } debug(sum_l); debug(instructions); State init_state; init_state.exec_time.resize(n); init_state.y.resize(n); vector<State> curr = {init_state}, nxt; for(int l = 0; l < sum_l; ++l) { sort(curr.begin(), curr.end()); curr.erase(unique(curr.begin(), curr.end()), curr.end()); for(State &s : curr) for(int i = 0; i < n; ++i) if(s.exec_time[i] != size(instructions[i])) { State cp = s; auto [type, c] = instructions[i][cp.exec_time[i]++]; if(type == 'W') cp.y[i] = cp.x; else if(type == 'Z') cp.x = cp.y[i]; else if(type == '+') cp.y[i] += c; else if(type == '-') cp.y[i] -= c; else assert(false); nxt.emplace_back(cp); } curr.clear(); swap(nxt, curr); } sort(curr.begin(), curr.end()); debug(curr); assert(size(curr)); LL min_x = curr.front().x; for(auto &s : curr) min_x = min(min_x, s.x); cout << min_x << '\n'; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while(t --> 0) solve(); } |