#include <iostream> #include <vector> #include <algorithm> #include <limits> using namespace std; struct Cmd{ char opcode; int val; }; int n; vector<vector<Cmd>>Programs; vector<int>ProgramCounters; vector<long long>PrivateCounters; long long globalCounter, bestVal; void R(){ bool finished=true; for(int i=0;i<n;i++) if(ProgramCounters[i]<(int)Programs[i].size()){ finished=false; Cmd cmd = Programs[i][ProgramCounters[i]]; ProgramCounters[i]++; switch(cmd.opcode){ case 'W':{ long long y_copy = PrivateCounters[i]; PrivateCounters[i] = globalCounter; R(); PrivateCounters[i] = y_copy; break; } case 'Z':{ long long x_copy = globalCounter; globalCounter = PrivateCounters[i]; R(); globalCounter = x_copy; break; } default:{ PrivateCounters[i] += cmd.val*((cmd.opcode=='+')?1:-1); R(); PrivateCounters[i] -= cmd.val*((cmd.opcode=='+')?1:-1); break; } } ProgramCounters[i]--; } if(finished) bestVal = min(bestVal, globalCounter); } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while(t--){ cin >> n; Programs.resize(n); for(vector<Cmd>& program : Programs){ int l; cin >> l; program.resize(l); for(Cmd& cmd : program){ cin >> cmd.opcode; if(cmd.opcode=='+' || cmd.opcode=='-') cin >> cmd.val; } } ProgramCounters.clear(); ProgramCounters.resize(n, 0); PrivateCounters.clear(); PrivateCounters.resize(n, 0); globalCounter=0; bestVal=numeric_limits<long long>::max(); R(); cout << bestVal << "\n"; } return 0; }
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 | #include <iostream> #include <vector> #include <algorithm> #include <limits> using namespace std; struct Cmd{ char opcode; int val; }; int n; vector<vector<Cmd>>Programs; vector<int>ProgramCounters; vector<long long>PrivateCounters; long long globalCounter, bestVal; void R(){ bool finished=true; for(int i=0;i<n;i++) if(ProgramCounters[i]<(int)Programs[i].size()){ finished=false; Cmd cmd = Programs[i][ProgramCounters[i]]; ProgramCounters[i]++; switch(cmd.opcode){ case 'W':{ long long y_copy = PrivateCounters[i]; PrivateCounters[i] = globalCounter; R(); PrivateCounters[i] = y_copy; break; } case 'Z':{ long long x_copy = globalCounter; globalCounter = PrivateCounters[i]; R(); globalCounter = x_copy; break; } default:{ PrivateCounters[i] += cmd.val*((cmd.opcode=='+')?1:-1); R(); PrivateCounters[i] -= cmd.val*((cmd.opcode=='+')?1:-1); break; } } ProgramCounters[i]--; } if(finished) bestVal = min(bestVal, globalCounter); } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while(t--){ cin >> n; Programs.resize(n); for(vector<Cmd>& program : Programs){ int l; cin >> l; program.resize(l); for(Cmd& cmd : program){ cin >> cmd.opcode; if(cmd.opcode=='+' || cmd.opcode=='-') cin >> cmd.val; } } ProgramCounters.clear(); ProgramCounters.resize(n, 0); PrivateCounters.clear(); PrivateCounters.resize(n, 0); globalCounter=0; bestVal=numeric_limits<long long>::max(); R(); cout << bestVal << "\n"; } return 0; } |