이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> ii;
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define rep(i,n) for(ll i = 1; i <= n; i++)
ll n, a[500010],k[500010],s[500010],st[500010];
ii dp[500010];
int main(){
cin>>n;
rep(i,n)cin>>a[i],s[i]=s[i-1]+a[i];
dp[1]={1,a[1]};
k[1]=1;
st[1]=1;
int K=1;
for(int i=2; i<=n; i++){
//make a new group
while(k[K]+1<i&&dp[k[K]+1].s<s[i]-s[k[K]+1]){
k[K]++;
}
if(dp[st[K]].s<=s[i]-s[k[K]]){
dp[i]={K+1,s[i]-s[k[K]]};
K++;
st[K]=i;
k[K]=i;
//cout<<">"<<dp[i].f<<' '<<dp[i].s<<endl;
continue;
}
dp[i]={K,1e9};
// get min from K-1;
while(k[K-1]+1<st[K]&&dp[k[K-1]+1].s<s[i]-s[k[K-1]+1]){
k[K-1]++;
}
if(dp[k[K-1]].s<s[i]-s[k[K-1]]){
dp[i].s=s[i]-s[k[K-1]];
}
//add on to k
dp[i].s=min(dp[i].s,dp[i-1].s+a[i]);
//cout<<dp[i].f<<' '<<dp[i].s<<endl;
}
cout<<K<<endl;
}
# | 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... |