/* * author: pavveu * task: Potyczki Algorytmiczne 2020 Runda 2 - Elektrownie i fabryki [B] */ #include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; using vll = vector<ll>; #define FOR(iter, val) for(int iter = 0; iter < (val); iter++) #define all(x) begin(x), end(x) vi generate_array(int n, int bound) { vector<int> a; for (int i = 0; i < n; i++) { int l = rand() % (2 * bound + 1) - bound; a.push_back(l); } return a; } int brute(const vi& a) { int n = a.size(); int ans { n }; int best_mask = -1; for (int mask = 0; mask < (1<<(n - 1)); mask++) { bool valid = true; ll prefix_sum { a[0] }; for (int i = 0; i < n - 1; i++) { if ( !(mask & (1<<i)) ) { if ( prefix_sum < 0 ) valid = false; prefix_sum = 0; } prefix_sum += a[i + 1]; } if ( prefix_sum < 0 ) valid = false; if ( valid ) { int cost { __builtin_popcount(mask) }; if ( ans > cost ) { ans = cost; best_mask = mask; } } } for (int i = 0; i < n - 1; i++) { if ( best_mask & (1<<i) ) cout << "-"; else cout << "."; } cout << endl; return (ans == n ? -1 : ans); } int solution(const vi& a) { ll queue_sum { accumulate(all(a), 0ll) }; if ( queue_sum < 0ll ) { return -1; } // next number in a pipe of a queue<int> q; // sum for every created segment stack<int> s; for (int x : a) q.push(x); // inf > a[i] const int inf = 1e9 + 1000; // wardens q.push(-inf); s.push(-inf); ll stack_sum { 0 }; int cost { 0 }; while ( q.size() > 1 ) { ll current { q.front() }; queue_sum -= q.front(); q.pop(); while ( current < 0 ) { cost++; if ( stack_sum + current >= 0 && queue_sum + current >= 0 ) { if ( s.top() > q.front() ) { // cerr << "stack wins with: " << s.top() << endl; current += s.top(); stack_sum -= s.top(); s.pop(); } else { // cerr << "queue wins with: " << q.front() << endl; current += q.front(); queue_sum -= q.front(); q.pop(); } } else { if ( stack_sum + current >= 0 ) { // cerr << "only stack valid" << endl; current += s.top(); stack_sum -= s.top(); s.pop(); } else if ( queue_sum + current >= 0 ) { // cerr << "only queue valid" << endl; current += q.front(); queue_sum -= q.front();; q.pop(); } else { assert("No solution?"); } } } s.push(current); stack_sum += current; // cerr << "stack sum: " << stack_sum << endl; } // stack contains warden, so size - 1 int segments_left = s.size() - 1; int max_links = a.size() - 1; int links_left { segments_left - 1 }; return max_links - links_left; } void test(int times = 1) { srand(time(NULL)); FOR(t, times) { vi a { generate_array(10, 10) }; int sol { solution(a) }; int br { brute(a) }; reverse(all(a)); int sol2 { solution(a) }; reverse(all(a)); if ( sol != br || sol2 != br ) { cout << "ERROR" << endl; cout << "SOL: " << sol << endl; cout << "SOL2: " << sol2 << endl; cout << "BRUTE: " << br << endl; cout <<"A: "; for (int x : a ) { cout << x << " "; } cout << endl; break; } else { cout << "OK.\n"; } } } void go() { int n; cin >> n; vi a(n); for (auto& i : a) cin >> i; int sol1 { solution(a) }; reverse(all(a)); int sol2 { solution(a) }; cout << std::min(sol1, sol2) << endl; } int main() { ios::sync_with_stdio(false); cin.tie(0); go(); // test(100); 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 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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | /* * author: pavveu * task: Potyczki Algorytmiczne 2020 Runda 2 - Elektrownie i fabryki [B] */ #include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; using vll = vector<ll>; #define FOR(iter, val) for(int iter = 0; iter < (val); iter++) #define all(x) begin(x), end(x) vi generate_array(int n, int bound) { vector<int> a; for (int i = 0; i < n; i++) { int l = rand() % (2 * bound + 1) - bound; a.push_back(l); } return a; } int brute(const vi& a) { int n = a.size(); int ans { n }; int best_mask = -1; for (int mask = 0; mask < (1<<(n - 1)); mask++) { bool valid = true; ll prefix_sum { a[0] }; for (int i = 0; i < n - 1; i++) { if ( !(mask & (1<<i)) ) { if ( prefix_sum < 0 ) valid = false; prefix_sum = 0; } prefix_sum += a[i + 1]; } if ( prefix_sum < 0 ) valid = false; if ( valid ) { int cost { __builtin_popcount(mask) }; if ( ans > cost ) { ans = cost; best_mask = mask; } } } for (int i = 0; i < n - 1; i++) { if ( best_mask & (1<<i) ) cout << "-"; else cout << "."; } cout << endl; return (ans == n ? -1 : ans); } int solution(const vi& a) { ll queue_sum { accumulate(all(a), 0ll) }; if ( queue_sum < 0ll ) { return -1; } // next number in a pipe of a queue<int> q; // sum for every created segment stack<int> s; for (int x : a) q.push(x); // inf > a[i] const int inf = 1e9 + 1000; // wardens q.push(-inf); s.push(-inf); ll stack_sum { 0 }; int cost { 0 }; while ( q.size() > 1 ) { ll current { q.front() }; queue_sum -= q.front(); q.pop(); while ( current < 0 ) { cost++; if ( stack_sum + current >= 0 && queue_sum + current >= 0 ) { if ( s.top() > q.front() ) { // cerr << "stack wins with: " << s.top() << endl; current += s.top(); stack_sum -= s.top(); s.pop(); } else { // cerr << "queue wins with: " << q.front() << endl; current += q.front(); queue_sum -= q.front(); q.pop(); } } else { if ( stack_sum + current >= 0 ) { // cerr << "only stack valid" << endl; current += s.top(); stack_sum -= s.top(); s.pop(); } else if ( queue_sum + current >= 0 ) { // cerr << "only queue valid" << endl; current += q.front(); queue_sum -= q.front();; q.pop(); } else { assert("No solution?"); } } } s.push(current); stack_sum += current; // cerr << "stack sum: " << stack_sum << endl; } // stack contains warden, so size - 1 int segments_left = s.size() - 1; int max_links = a.size() - 1; int links_left { segments_left - 1 }; return max_links - links_left; } void test(int times = 1) { srand(time(NULL)); FOR(t, times) { vi a { generate_array(10, 10) }; int sol { solution(a) }; int br { brute(a) }; reverse(all(a)); int sol2 { solution(a) }; reverse(all(a)); if ( sol != br || sol2 != br ) { cout << "ERROR" << endl; cout << "SOL: " << sol << endl; cout << "SOL2: " << sol2 << endl; cout << "BRUTE: " << br << endl; cout <<"A: "; for (int x : a ) { cout << x << " "; } cout << endl; break; } else { cout << "OK.\n"; } } } void go() { int n; cin >> n; vi a(n); for (auto& i : a) cin >> i; int sol1 { solution(a) }; reverse(all(a)); int sol2 { solution(a) }; cout << std::min(sol1, sol2) << endl; } int main() { ios::sync_with_stdio(false); cin.tie(0); go(); // test(100); return 0; } |