Skip to content

Commit f46fffa

Browse files
Create Pchelyonok and Segments Explanation.txt
1 parent 477a190 commit f46fffa

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
How to use smaller subproblems to build the solution $?$
2+
3+
- In order to know if we can make the $P$-th partition here, we need to know if it is possible to make $P - 1, P - 2, \dots , 1$ partitions from $[i + P]$.
4+
- Let $f(i, P)$ be the maximum sum that the $P$-th partition can have if it begins anywhere on or after $i$ and is followed by all the smaller partitions.
5+
- $f(i, P) = \max\{f(i + 1, P), S[i, i + P - 1]\}$
6+
- We need to be careful and first check that $S[i, i + P - 1] < f(i + P, P - 1)$ in order to maintain the decreasing sum condition.
7+
8+
---
9+
10+
## Why are we taking the maximum sum $?$
11+
12+
- We need to ensure that the sum of the $P$-th segment should be smaller than the $P - 1$ segment.
13+
- Since, we are going from right to left, we might as well choose the maximum possible sum for each segment greedily.
14+
- We can use an exchange argument to show that the number of segments will never decrease by choosing a greater sum segment beginning on or after the same index.
15+
- The answer is the largest $k$ for which $f(1, k) > 0$
16+
17+
---
18+
19+
## Why is this not $O(n^2)$ $?$
20+
21+
- The maximum $k$ is such that $\frac{k(k + 1)}{2} \le n \implies k(k + 1) \le 2n \implies k^2 < 2n$
22+
- So, $k < \sqrt{2n}$
23+
- This is actually $O(n \sqrt{n})$ because of how quickly the triangular numbers grow.
24+
25+
-----
26+
27+
```cpp
28+
void solve()
29+
{
30+
int no_of_elements;
31+
cin >> no_of_elements;
32+
33+
vector <long long> A(no_of_elements + 5, 0);
34+
for(int i = 1; i <= no_of_elements; i++)
35+
{
36+
cin >> A[i];
37+
}
38+
39+
vector <long long> sum_from(no_of_elements + 5, 0);
40+
for(int i = no_of_elements; i >= 1; i--)
41+
{
42+
sum_from[i] = A[i] + sum_from[i + 1];
43+
}
44+
45+
long long max_k = get_largest_k(no_of_elements);
46+
47+
vector <vector <long long> > max_segment_sum(no_of_elements + 5, vector <long long> (max_k + 1));
48+
for(int i = no_of_elements; i >= 1; i--)
49+
{
50+
for(int segment_size = 1; segment_size <= max_k; segment_size++)
51+
{
52+
max_segment_sum[i][segment_size] = max_segment_sum[i + 1][segment_size];
53+
54+
if(i + segment_size - 1 > no_of_elements)
55+
{
56+
continue;
57+
}
58+
59+
long long sum_here = sum_from[i] - sum_from[i + segment_size];
60+
61+
if(segment_size == 1 || sum_here < max_segment_sum[i + segment_size][segment_size - 1])
62+
{
63+
max_segment_sum[i][segment_size] = max(max_segment_sum[i][segment_size], sum_here);
64+
}
65+
}
66+
}
67+
68+
int answer = 0;
69+
for(int segment_size = max_k; segment_size >= 1; segment_size--)
70+
{
71+
if(max_segment_sum[1][segment_size] > 0)
72+
{
73+
answer = segment_size;
74+
break;
75+
}
76+
}
77+
78+
cout << answer << "\n";
79+
}
80+
```

0 commit comments

Comments
 (0)