#include <bits/stdc++.h>
//#include <ext/pb_ds/detail/standard_policies.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx2")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
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>;
mt19937 gen(time(0));
#define ll long long
#define ld long double
#define pb push_back
#define F first
#define S second
#define TIME clock() * 1.0 / CLOCKS_PER_SEC
#define sz(a) int32_t(a.size())
#define endl '\n'
//#define int long long
const int N = 5e5 + 100;
const int M = 31;
const int mod = 1e9 + 7;
const ll inf = 1e18 + 7;
ll pref[N];
int a[N];
vector < int > add[N];
set < int > st;
pair < int, ll > dp[N];
ll get(int l, int r)
{
if (l > r) return 0;
ll ans = pref[r];
if (l) ans -= pref[l - 1];
return ans;
}
void upd(pair < int, ll > &a, pair < int, ll > b)
{
if (a.F < b.F) a = b;
else if (a.F == b.F) a.S = min(a.S, b.S);
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#else
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif // LOCAL
int n;
cin >> n;
for(int i = 0; i < n; i++)
{
cin >> a[i];
if (i) pref[i] = pref[i - 1];
pref[i] += a[i];
}
dp[0] = {1, a[0]};
for(int i = 0; i < n; i++)
{
dp[i] = {1, pref[i]};
if (i) dp[i] = {dp[i - 1].F, dp[i - 1].S + a[i]};
for(auto to: add[i])
st.insert(to);
if (sz(st))
{
int j = *st.rbegin();
int l = j, r = i - 1;
while (l <= r)
{
int mid = (l + r) / 2;
if (dp[j].S + get(j + 1, mid) <= get(mid + 1, i))
{
upd(dp[i], {dp[j].F + 1, get(mid + 1, i)});
l = mid + 1;
}
else r = mid - 1;
}
}
int l = i + 1, r = n - 1, gr = n;
while (l <= r)
{
int mid = (l + r) / 2;
ll cnt = pref[mid] - pref[i];
if (cnt >= dp[i].S)
{
gr = mid;
r = mid - 1;
}
else l = mid + 1;
}
add[gr].pb(i);
}
cout << dp[n - 1].F;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11980 KB |
Output is correct |
2 |
Correct |
7 ms |
11980 KB |
Output is correct |
3 |
Correct |
6 ms |
11980 KB |
Output is correct |
4 |
Correct |
6 ms |
12080 KB |
Output is correct |
5 |
Correct |
6 ms |
11980 KB |
Output is correct |
6 |
Correct |
6 ms |
11980 KB |
Output is correct |
7 |
Correct |
6 ms |
11980 KB |
Output is correct |
8 |
Correct |
6 ms |
11980 KB |
Output is correct |
9 |
Correct |
6 ms |
11980 KB |
Output is correct |
10 |
Correct |
7 ms |
11980 KB |
Output is correct |
11 |
Correct |
7 ms |
11980 KB |
Output is correct |
12 |
Correct |
6 ms |
11980 KB |
Output is correct |
13 |
Correct |
6 ms |
12080 KB |
Output is correct |
14 |
Correct |
7 ms |
11980 KB |
Output is correct |
15 |
Correct |
6 ms |
11980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11980 KB |
Output is correct |
2 |
Correct |
7 ms |
11980 KB |
Output is correct |
3 |
Correct |
6 ms |
11980 KB |
Output is correct |
4 |
Correct |
6 ms |
12080 KB |
Output is correct |
5 |
Correct |
6 ms |
11980 KB |
Output is correct |
6 |
Correct |
6 ms |
11980 KB |
Output is correct |
7 |
Correct |
6 ms |
11980 KB |
Output is correct |
8 |
Correct |
6 ms |
11980 KB |
Output is correct |
9 |
Correct |
6 ms |
11980 KB |
Output is correct |
10 |
Correct |
7 ms |
11980 KB |
Output is correct |
11 |
Correct |
7 ms |
11980 KB |
Output is correct |
12 |
Correct |
6 ms |
11980 KB |
Output is correct |
13 |
Correct |
6 ms |
12080 KB |
Output is correct |
14 |
Correct |
7 ms |
11980 KB |
Output is correct |
15 |
Correct |
6 ms |
11980 KB |
Output is correct |
16 |
Correct |
6 ms |
11980 KB |
Output is correct |
17 |
Correct |
6 ms |
12048 KB |
Output is correct |
18 |
Correct |
6 ms |
12108 KB |
Output is correct |
19 |
Correct |
7 ms |
12120 KB |
Output is correct |
20 |
Correct |
6 ms |
12108 KB |
Output is correct |
21 |
Correct |
6 ms |
12108 KB |
Output is correct |
22 |
Correct |
6 ms |
12108 KB |
Output is correct |
23 |
Correct |
6 ms |
12108 KB |
Output is correct |
24 |
Correct |
6 ms |
12108 KB |
Output is correct |
25 |
Correct |
6 ms |
12108 KB |
Output is correct |
26 |
Correct |
6 ms |
12136 KB |
Output is correct |
27 |
Correct |
7 ms |
12108 KB |
Output is correct |
28 |
Correct |
6 ms |
12108 KB |
Output is correct |
29 |
Correct |
6 ms |
12108 KB |
Output is correct |
30 |
Correct |
8 ms |
12108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11980 KB |
Output is correct |
2 |
Correct |
7 ms |
11980 KB |
Output is correct |
3 |
Correct |
6 ms |
11980 KB |
Output is correct |
4 |
Correct |
6 ms |
12080 KB |
Output is correct |
5 |
Correct |
6 ms |
11980 KB |
Output is correct |
6 |
Correct |
6 ms |
11980 KB |
Output is correct |
7 |
Correct |
6 ms |
11980 KB |
Output is correct |
8 |
Correct |
6 ms |
11980 KB |
Output is correct |
9 |
Correct |
6 ms |
11980 KB |
Output is correct |
10 |
Correct |
7 ms |
11980 KB |
Output is correct |
11 |
Correct |
7 ms |
11980 KB |
Output is correct |
12 |
Correct |
6 ms |
11980 KB |
Output is correct |
13 |
Correct |
6 ms |
12080 KB |
Output is correct |
14 |
Correct |
7 ms |
11980 KB |
Output is correct |
15 |
Correct |
6 ms |
11980 KB |
Output is correct |
16 |
Correct |
6 ms |
11980 KB |
Output is correct |
17 |
Correct |
6 ms |
12048 KB |
Output is correct |
18 |
Correct |
6 ms |
12108 KB |
Output is correct |
19 |
Correct |
7 ms |
12120 KB |
Output is correct |
20 |
Correct |
6 ms |
12108 KB |
Output is correct |
21 |
Correct |
6 ms |
12108 KB |
Output is correct |
22 |
Correct |
6 ms |
12108 KB |
Output is correct |
23 |
Correct |
6 ms |
12108 KB |
Output is correct |
24 |
Correct |
6 ms |
12108 KB |
Output is correct |
25 |
Correct |
6 ms |
12108 KB |
Output is correct |
26 |
Correct |
6 ms |
12136 KB |
Output is correct |
27 |
Correct |
7 ms |
12108 KB |
Output is correct |
28 |
Correct |
6 ms |
12108 KB |
Output is correct |
29 |
Correct |
6 ms |
12108 KB |
Output is correct |
30 |
Correct |
8 ms |
12108 KB |
Output is correct |
31 |
Correct |
6 ms |
12108 KB |
Output is correct |
32 |
Correct |
7 ms |
12236 KB |
Output is correct |
33 |
Correct |
6 ms |
12364 KB |
Output is correct |
34 |
Correct |
7 ms |
12236 KB |
Output is correct |
35 |
Correct |
7 ms |
12364 KB |
Output is correct |
36 |
Correct |
7 ms |
12364 KB |
Output is correct |
37 |
Correct |
7 ms |
12236 KB |
Output is correct |
38 |
Correct |
7 ms |
12236 KB |
Output is correct |
39 |
Correct |
7 ms |
12236 KB |
Output is correct |
40 |
Correct |
7 ms |
12388 KB |
Output is correct |
41 |
Correct |
7 ms |
12340 KB |
Output is correct |
42 |
Correct |
9 ms |
12364 KB |
Output is correct |
43 |
Correct |
7 ms |
12364 KB |
Output is correct |
44 |
Correct |
7 ms |
12372 KB |
Output is correct |
45 |
Correct |
7 ms |
12364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11980 KB |
Output is correct |
2 |
Correct |
7 ms |
11980 KB |
Output is correct |
3 |
Correct |
6 ms |
11980 KB |
Output is correct |
4 |
Correct |
6 ms |
12080 KB |
Output is correct |
5 |
Correct |
6 ms |
11980 KB |
Output is correct |
6 |
Correct |
6 ms |
11980 KB |
Output is correct |
7 |
Correct |
6 ms |
11980 KB |
Output is correct |
8 |
Correct |
6 ms |
11980 KB |
Output is correct |
9 |
Correct |
6 ms |
11980 KB |
Output is correct |
10 |
Correct |
7 ms |
11980 KB |
Output is correct |
11 |
Correct |
7 ms |
11980 KB |
Output is correct |
12 |
Correct |
6 ms |
11980 KB |
Output is correct |
13 |
Correct |
6 ms |
12080 KB |
Output is correct |
14 |
Correct |
7 ms |
11980 KB |
Output is correct |
15 |
Correct |
6 ms |
11980 KB |
Output is correct |
16 |
Correct |
6 ms |
11980 KB |
Output is correct |
17 |
Correct |
6 ms |
12048 KB |
Output is correct |
18 |
Correct |
6 ms |
12108 KB |
Output is correct |
19 |
Correct |
7 ms |
12120 KB |
Output is correct |
20 |
Correct |
6 ms |
12108 KB |
Output is correct |
21 |
Correct |
6 ms |
12108 KB |
Output is correct |
22 |
Correct |
6 ms |
12108 KB |
Output is correct |
23 |
Correct |
6 ms |
12108 KB |
Output is correct |
24 |
Correct |
6 ms |
12108 KB |
Output is correct |
25 |
Correct |
6 ms |
12108 KB |
Output is correct |
26 |
Correct |
6 ms |
12136 KB |
Output is correct |
27 |
Correct |
7 ms |
12108 KB |
Output is correct |
28 |
Correct |
6 ms |
12108 KB |
Output is correct |
29 |
Correct |
6 ms |
12108 KB |
Output is correct |
30 |
Correct |
8 ms |
12108 KB |
Output is correct |
31 |
Correct |
6 ms |
12108 KB |
Output is correct |
32 |
Correct |
7 ms |
12236 KB |
Output is correct |
33 |
Correct |
6 ms |
12364 KB |
Output is correct |
34 |
Correct |
7 ms |
12236 KB |
Output is correct |
35 |
Correct |
7 ms |
12364 KB |
Output is correct |
36 |
Correct |
7 ms |
12364 KB |
Output is correct |
37 |
Correct |
7 ms |
12236 KB |
Output is correct |
38 |
Correct |
7 ms |
12236 KB |
Output is correct |
39 |
Correct |
7 ms |
12236 KB |
Output is correct |
40 |
Correct |
7 ms |
12388 KB |
Output is correct |
41 |
Correct |
7 ms |
12340 KB |
Output is correct |
42 |
Correct |
9 ms |
12364 KB |
Output is correct |
43 |
Correct |
7 ms |
12364 KB |
Output is correct |
44 |
Correct |
7 ms |
12372 KB |
Output is correct |
45 |
Correct |
7 ms |
12364 KB |
Output is correct |
46 |
Correct |
16 ms |
15264 KB |
Output is correct |
47 |
Correct |
39 ms |
21136 KB |
Output is correct |
48 |
Correct |
43 ms |
21940 KB |
Output is correct |
49 |
Correct |
46 ms |
22780 KB |
Output is correct |
50 |
Correct |
44 ms |
23592 KB |
Output is correct |
51 |
Correct |
45 ms |
22352 KB |
Output is correct |
52 |
Correct |
25 ms |
17208 KB |
Output is correct |
53 |
Correct |
17 ms |
15168 KB |
Output is correct |
54 |
Correct |
40 ms |
21308 KB |
Output is correct |
55 |
Correct |
42 ms |
23084 KB |
Output is correct |
56 |
Correct |
42 ms |
22992 KB |
Output is correct |
57 |
Correct |
40 ms |
21800 KB |
Output is correct |
58 |
Correct |
42 ms |
22360 KB |
Output is correct |
59 |
Correct |
42 ms |
23148 KB |
Output is correct |
60 |
Correct |
39 ms |
23152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11980 KB |
Output is correct |
2 |
Correct |
7 ms |
11980 KB |
Output is correct |
3 |
Correct |
6 ms |
11980 KB |
Output is correct |
4 |
Correct |
6 ms |
12080 KB |
Output is correct |
5 |
Correct |
6 ms |
11980 KB |
Output is correct |
6 |
Correct |
6 ms |
11980 KB |
Output is correct |
7 |
Correct |
6 ms |
11980 KB |
Output is correct |
8 |
Correct |
6 ms |
11980 KB |
Output is correct |
9 |
Correct |
6 ms |
11980 KB |
Output is correct |
10 |
Correct |
7 ms |
11980 KB |
Output is correct |
11 |
Correct |
7 ms |
11980 KB |
Output is correct |
12 |
Correct |
6 ms |
11980 KB |
Output is correct |
13 |
Correct |
6 ms |
12080 KB |
Output is correct |
14 |
Correct |
7 ms |
11980 KB |
Output is correct |
15 |
Correct |
6 ms |
11980 KB |
Output is correct |
16 |
Correct |
6 ms |
11980 KB |
Output is correct |
17 |
Correct |
6 ms |
12048 KB |
Output is correct |
18 |
Correct |
6 ms |
12108 KB |
Output is correct |
19 |
Correct |
7 ms |
12120 KB |
Output is correct |
20 |
Correct |
6 ms |
12108 KB |
Output is correct |
21 |
Correct |
6 ms |
12108 KB |
Output is correct |
22 |
Correct |
6 ms |
12108 KB |
Output is correct |
23 |
Correct |
6 ms |
12108 KB |
Output is correct |
24 |
Correct |
6 ms |
12108 KB |
Output is correct |
25 |
Correct |
6 ms |
12108 KB |
Output is correct |
26 |
Correct |
6 ms |
12136 KB |
Output is correct |
27 |
Correct |
7 ms |
12108 KB |
Output is correct |
28 |
Correct |
6 ms |
12108 KB |
Output is correct |
29 |
Correct |
6 ms |
12108 KB |
Output is correct |
30 |
Correct |
8 ms |
12108 KB |
Output is correct |
31 |
Correct |
6 ms |
12108 KB |
Output is correct |
32 |
Correct |
7 ms |
12236 KB |
Output is correct |
33 |
Correct |
6 ms |
12364 KB |
Output is correct |
34 |
Correct |
7 ms |
12236 KB |
Output is correct |
35 |
Correct |
7 ms |
12364 KB |
Output is correct |
36 |
Correct |
7 ms |
12364 KB |
Output is correct |
37 |
Correct |
7 ms |
12236 KB |
Output is correct |
38 |
Correct |
7 ms |
12236 KB |
Output is correct |
39 |
Correct |
7 ms |
12236 KB |
Output is correct |
40 |
Correct |
7 ms |
12388 KB |
Output is correct |
41 |
Correct |
7 ms |
12340 KB |
Output is correct |
42 |
Correct |
9 ms |
12364 KB |
Output is correct |
43 |
Correct |
7 ms |
12364 KB |
Output is correct |
44 |
Correct |
7 ms |
12372 KB |
Output is correct |
45 |
Correct |
7 ms |
12364 KB |
Output is correct |
46 |
Correct |
16 ms |
15264 KB |
Output is correct |
47 |
Correct |
39 ms |
21136 KB |
Output is correct |
48 |
Correct |
43 ms |
21940 KB |
Output is correct |
49 |
Correct |
46 ms |
22780 KB |
Output is correct |
50 |
Correct |
44 ms |
23592 KB |
Output is correct |
51 |
Correct |
45 ms |
22352 KB |
Output is correct |
52 |
Correct |
25 ms |
17208 KB |
Output is correct |
53 |
Correct |
17 ms |
15168 KB |
Output is correct |
54 |
Correct |
40 ms |
21308 KB |
Output is correct |
55 |
Correct |
42 ms |
23084 KB |
Output is correct |
56 |
Correct |
42 ms |
22992 KB |
Output is correct |
57 |
Correct |
40 ms |
21800 KB |
Output is correct |
58 |
Correct |
42 ms |
22360 KB |
Output is correct |
59 |
Correct |
42 ms |
23148 KB |
Output is correct |
60 |
Correct |
39 ms |
23152 KB |
Output is correct |
61 |
Correct |
73 ms |
29744 KB |
Output is correct |
62 |
Correct |
146 ms |
43072 KB |
Output is correct |
63 |
Correct |
190 ms |
56388 KB |
Output is correct |
64 |
Correct |
205 ms |
64836 KB |
Output is correct |
65 |
Correct |
253 ms |
69780 KB |
Output is correct |
66 |
Correct |
221 ms |
63460 KB |
Output is correct |
67 |
Correct |
169 ms |
53224 KB |
Output is correct |
68 |
Correct |
82 ms |
32564 KB |
Output is correct |
69 |
Correct |
183 ms |
58864 KB |
Output is correct |
70 |
Correct |
193 ms |
67524 KB |
Output is correct |
71 |
Correct |
212 ms |
66960 KB |
Output is correct |
72 |
Correct |
207 ms |
61764 KB |
Output is correct |
73 |
Correct |
211 ms |
65680 KB |
Output is correct |
74 |
Correct |
211 ms |
68156 KB |
Output is correct |
75 |
Correct |
203 ms |
68068 KB |
Output is correct |