// PA2025 runda 2B - https://sio2.mimuw.edu.pl/c/pa-2025-1/p/wyl/ //-std=c++20 #include<iostream> #include <algorithm> #include <vector> #include<list> #include <stack> using namespace std; #define MIN(X, Y) ((X) < (Y) ? (X) : (Y)) struct frame { frame(const frame &other) = default; frame(u_int8_t ends, u_int32_t forwards) : ends(ends), forwards(forwards) {} u_int8_t ends; u_int32_t forwards; }; bool solve(list<u_int32_t> &toys) { auto it = toys.begin(); // gwarantowane że jest co najmniej 1 stack<frame> history; history.push(frame(0, (*it) * 2)); while(true){ it++; bool move_back = false; if (it == toys.end()) { if (history.top().forwards == 0 && history.top().ends == 2) { return true; } else { move_back = true;// stan niepoprawny, } } else if (history.top().forwards == 0) { move_back = true;// stan niepoprawny, nie ma poloczenia dla kolejnych wezlow } else if ((*it) * 2 >= history.top().forwards) { history.push(frame(history.top().ends, (*it) * 2 - history.top().forwards)); } else { move_back = true;// stan niepoprawny, brakuje polaczen w kolejnym wezle } if (move_back) { do { it--; history.top().ends++; history.top().forwards--; move_back = history.top().ends > 2; if (move_back) { history.pop(); } } while (!history.empty() && move_back); if (history.empty()) { return false; } } } } void subtask() { u_int32_t n; cin >> n; list<u_int32_t> toys; toys.resize(n); for (auto &t: toys) { cin >> t; } //zera z przodu niepotrzebne auto it = toys.begin(); // zagwarantowane że 1 chociaż zostanie while (*it == 0) it = toys.erase(it); it = toys.end(); while (*(--it) == 0) it = toys.erase(it); for (auto &x: toys) { if (x == 0) { cout << "NIE\n"; return; } } auto res = solve(toys); if (res) { cout << "TAK\n"; } else { cout << "NIE\n"; } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); u_int32_t t; cin >> t; for (int i = 0; i < t; ++i) { subtask(); } }
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 | // PA2025 runda 2B - https://sio2.mimuw.edu.pl/c/pa-2025-1/p/wyl/ //-std=c++20 #include<iostream> #include <algorithm> #include <vector> #include<list> #include <stack> using namespace std; #define MIN(X, Y) ((X) < (Y) ? (X) : (Y)) struct frame { frame(const frame &other) = default; frame(u_int8_t ends, u_int32_t forwards) : ends(ends), forwards(forwards) {} u_int8_t ends; u_int32_t forwards; }; bool solve(list<u_int32_t> &toys) { auto it = toys.begin(); // gwarantowane że jest co najmniej 1 stack<frame> history; history.push(frame(0, (*it) * 2)); while(true){ it++; bool move_back = false; if (it == toys.end()) { if (history.top().forwards == 0 && history.top().ends == 2) { return true; } else { move_back = true;// stan niepoprawny, } } else if (history.top().forwards == 0) { move_back = true;// stan niepoprawny, nie ma poloczenia dla kolejnych wezlow } else if ((*it) * 2 >= history.top().forwards) { history.push(frame(history.top().ends, (*it) * 2 - history.top().forwards)); } else { move_back = true;// stan niepoprawny, brakuje polaczen w kolejnym wezle } if (move_back) { do { it--; history.top().ends++; history.top().forwards--; move_back = history.top().ends > 2; if (move_back) { history.pop(); } } while (!history.empty() && move_back); if (history.empty()) { return false; } } } } void subtask() { u_int32_t n; cin >> n; list<u_int32_t> toys; toys.resize(n); for (auto &t: toys) { cin >> t; } //zera z przodu niepotrzebne auto it = toys.begin(); // zagwarantowane że 1 chociaż zostanie while (*it == 0) it = toys.erase(it); it = toys.end(); while (*(--it) == 0) it = toys.erase(it); for (auto &x: toys) { if (x == 0) { cout << "NIE\n"; return; } } auto res = solve(toys); if (res) { cout << "TAK\n"; } else { cout << "NIE\n"; } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); u_int32_t t; cin >> t; for (int i = 0; i < t; ++i) { subtask(); } } |