이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define rep(i,a,b) for (int i = (a); i <= (b);i++)
#define repa(i,a,b) for (int i = (a); i >= (b);i--)
#define debug(a) cout << #a << " = " << a << endl
#define debugsl(a) cout << #a << " = " << a << ", "
#define MAX 500000
#define val first
#define res second
#define INF 1000000000000
lli n,a;
lli prim[MAX+2],ult[MAX+2],res[MAX+2],arr[MAX+2],acu[MAX+2];
pair<lli,lli> dp[MAX+2];
lli binaria(lli f, lli busco) {
lli fin = ult[busco - 1];
lli ini = prim[busco-1];
lli a,b,mitad,last = -1;
while (ini <= fin) {
mitad = (ini+fin)/2;
a = dp[mitad].first;
b = acu[f] - acu[mitad];
if (a <= b) {
last = mitad;
ini = mitad+1;
}
else fin = mitad-1;
}
return last;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
rep(i,1,n) {
cin >> arr[i];
acu[i] = arr[i] + acu[i-1];
}
dp[1] = {arr[1],INF};
res[1] = 1;
prim[1] = ult[1] = 1;
//ciudado con el second 0
rep(i,2,n) {
a = binaria(i,res[i-1]+1);
if (a != -1) {
res[i] = res[a]+1;
prim[res[i]] = i;
dp[i].first = acu[i]-acu[a];
}
else {
res[i] = res[i-1];
if (res[i] > 1){
a = binaria(ult[res[i] - 1], res[i]);
dp[i].first = dp[a].first + acu[i] - acu[a];
}
else dp[i].first = dp[i - 1].first + arr[i];
}
ult[res[i]] = i;
//debugsl(i);
//debugsl(res[i]);
//debugsl(dp[i].first);
//debug(dp[i].second);
}
cout << res[n];
}
# | 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... |