#include <iostream> #include <vector> #include <algorithm> using namespace std; bool do_test_case() { int n; cin >> n; int w[n]; for(int i = 0; i < n; i++) cin >> w[i]; int first = 0; while(w[first] == 0) first++; int last = n-1; while(w[last] == 0) last--; int bilans = 0; bool przewaga_parzystych = false; bool przewaga_nieparzystych = false; for(int i = first; i<last; i++) { if( i % 2 == 0) { bilans += w[i]; if(bilans < 0 || (bilans==0 && przewaga_parzystych)) return false; if(bilans ==0) przewaga_nieparzystych = true; } else { bilans -= w[i]; if(bilans > 0 || (bilans==0 && przewaga_nieparzystych)) return false; if(bilans ==0) przewaga_parzystych = true; } } if( last % 2 == 0) { bilans += w[last]; } else { bilans -= w[last]; } return (bilans == 0 || (bilans == 1 && !przewaga_nieparzystych) || (bilans == -1 && !przewaga_parzystych) ); } int main() { int t; cin >> t; for(int i = 0; i < t; i++) { if(do_test_case()) cout << "TAK\n"; else cout << "NIE\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 | #include <iostream> #include <vector> #include <algorithm> using namespace std; bool do_test_case() { int n; cin >> n; int w[n]; for(int i = 0; i < n; i++) cin >> w[i]; int first = 0; while(w[first] == 0) first++; int last = n-1; while(w[last] == 0) last--; int bilans = 0; bool przewaga_parzystych = false; bool przewaga_nieparzystych = false; for(int i = first; i<last; i++) { if( i % 2 == 0) { bilans += w[i]; if(bilans < 0 || (bilans==0 && przewaga_parzystych)) return false; if(bilans ==0) przewaga_nieparzystych = true; } else { bilans -= w[i]; if(bilans > 0 || (bilans==0 && przewaga_nieparzystych)) return false; if(bilans ==0) przewaga_parzystych = true; } } if( last % 2 == 0) { bilans += w[last]; } else { bilans -= w[last]; } return (bilans == 0 || (bilans == 1 && !przewaga_nieparzystych) || (bilans == -1 && !przewaga_parzystych) ); } int main() { int t; cin >> t; for(int i = 0; i < t; i++) { if(do_test_case()) cout << "TAK\n"; else cout << "NIE\n"; } return 0; } |