//#pragma GCC optimize("Ofast") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; //~ while (clock()<=69*CLOCKS_PER_SEC) #define ll long long #define ld long double #define pi pair<int,int> #define pd pair<ld,ld> #define ft first #define sd second #define pb push_back #define eb emplace_back #define FOR(i,a,b) for(int i=(a); i<=(b);i++) #define F(i,a,b) FOR(i,(a),(b)-1) #define REV(i,a,b) for(int i=(a); i>=(b);i--) #define VI vector<int> #define VPI vector<pi> #define VPD vector<pd> #define PI 3.14159265 #define all(x) (x).begin(), (x).end() #define sz(a) (int)((a).size()) #define int long long template<class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<"\n";} template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while(*sdbg!=',')cerr<<*sdbg++;cerr<<"="<<h<<","; _dbg(sdbg+1, a...); } #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); const long long K = 1000; const long long KK = K*K; const long long MIL = KK*K; const long long INF = MIL*MIL; const long long MOD = 1e9 + 7; const long long N = 10, M=310; const int base = 1 << 19; int drz[2 * base]; int szukaj(int a, int b, int p, int k, int x) { if (a <= p && k <= b) return drz[x]; if (k < a || p > b) return INF; int sr = (p + k) / 2; return min(szukaj(a, b, p, sr, 2 * x), szukaj(a, b, sr + 1, k, 2 * x + 1)); } void update(int x, int v) { x += base; drz[x] = v; while(x /= 2) { drz[x] = min(drz[2 * x], drz[2 * x + 1]); } } void solve() { int n; cin >> n; VI res(n + 1, INF), v(n + 1); F(i, 0, 2 * base) drz[i] = INF; VI agg_v(1, 0); int agg = 0; res[0] = 0; FOR(i, 1, n) { cin >> v[i]; agg += v[i]; agg_v.eb(agg); } sort(all(agg_v)); agg_v.resize(unique(all(agg_v)) - agg_v.begin()); agg = 0; int zero_id = lower_bound(all(agg_v), 0) - agg_v.begin(); update(zero_id, 0); FOR(i, 1, n) { agg += v[i]; int agg_id = lower_bound(all(agg_v), agg) - agg_v.begin(); if (agg_id < zero_id) { continue; } int wcz = szukaj(0, agg_id, 0, base - 1, 1); if (wcz != INF) { res[i] = i + wcz - 1; } update(agg_id, res[i] - i); } if (agg < 0) { cout << "-1\n"; } else { int res_int = INF; REV(i, n, 1) { res_int = min(res_int, res[i]); if (v[i] < 0) break; } cout << res_int << "\n"; } } int32_t main() { // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); ios_base::sync_with_stdio(0); cin.tie(0); // cout.tie(0); cerr.tie(0); cout << setprecision(9) << fixed; cerr << setprecision(6) << fixed; int test = 1, f; // cin >> test; F(_test, 0, test) { //cout<<"Case #"<<_test + 1<<": "; solve(); // if(_test == 1) // return 0; } } /* 5 2 -2 1 -2 4 */
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 | //#pragma GCC optimize("Ofast") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; //~ while (clock()<=69*CLOCKS_PER_SEC) #define ll long long #define ld long double #define pi pair<int,int> #define pd pair<ld,ld> #define ft first #define sd second #define pb push_back #define eb emplace_back #define FOR(i,a,b) for(int i=(a); i<=(b);i++) #define F(i,a,b) FOR(i,(a),(b)-1) #define REV(i,a,b) for(int i=(a); i>=(b);i--) #define VI vector<int> #define VPI vector<pi> #define VPD vector<pd> #define PI 3.14159265 #define all(x) (x).begin(), (x).end() #define sz(a) (int)((a).size()) #define int long long template<class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<"\n";} template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while(*sdbg!=',')cerr<<*sdbg++;cerr<<"="<<h<<","; _dbg(sdbg+1, a...); } #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); const long long K = 1000; const long long KK = K*K; const long long MIL = KK*K; const long long INF = MIL*MIL; const long long MOD = 1e9 + 7; const long long N = 10, M=310; const int base = 1 << 19; int drz[2 * base]; int szukaj(int a, int b, int p, int k, int x) { if (a <= p && k <= b) return drz[x]; if (k < a || p > b) return INF; int sr = (p + k) / 2; return min(szukaj(a, b, p, sr, 2 * x), szukaj(a, b, sr + 1, k, 2 * x + 1)); } void update(int x, int v) { x += base; drz[x] = v; while(x /= 2) { drz[x] = min(drz[2 * x], drz[2 * x + 1]); } } void solve() { int n; cin >> n; VI res(n + 1, INF), v(n + 1); F(i, 0, 2 * base) drz[i] = INF; VI agg_v(1, 0); int agg = 0; res[0] = 0; FOR(i, 1, n) { cin >> v[i]; agg += v[i]; agg_v.eb(agg); } sort(all(agg_v)); agg_v.resize(unique(all(agg_v)) - agg_v.begin()); agg = 0; int zero_id = lower_bound(all(agg_v), 0) - agg_v.begin(); update(zero_id, 0); FOR(i, 1, n) { agg += v[i]; int agg_id = lower_bound(all(agg_v), agg) - agg_v.begin(); if (agg_id < zero_id) { continue; } int wcz = szukaj(0, agg_id, 0, base - 1, 1); if (wcz != INF) { res[i] = i + wcz - 1; } update(agg_id, res[i] - i); } if (agg < 0) { cout << "-1\n"; } else { int res_int = INF; REV(i, n, 1) { res_int = min(res_int, res[i]); if (v[i] < 0) break; } cout << res_int << "\n"; } } int32_t main() { // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); ios_base::sync_with_stdio(0); cin.tie(0); // cout.tie(0); cerr.tie(0); cout << setprecision(9) << fixed; cerr << setprecision(6) << fixed; int test = 1, f; // cin >> test; F(_test, 0, test) { //cout<<"Case #"<<_test + 1<<": "; solve(); // if(_test == 1) // return 0; } } /* 5 2 -2 1 -2 4 */ |