#include <bits/stdc++.h>
using namespace std;
struct segTree {
vector<long long> v, upd;
int sz;
void init(int n){
sz = 1;
while(sz < n) sz *= 2;
v.assign(2 * sz, 1e18);
upd.assign(2 * sz, 0);
}
void set(int l, int r, long long val, int x, int lx, int rx){
if(lx >= l && rx <= r){
if(v[x] == 1e18) v[x] = 0;
v[x] += val;
upd[x] += val;
// cout << "lx " << lx << " rx " << rx <<
return;
}else if(lx >= r || rx <= l) return;
int m = (lx + rx) / 2;
set(l, r, val, 2 * x + 1, lx, m);
set(l, r, val, 2 * x + 2, m, rx);
v[x] = min(v[2 * x + 1], v[2 * x + 2]) + upd[x];
}
void set(int l, int r, long long val){
set(l, r, val, 0, 0, sz);
}
int walk(int x, int lx, int rx, long long curr){
if(rx - lx == 1){
return lx;
}
int m = (lx + rx) / 2;
curr += upd[x];
if(v[2 * x + 2] + curr <= 0) return walk(2 * x + 2, m, rx, curr);
else return walk(2 * x + 1, lx, m, curr);
}
int walk(){
return walk(0, 0, sz, 0);
}
};
long long sum(int l, int r, vector<long long>& prefix){
if(l > r) return 0;
return prefix[r] - prefix[l-1];
}
int solve(int N, vector<int>& a){
vector<long long> prefix(N+1, 0);
for(int i = 1; i <= N; i++){
prefix[i] = prefix[i-1] + a[i];
}
vector<int> ans(N+1);
ans[0] = 0;
segTree st; st.init(N+1);
st.set(0, 1, 0);
for(int i = 1; i <= N; i++){
st.set(0, i, -a[i]);
int ind = st.walk();
ans[i] = ans[ind] + 1;
long long sm = sum(ind + 1, i, prefix);
// cout << "i " << i << " ind " << ind << " sm " << sm << "\n";
st.set(i, i + 1, sm);
}
return ans[N];
}
int brute(int N, vector<int>& a){
int ans = 0;
for(int b = 0; b < (1 << N); b++){
long long lst = 0;
long long sm = 0;
bool gd = true;
int segments = 0;
// cout << "b " << b << "\n";
for(int i = 0; i < N; i++){
sm += a[i+1];
if((1 << i) & b || i == N - 1){
// cout << "i " <<
segments++;
if(lst > sm) gd = false;
lst = sm;
sm = 0;
}
}
if(gd) ans = max(ans, segments);
}
return ans;
}
mt19937 gen(time(0));
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
// string s; cin >> s;
string s = "RUN";
if(s == "RUN"){
int N; cin >> N;
vector<int> a(N+1, 0);
for(int i = 1; i <= N; i++) cin >> a[i];
cout << solve(N, a) << "\n";
return 0;
}
for(int h = 0; h < 1000000; h++){
int n = gen() % 10;
vector<int> a(n+1, 0);
for(int i = 0; i < n; i++){
a[i] = gen() % 10 + 1;
}
int ans1 = solve(n, a);
int ans2 = brute(n, a);
if(ans1 != ans2){
cout << n << "\n";
for(int i = 1; i <= n; i++) cout << a[i] << " ";
cout << "\n";
cout << "Fast ans: " << ans1 << "\n";
cout << "Slow ans: " << ans2 << "\n";
return 0;
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
428 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
428 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
484 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
428 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
484 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
604 KB |
Output is correct |
36 |
Correct |
1 ms |
604 KB |
Output is correct |
37 |
Correct |
1 ms |
496 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
604 KB |
Output is correct |
40 |
Correct |
1 ms |
468 KB |
Output is correct |
41 |
Correct |
1 ms |
600 KB |
Output is correct |
42 |
Correct |
1 ms |
644 KB |
Output is correct |
43 |
Correct |
1 ms |
604 KB |
Output is correct |
44 |
Correct |
1 ms |
604 KB |
Output is correct |
45 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
428 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
484 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
604 KB |
Output is correct |
36 |
Correct |
1 ms |
604 KB |
Output is correct |
37 |
Correct |
1 ms |
496 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
604 KB |
Output is correct |
40 |
Correct |
1 ms |
468 KB |
Output is correct |
41 |
Correct |
1 ms |
600 KB |
Output is correct |
42 |
Correct |
1 ms |
644 KB |
Output is correct |
43 |
Correct |
1 ms |
604 KB |
Output is correct |
44 |
Correct |
1 ms |
604 KB |
Output is correct |
45 |
Correct |
1 ms |
604 KB |
Output is correct |
46 |
Correct |
38 ms |
6520 KB |
Output is correct |
47 |
Correct |
34 ms |
6492 KB |
Output is correct |
48 |
Correct |
35 ms |
6740 KB |
Output is correct |
49 |
Correct |
37 ms |
7004 KB |
Output is correct |
50 |
Correct |
37 ms |
7004 KB |
Output is correct |
51 |
Correct |
37 ms |
7004 KB |
Output is correct |
52 |
Correct |
18 ms |
3676 KB |
Output is correct |
53 |
Correct |
11 ms |
2136 KB |
Output is correct |
54 |
Correct |
33 ms |
6236 KB |
Output is correct |
55 |
Correct |
42 ms |
7004 KB |
Output is correct |
56 |
Correct |
35 ms |
6604 KB |
Output is correct |
57 |
Correct |
33 ms |
6488 KB |
Output is correct |
58 |
Correct |
34 ms |
6488 KB |
Output is correct |
59 |
Correct |
36 ms |
6492 KB |
Output is correct |
60 |
Correct |
36 ms |
6748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
428 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
484 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
604 KB |
Output is correct |
36 |
Correct |
1 ms |
604 KB |
Output is correct |
37 |
Correct |
1 ms |
496 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
604 KB |
Output is correct |
40 |
Correct |
1 ms |
468 KB |
Output is correct |
41 |
Correct |
1 ms |
600 KB |
Output is correct |
42 |
Correct |
1 ms |
644 KB |
Output is correct |
43 |
Correct |
1 ms |
604 KB |
Output is correct |
44 |
Correct |
1 ms |
604 KB |
Output is correct |
45 |
Correct |
1 ms |
604 KB |
Output is correct |
46 |
Correct |
38 ms |
6520 KB |
Output is correct |
47 |
Correct |
34 ms |
6492 KB |
Output is correct |
48 |
Correct |
35 ms |
6740 KB |
Output is correct |
49 |
Correct |
37 ms |
7004 KB |
Output is correct |
50 |
Correct |
37 ms |
7004 KB |
Output is correct |
51 |
Correct |
37 ms |
7004 KB |
Output is correct |
52 |
Correct |
18 ms |
3676 KB |
Output is correct |
53 |
Correct |
11 ms |
2136 KB |
Output is correct |
54 |
Correct |
33 ms |
6236 KB |
Output is correct |
55 |
Correct |
42 ms |
7004 KB |
Output is correct |
56 |
Correct |
35 ms |
6604 KB |
Output is correct |
57 |
Correct |
33 ms |
6488 KB |
Output is correct |
58 |
Correct |
34 ms |
6488 KB |
Output is correct |
59 |
Correct |
36 ms |
6492 KB |
Output is correct |
60 |
Correct |
36 ms |
6748 KB |
Output is correct |
61 |
Correct |
168 ms |
26448 KB |
Output is correct |
62 |
Correct |
173 ms |
26596 KB |
Output is correct |
63 |
Correct |
183 ms |
27056 KB |
Output is correct |
64 |
Correct |
189 ms |
29012 KB |
Output is correct |
65 |
Correct |
194 ms |
29524 KB |
Output is correct |
66 |
Correct |
191 ms |
29412 KB |
Output is correct |
67 |
Correct |
154 ms |
26960 KB |
Output is correct |
68 |
Correct |
77 ms |
13728 KB |
Output is correct |
69 |
Correct |
188 ms |
25756 KB |
Output is correct |
70 |
Correct |
204 ms |
29624 KB |
Output is correct |
71 |
Correct |
180 ms |
26452 KB |
Output is correct |
72 |
Correct |
174 ms |
26984 KB |
Output is correct |
73 |
Correct |
187 ms |
27752 KB |
Output is correct |
74 |
Correct |
188 ms |
27984 KB |
Output is correct |
75 |
Correct |
189 ms |
27988 KB |
Output is correct |