Submission #1007004

# Submission time Handle Problem Language Result Execution time Memory
1007004 2024-06-24T10:48:57 Z kebine Bigger segments (IZhO19_segments) C++17
100 / 100
55 ms 18868 KB
#include<bits/stdc++.h>
#define int long long
#define fi first
#define se second

using namespace std;
const int maxn = 5e5;
bool debug = 1;

// dp[i] = maximum # segments if i is the end of the current segment
// pos[i] = last value of R that is lesser than i
// pref[i] - pref[pos[i]] >= pref[j] - pref[i] with j being the lb of the next segment
// 2*pref[i] - pref[pos[i]] >= pref[j]
// binary search and find the leftmost j (since we want to minimize the size of the current segment)
// pos[j] = i

int n;
int pref[maxn+5], dp[maxn+5], pos[maxn+5], a[maxn+5];

signed main() {
  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  cin >> n;
  for (int i=1; i<=n; i++) {
    cin >> a[i];
    pref[i] = pref[i-1] + a[i];
  }
  dp[1] = 1; dp[0] = 0;
  pos[1] = 0; pos[0] = 0;
  for (int i=1; i<=n; i++) {
    int l = i+1, r = n, j = -1;
    pos[i] = max(pos[i], pos[i-1]);
    while (l <= r) {
      int mid = (l+r)/2;
      if (pref[mid] >= 2*pref[i] - pref[pos[i]]) {
        r = mid-1;
        j = mid;
      }
      else l = mid+1;
    }
    if (j != -1) {
      pos[j] = max(pos[j], i);
    } 
    dp[i] = max(dp[i], dp[pos[i]] + 1);
  }
  cout << dp[n] << endl;
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 0 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4560 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4440 KB Output is correct
11 Correct 1 ms 4440 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4440 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 0 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4560 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4440 KB Output is correct
11 Correct 1 ms 4440 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4440 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4444 KB Output is correct
18 Correct 1 ms 4440 KB Output is correct
19 Correct 1 ms 4564 KB Output is correct
20 Correct 1 ms 4444 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 1 ms 4444 KB Output is correct
23 Correct 1 ms 4440 KB Output is correct
24 Correct 1 ms 4444 KB Output is correct
25 Correct 1 ms 4444 KB Output is correct
26 Correct 1 ms 4444 KB Output is correct
27 Correct 1 ms 4444 KB Output is correct
28 Correct 1 ms 4444 KB Output is correct
29 Correct 1 ms 4444 KB Output is correct
30 Correct 1 ms 4564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 0 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4560 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4440 KB Output is correct
11 Correct 1 ms 4440 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4440 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4444 KB Output is correct
18 Correct 1 ms 4440 KB Output is correct
19 Correct 1 ms 4564 KB Output is correct
20 Correct 1 ms 4444 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 1 ms 4444 KB Output is correct
23 Correct 1 ms 4440 KB Output is correct
24 Correct 1 ms 4444 KB Output is correct
25 Correct 1 ms 4444 KB Output is correct
26 Correct 1 ms 4444 KB Output is correct
27 Correct 1 ms 4444 KB Output is correct
28 Correct 1 ms 4444 KB Output is correct
29 Correct 1 ms 4444 KB Output is correct
30 Correct 1 ms 4564 KB Output is correct
31 Correct 1 ms 4444 KB Output is correct
32 Correct 1 ms 4632 KB Output is correct
33 Correct 1 ms 4444 KB Output is correct
34 Correct 1 ms 4444 KB Output is correct
35 Correct 1 ms 4444 KB Output is correct
36 Correct 1 ms 4444 KB Output is correct
37 Correct 1 ms 4444 KB Output is correct
38 Correct 1 ms 4444 KB Output is correct
39 Correct 1 ms 4572 KB Output is correct
40 Correct 1 ms 4572 KB Output is correct
41 Correct 1 ms 4444 KB Output is correct
42 Correct 1 ms 4444 KB Output is correct
43 Correct 1 ms 4568 KB Output is correct
44 Correct 1 ms 4444 KB Output is correct
45 Correct 1 ms 4440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 0 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4560 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4440 KB Output is correct
11 Correct 1 ms 4440 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4440 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4444 KB Output is correct
18 Correct 1 ms 4440 KB Output is correct
19 Correct 1 ms 4564 KB Output is correct
20 Correct 1 ms 4444 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 1 ms 4444 KB Output is correct
23 Correct 1 ms 4440 KB Output is correct
24 Correct 1 ms 4444 KB Output is correct
25 Correct 1 ms 4444 KB Output is correct
26 Correct 1 ms 4444 KB Output is correct
27 Correct 1 ms 4444 KB Output is correct
28 Correct 1 ms 4444 KB Output is correct
29 Correct 1 ms 4444 KB Output is correct
30 Correct 1 ms 4564 KB Output is correct
31 Correct 1 ms 4444 KB Output is correct
32 Correct 1 ms 4632 KB Output is correct
33 Correct 1 ms 4444 KB Output is correct
34 Correct 1 ms 4444 KB Output is correct
35 Correct 1 ms 4444 KB Output is correct
36 Correct 1 ms 4444 KB Output is correct
37 Correct 1 ms 4444 KB Output is correct
38 Correct 1 ms 4444 KB Output is correct
39 Correct 1 ms 4572 KB Output is correct
40 Correct 1 ms 4572 KB Output is correct
41 Correct 1 ms 4444 KB Output is correct
42 Correct 1 ms 4444 KB Output is correct
43 Correct 1 ms 4568 KB Output is correct
44 Correct 1 ms 4444 KB Output is correct
45 Correct 1 ms 4440 KB Output is correct
46 Correct 7 ms 10588 KB Output is correct
47 Correct 12 ms 10588 KB Output is correct
48 Correct 10 ms 10588 KB Output is correct
49 Correct 10 ms 10684 KB Output is correct
50 Correct 10 ms 10840 KB Output is correct
51 Correct 11 ms 11100 KB Output is correct
52 Correct 6 ms 7648 KB Output is correct
53 Correct 4 ms 7256 KB Output is correct
54 Correct 9 ms 10328 KB Output is correct
55 Correct 11 ms 11096 KB Output is correct
56 Correct 8 ms 10588 KB Output is correct
57 Correct 8 ms 10536 KB Output is correct
58 Correct 9 ms 10632 KB Output is correct
59 Correct 8 ms 10588 KB Output is correct
60 Correct 8 ms 10600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 0 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4560 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4440 KB Output is correct
11 Correct 1 ms 4440 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4440 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4444 KB Output is correct
18 Correct 1 ms 4440 KB Output is correct
19 Correct 1 ms 4564 KB Output is correct
20 Correct 1 ms 4444 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 1 ms 4444 KB Output is correct
23 Correct 1 ms 4440 KB Output is correct
24 Correct 1 ms 4444 KB Output is correct
25 Correct 1 ms 4444 KB Output is correct
26 Correct 1 ms 4444 KB Output is correct
27 Correct 1 ms 4444 KB Output is correct
28 Correct 1 ms 4444 KB Output is correct
29 Correct 1 ms 4444 KB Output is correct
30 Correct 1 ms 4564 KB Output is correct
31 Correct 1 ms 4444 KB Output is correct
32 Correct 1 ms 4632 KB Output is correct
33 Correct 1 ms 4444 KB Output is correct
34 Correct 1 ms 4444 KB Output is correct
35 Correct 1 ms 4444 KB Output is correct
36 Correct 1 ms 4444 KB Output is correct
37 Correct 1 ms 4444 KB Output is correct
38 Correct 1 ms 4444 KB Output is correct
39 Correct 1 ms 4572 KB Output is correct
40 Correct 1 ms 4572 KB Output is correct
41 Correct 1 ms 4444 KB Output is correct
42 Correct 1 ms 4444 KB Output is correct
43 Correct 1 ms 4568 KB Output is correct
44 Correct 1 ms 4444 KB Output is correct
45 Correct 1 ms 4440 KB Output is correct
46 Correct 7 ms 10588 KB Output is correct
47 Correct 12 ms 10588 KB Output is correct
48 Correct 10 ms 10588 KB Output is correct
49 Correct 10 ms 10684 KB Output is correct
50 Correct 10 ms 10840 KB Output is correct
51 Correct 11 ms 11100 KB Output is correct
52 Correct 6 ms 7648 KB Output is correct
53 Correct 4 ms 7256 KB Output is correct
54 Correct 9 ms 10328 KB Output is correct
55 Correct 11 ms 11096 KB Output is correct
56 Correct 8 ms 10588 KB Output is correct
57 Correct 8 ms 10536 KB Output is correct
58 Correct 9 ms 10632 KB Output is correct
59 Correct 8 ms 10588 KB Output is correct
60 Correct 8 ms 10600 KB Output is correct
61 Correct 35 ms 17232 KB Output is correct
62 Correct 46 ms 17332 KB Output is correct
63 Correct 54 ms 17812 KB Output is correct
64 Correct 51 ms 18828 KB Output is correct
65 Correct 47 ms 18768 KB Output is correct
66 Correct 55 ms 18820 KB Output is correct
67 Correct 44 ms 16720 KB Output is correct
68 Correct 22 ms 12888 KB Output is correct
69 Correct 39 ms 16856 KB Output is correct
70 Correct 50 ms 18868 KB Output is correct
71 Correct 34 ms 17336 KB Output is correct
72 Correct 42 ms 17748 KB Output is correct
73 Correct 43 ms 17812 KB Output is correct
74 Correct 41 ms 18000 KB Output is correct
75 Correct 40 ms 18008 KB Output is correct