// Marcin Knapik // Potyczki Algorytmiczne 2020 - runda druga zadanie B #include<bits/stdc++.h> using namespace std; #define f first #define s second #define sz(s) (int)s.size() #define all(s) s.begin(), s.end() #define pb push_back #define vi vector<int> #define ld long long #define ll long long #define ii pair<int, int> #define vi vector<int> #define vii vector<ii> #define vvi vector<vi> const ll INF = 1e18; const int inf = 1e9; int main(){ ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vi tab(n); int ostatnia_ujemna = -1; for(int i = 0; i < n; i++){ cin >> tab[i]; if(tab[i] < 0) ostatnia_ujemna = i; } if(ostatnia_ujemna == -1){ cout << 0 << '\n'; exit(0); } set<pair<ll, int> > secik = { {-INF, inf}, {0ll, 0ll} }; ll suma = 0; int ile = 0; int ans = n - 1; int best_do_tej_pory = 0; for(int i = 0; i < n; i++){ ile++; suma += tab[i]; if(tab[i] < 0) best_do_tej_pory = inf; auto it = secik.upper_bound({suma, inf}); assert(it != secik.begin()); it = prev(it); if(it == secik.begin()) continue; best_do_tej_pory = min(best_do_tej_pory, (*it).s + ile - 1); if(i >= ostatnia_ujemna) ans = min(ans, best_do_tej_pory); while(it != prev(secik.end()) and (*next(it)).s >= best_do_tej_pory - ile) secik.erase(next(it)); if(best_do_tej_pory - ile < (*it).s){ if((*it).f == suma) secik.erase(it); secik.insert({suma, best_do_tej_pory - ile}); } } cout << ans << '\n'; }
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 | // Marcin Knapik // Potyczki Algorytmiczne 2020 - runda druga zadanie B #include<bits/stdc++.h> using namespace std; #define f first #define s second #define sz(s) (int)s.size() #define all(s) s.begin(), s.end() #define pb push_back #define vi vector<int> #define ld long long #define ll long long #define ii pair<int, int> #define vi vector<int> #define vii vector<ii> #define vvi vector<vi> const ll INF = 1e18; const int inf = 1e9; int main(){ ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vi tab(n); int ostatnia_ujemna = -1; for(int i = 0; i < n; i++){ cin >> tab[i]; if(tab[i] < 0) ostatnia_ujemna = i; } if(ostatnia_ujemna == -1){ cout << 0 << '\n'; exit(0); } set<pair<ll, int> > secik = { {-INF, inf}, {0ll, 0ll} }; ll suma = 0; int ile = 0; int ans = n - 1; int best_do_tej_pory = 0; for(int i = 0; i < n; i++){ ile++; suma += tab[i]; if(tab[i] < 0) best_do_tej_pory = inf; auto it = secik.upper_bound({suma, inf}); assert(it != secik.begin()); it = prev(it); if(it == secik.begin()) continue; best_do_tej_pory = min(best_do_tej_pory, (*it).s + ile - 1); if(i >= ostatnia_ujemna) ans = min(ans, best_do_tej_pory); while(it != prev(secik.end()) and (*next(it)).s >= best_do_tej_pory - ile) secik.erase(next(it)); if(best_do_tej_pory - ile < (*it).s){ if((*it).f == suma) secik.erase(it); secik.insert({suma, best_do_tej_pory - ile}); } } cout << ans << '\n'; } |