이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
long long sum[500001],tree[1050000];
int dp[500001];
void update(int pos,long long val) {
while (pos) {
tree[pos]=min(tree[pos],val);
pos/=2;
}
}
long long solve(int p,int l,int r,int x,int y) {
if (x>r || l>y)
return 2000000000000000000ll;
if (l>=x && r<=y)
return tree[p];
return min(solve(2*p,l,(l+r)/2,x,y),solve(2*p+1,(l+r)/2+1,r,x,y));
}
int main() {
for (int i=0;i<1050000;i++)
tree[i]=2000000000000000000ll;
int n,N=1;
cin>>n;
while (N<n)
N*=2;
for (int i=1;i<=n;i++) {
int x;
cin>>x;
sum[i]=sum[i-1]+x;
int pos=i;
for (int j=18;j>=0;j--)
if (pos-(1<<j)>0 && solve(1,N,2*N-1,pos-(1<<j)+N-1,pos-1+N-1)>sum[i])
pos-=(1<<j);
dp[i]=dp[pos-1]+1;
update(i+N-1,2*sum[i]-sum[pos-1]);
}
cout<<dp[n]<<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... |