Paste random, #sxr -- näytä pelkkänä tekstinä -- uusi tämän pohjalta
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 | #include <bits/stdc++.h> #define ll long long using namespace std; map<ll, vector<string>> opt; ll xs[101]; int main() { srand(time(0)); ifstream input("test.in"); ifstream output("out.txt"); int t; input>>t; for (int i = 0; i < t; i++) { ll x; input>>x; xs[i] = x; int len; output>>len; string s; for (int j = 0; j < len; j++) { output>>s; opt[x].push_back(s); } } input.close(); output.close(); while (true) { vector<string> seq; int slen = 10 + (rand() % 30); for (int i = 0; i < slen; i++) { int type = rand() % 9; if (type == 0) seq.push_back("+"); if (type == 1) seq.push_back("+"); if (type == 2) seq.push_back("+"); if (type == 3) seq.push_back("DUP"); if (type == 4) seq.push_back("DUP"); if (type == 5) seq.push_back("DUP"); if (type == 6) seq.push_back("*"); if (type == 7) seq.push_back("OVER"); if (type == 8) seq.push_back("SWAP"); } vector<ll> st = {1}; for (int j = 0; j < seq.size(); j++) { string s = seq[j]; if (s == "+") { if (st.size() == 1) { seq.erase(seq.begin() + j); j--; } else { ll y = st.back(); st.pop_back(); st.back() += y; } } else if (s == "*") { if (st.size() == 1) { seq.erase(seq.begin() + j); j--; } else { ll y = st.back(); st.pop_back(); st.back() *= y; } } else if (s == "DUP") { st.push_back(st.back()); } else if (s == "OVER") { if (st.size() == 1) { seq.erase(seq.begin() + j); j--; } else { st.push_back(st[st.size() - 2]); } } else if (s == "SWAP") { if (st.size() == 1) { seq.erase(seq.begin() + j); j--; } else { swap(st[st.size() - 1], st[st.size() - 2]); } } } while (st.size() > 1) { seq.push_back("+"); ll y = st.back(); st.pop_back(); st.back() += y; } ll x = st.back(); if (opt.find(x) == opt.end()) continue; if (opt[x].size() > seq.size()) { cout<<"Improved "<<x<<" from "<<opt[x].size()<<" to "<<seq.size()<<endl; opt[x] = seq; ofstream out("new_out.txt"); for (int i = 0; i < t; i++) { out<<opt[xs[i]].size()<<endl; for (string s : opt[xs[i]]) { out<<s<<" "; } out<<endl; } out.close(); } } } |