이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define all(c) c.begin(), c.end()
#define endl "\n"
const double PI=3.141592653589;
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define dbg(x...) cerr << "LINE(" << __LINE__ << ") -> " <<"[" << #x << "] = ["; _print(x)
#else
#define dbg(x...)
#endif
int n;
vector<int>a,pref;
int f(int x){
vector<pair<int,int>>dp(n+1);
int pos = 0;
for(int i = 1;i<=n;++i){
dp[i].second = dp[i-1].second+a[i];
if(dp[i].second >= x){
pos = i;
dp[i].first = 1;
break;
}
}
if(pos==0)return 0;
// pos denotes starting position of maxdp value so far
vector<pair<int,int>>p;
for(int i = pos+1;i<=n;++i){
int l = pos, r = i-1;
bool got = 0;
dp[i] = {dp[i-1].first,dp[i-1].second+a[i]};
while(l <= r){
int m = (l+r)/2;
if(pref[i]-pref[m] >= dp[m].second){
l = m+1;
got = 1;
dp[i].first = dp[pos].first+1;
dp[i].second = pref[i]-pref[m];
}else r = m-1;
}
if(got){
p.pb({pos,i-1});
pos = i;
continue;
}
if(p.empty())continue;
// can't increase segment size, so try to reduce sum of current segment
l = p.back().first, r = p.back().second;
while(l <= r){
int m = (l+r)/2;
if(pref[i]-pref[m] >= dp[m].second){
l = m+1;
dp[i].second = min(dp[i].second,pref[i]-pref[m]);
}else r = m-1;
}
}
return dp[n].first;
}
void solve()
{
cin >> n;
a.resize(n+1);
pref.resize(n+1);
for(int i = 1;i<=n;++i)cin >> a[i];
for(int i = 1;i<=n;++i)pref[i] = pref[i-1]+a[i];
int l = 1, r = 1e18;
while(l+2 < r){
int m1 = (l+(r-l)/3), m2 = (r-(r-l)/3);
if(f(m1) < f(m2))l = m1;
else r = m2;
}
int res = 0;
for(int i = l;i<=r;++i)res = max(res, f(i));
cout << res << endl;
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int T=1;
for(int i = 1;i<=T;++i)
{
// cout << "Case #" << i << ": ";
solve();
}
}
# | 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... |