# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1163233 | SmuggingSpun | Bigger segments (IZhO19_segments) | C++20 | 143 ms | 35836 KiB |
#include<bits/stdc++.h>
#define taskname "B"
using namespace std;
typedef long long ll;
const int INF = 1e9;
const ll LINF = 1e18;
template<class T>void maximize(T& a, T b){
if(a < b){
a = b;
}
}
int n;
namespace sub123{
void solve(){
vector<ll>f(n + 1);
f[0] = 0;
for(int i = 1; i <= n; i++){
cin >> f[i];
f[i] += f[i - 1];
}
vector<vector<int>>dp(n + 1, vector<int>(n + 1, -INF));
for(int l = dp[1][1] = 1; l < n; l++){
for(int r = l; r < n; r++){
maximize(dp[l][r + 1], dp[l][r]);
int low = r + 1, high = n, p = -1;
while(low <= high){
int mid = (low + high) >> 1;
if(f[mid] - f[r] >= f[r] - f[l - 1]){
high = (p = mid) - 1;
}
else{
low = mid + 1;
}
}
if(p != -1){
maximize(dp[r + 1][p], dp[l][r] + 1);
}
}
}
int ans = 0;
for(int i = 1; i <= n; i++){
maximize(ans, dp[i][n]);
}
cout << ans;
}
}
namespace sub45{
const int lim = 5e5 + 5;
ll a[lim];
int dp[lim], pre[lim];
void solve(){
memset(pre, a[0] = dp[0] = 0, sizeof(pre));
for(int i = 1; i <= n; i++){
cin >> a[i];
a[i] += a[i - 1];
}
a[n + 1] = LINF;
for(int i = 1; i <= n; i++){
maximize(pre[i], pre[i - 1]);
dp[i] = dp[pre[pre[lower_bound(a + i + 1, a + n + 2, (a[i] << 1LL) - a[pre[i]]) - a] = i]] + 1;
}
cout << dp[n];
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if(fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
}
cin >> n;
if(n <= 3000){
sub123::solve();
}
else{
sub45::solve();
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |