#include <bits/stdc++.h>
using namespace std;
#define u_map unordered_map
#define u_set unordered_set
#define u_multiset unordered_multiset
using ll = long long;
using vvi = vector<vector<int>>;
using vi = vector<int>;
using vvll = vector<vector<long long>>;
using vll = vector<long long>;
using vd = vector<double>;
using vvd = vector<vector<double>>;
using pii = pair<int, int>;
using vpii = vector<pair<int, int>>;
template<typename C> struct rge{C l, r;};
template<typename C> rge<C> range(C i, C j) { return rge<C>{i, j}; }
template<typename C> ostream& operator<<(ostream &os, rge<C> &r) { os << '{'; for(auto it = r.l; it != r.r; it++) os << "," + (it == r.l) << *it; os << '}'; return os; }
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '{' << p.first << "," << p.second << '}'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ","; return os << '}'; }
void dbg_out() { cerr << ']' << endl; }
template<typename A> void dbg_out(A H) { cerr << H; dbg_out(); }
template<typename A, typename B, typename... C> void dbg_out(A H, B G, C... T) { cerr << H << ","; dbg_out(G, T...); }
#ifdef DEBUG
#define debug(...) cerr << "[" << #__VA_ARGS__ << "] = [", dbg_out(__VA_ARGS__)
#else
#define debug(...)
#endif
const ll INF = 1e18;
struct Node {
Node *l, *r;
int lo, hi;
ll vmin;
void update(){
vmin = min(l->vmin, r->vmin);
}
Node(int lo, int hi, ll id=INF) : lo(lo), hi(hi){
if(lo + 1 == hi){
vmin = id;
} else {
int mid = lo + (hi - lo) / 2;
l = new Node(lo, mid, id);
r = new Node(mid, hi, id);
update();
}
}
// vals[i] = x
void set(int i, ll x){
if(i < lo || hi <= i){
return;
}
if(lo + 1 == hi){
vmin = x;
} else {
l->set(i, x);
r->set(i, x);
update();
}
}
// greatest i such that vals[i] <= d
// -1 otherwise
int query(ll d) {
if(vmin > d) return -1;
if(lo + 1 == hi) return lo;
if(r->vmin <= d) return r->query(d);
else return l->query(d);
}
};
void solve(){
int n;
cin >> n;
vll v(n);
for(int i = 0; i < n; i++) cin >> v[i];
vll pref(n+1);
for(int i = 1; i <= n; i++) pref[i] = pref[i-1] + v[i-1];
Node* st = new Node(0, n+1);
st->set(0, 0);
vi dp(n+1, 1e9);
dp[0] = 0;
for(int i = 1; i <= n; i++){
int idx = st->query(pref[i]);
dp[i] = dp[idx] + 1;
debug(i, idx, pref[i] - pref[idx]);
st->set(i, 2LL * pref[i] - pref[idx]);
}
debug(dp);
cout << dp[n] << endl;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
388 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
388 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
3 ms |
596 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
3 ms |
596 KB |
Output is correct |
34 |
Correct |
3 ms |
596 KB |
Output is correct |
35 |
Correct |
2 ms |
596 KB |
Output is correct |
36 |
Correct |
2 ms |
656 KB |
Output is correct |
37 |
Correct |
2 ms |
596 KB |
Output is correct |
38 |
Correct |
2 ms |
468 KB |
Output is correct |
39 |
Correct |
1 ms |
596 KB |
Output is correct |
40 |
Correct |
2 ms |
596 KB |
Output is correct |
41 |
Correct |
1 ms |
596 KB |
Output is correct |
42 |
Correct |
2 ms |
660 KB |
Output is correct |
43 |
Correct |
2 ms |
596 KB |
Output is correct |
44 |
Correct |
2 ms |
596 KB |
Output is correct |
45 |
Correct |
2 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
388 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
3 ms |
596 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
3 ms |
596 KB |
Output is correct |
34 |
Correct |
3 ms |
596 KB |
Output is correct |
35 |
Correct |
2 ms |
596 KB |
Output is correct |
36 |
Correct |
2 ms |
656 KB |
Output is correct |
37 |
Correct |
2 ms |
596 KB |
Output is correct |
38 |
Correct |
2 ms |
468 KB |
Output is correct |
39 |
Correct |
1 ms |
596 KB |
Output is correct |
40 |
Correct |
2 ms |
596 KB |
Output is correct |
41 |
Correct |
1 ms |
596 KB |
Output is correct |
42 |
Correct |
2 ms |
660 KB |
Output is correct |
43 |
Correct |
2 ms |
596 KB |
Output is correct |
44 |
Correct |
2 ms |
596 KB |
Output is correct |
45 |
Correct |
2 ms |
596 KB |
Output is correct |
46 |
Correct |
42 ms |
11676 KB |
Output is correct |
47 |
Correct |
35 ms |
12140 KB |
Output is correct |
48 |
Correct |
50 ms |
12480 KB |
Output is correct |
49 |
Correct |
43 ms |
12600 KB |
Output is correct |
50 |
Correct |
44 ms |
12652 KB |
Output is correct |
51 |
Correct |
52 ms |
12608 KB |
Output is correct |
52 |
Correct |
23 ms |
6436 KB |
Output is correct |
53 |
Correct |
12 ms |
3924 KB |
Output is correct |
54 |
Correct |
30 ms |
11860 KB |
Output is correct |
55 |
Correct |
39 ms |
12656 KB |
Output is correct |
56 |
Correct |
44 ms |
12140 KB |
Output is correct |
57 |
Correct |
35 ms |
12084 KB |
Output is correct |
58 |
Correct |
49 ms |
12116 KB |
Output is correct |
59 |
Correct |
35 ms |
12224 KB |
Output is correct |
60 |
Correct |
48 ms |
12180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
388 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
3 ms |
596 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
3 ms |
596 KB |
Output is correct |
34 |
Correct |
3 ms |
596 KB |
Output is correct |
35 |
Correct |
2 ms |
596 KB |
Output is correct |
36 |
Correct |
2 ms |
656 KB |
Output is correct |
37 |
Correct |
2 ms |
596 KB |
Output is correct |
38 |
Correct |
2 ms |
468 KB |
Output is correct |
39 |
Correct |
1 ms |
596 KB |
Output is correct |
40 |
Correct |
2 ms |
596 KB |
Output is correct |
41 |
Correct |
1 ms |
596 KB |
Output is correct |
42 |
Correct |
2 ms |
660 KB |
Output is correct |
43 |
Correct |
2 ms |
596 KB |
Output is correct |
44 |
Correct |
2 ms |
596 KB |
Output is correct |
45 |
Correct |
2 ms |
596 KB |
Output is correct |
46 |
Correct |
42 ms |
11676 KB |
Output is correct |
47 |
Correct |
35 ms |
12140 KB |
Output is correct |
48 |
Correct |
50 ms |
12480 KB |
Output is correct |
49 |
Correct |
43 ms |
12600 KB |
Output is correct |
50 |
Correct |
44 ms |
12652 KB |
Output is correct |
51 |
Correct |
52 ms |
12608 KB |
Output is correct |
52 |
Correct |
23 ms |
6436 KB |
Output is correct |
53 |
Correct |
12 ms |
3924 KB |
Output is correct |
54 |
Correct |
30 ms |
11860 KB |
Output is correct |
55 |
Correct |
39 ms |
12656 KB |
Output is correct |
56 |
Correct |
44 ms |
12140 KB |
Output is correct |
57 |
Correct |
35 ms |
12084 KB |
Output is correct |
58 |
Correct |
49 ms |
12116 KB |
Output is correct |
59 |
Correct |
35 ms |
12224 KB |
Output is correct |
60 |
Correct |
48 ms |
12180 KB |
Output is correct |
61 |
Correct |
197 ms |
59020 KB |
Output is correct |
62 |
Correct |
221 ms |
59028 KB |
Output is correct |
63 |
Correct |
213 ms |
59420 KB |
Output is correct |
64 |
Correct |
192 ms |
61612 KB |
Output is correct |
65 |
Correct |
238 ms |
61952 KB |
Output is correct |
66 |
Correct |
223 ms |
61904 KB |
Output is correct |
67 |
Correct |
163 ms |
49584 KB |
Output is correct |
68 |
Correct |
80 ms |
24952 KB |
Output is correct |
69 |
Correct |
236 ms |
58116 KB |
Output is correct |
70 |
Correct |
214 ms |
61952 KB |
Output is correct |
71 |
Correct |
232 ms |
59008 KB |
Output is correct |
72 |
Correct |
192 ms |
59404 KB |
Output is correct |
73 |
Correct |
197 ms |
60016 KB |
Output is correct |
74 |
Correct |
206 ms |
60384 KB |
Output is correct |
75 |
Correct |
332 ms |
60388 KB |
Output is correct |