#include <bits/stdc++.h>
using namespace std;
const int MX = 5005;
void chmax(int& a, int b){
if(b > a) a = b;
}
int N, arr[MX], dp[MX][MX];
// Observation #1 : We can easily solve this in O(N^3)
// dp(i, j) is the largest beauty of the wall if the highest row includes [i, j]
// the transition is we add another row [j + 1, k] for every j + 1 <= k <= N
// calculating is easy two pointer
// Observation #2 : if the size of row[j + 1, k] is higher than row[i, j]
// then for every other k >= current_k
// the value of the dp is constant
// this is also correct for the opposite
// By using two pointer we can find the positions where the number changes
// We update those positions
// and in the end max each positions with the previous one
int main(){
ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
cin >> N;
for(int i = 0; i < N; i++) cin >> arr[i];
for(int p = 0; p < N; p++){
int i = p, k = p + 1, cnt = -1, bot = 0, top = 0;
while(i >= 0 && k < N){
if(bot == top){
cnt++; chmax(dp[p + 1][k], dp[i][p] + cnt);
bot += arr[i--]; top += arr[k++];
}else if(bot < top){
bot += arr[i--];
}else if(top < bot){
top += arr[k++];
}
}
for(int j = p + 1; j < N; j++) chmax(dp[p + 1][j], dp[p + 1][j - 1]);
}
int ans = 0;
for(int p = 0; p < N; p++) chmax(ans, dp[p][N - 1]);
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2764 KB |
Output is correct |
2 |
Incorrect |
4 ms |
2764 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |