이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
# include <bits/stdc++.h>
# define pb push_back
# define ff first
# define ss second
# define nl "\n"
# define sz(x) ((int)(x).size())
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ll maxn = 5e5 + 25;
const ll inf = 1e18 + 0;
const ll mod = 1e9 + 7;
const ll dx[] = {-1, 1, 0, 0};
const ll dy[] = {0, 0, -1, 1};
using namespace std;
ll n, a[maxn], pref[maxn];
pair <ll, ll> dp[maxn];
void ma1n (/* SABR */)
{
cin >> n;
for (ll i = 1; i <= n; ++i)
{
cin >> a[i];
pref[i] = pref[i - 1] + a[i];
dp[i].ss = -1;
}
dp[1].ff = 1, dp[1].ss = pref[1];
for (ll i = 1; i <= n; ++i)
{
if (dp[i - 1].ff > dp[i].ff)
{
dp[i].ff = dp[i - 1].ff;
dp[i].ss = dp[i - 1].ss + a[i];
}
ll l = i + 1, r = n, pos = -1;
while (l <= r)
{
ll mid = (l + r) >> 1;
if (pref[mid] >= dp[i].ss + pref[i])
{
r = mid - 1;
pos = mid;
}
else
{
l = mid + 1;
}
}
if (pos == -1) continue;
if (dp[i].ff + 1 > dp[pos].ff)
{
dp[pos].ff = dp[i].ff + 1;
dp[pos].ss = pref[pos] - pref[i];
}
else if (dp[i].ff + 1 == dp[pos].ff)
{
dp[pos].ff = dp[i].ff + 1;
if (dp[pos].ss == -1) dp[pos].ss = pref[pos] - pref[i];
else dp[pos].ss = min(dp[pos].ss, pref[pos] - pref[i]);
}
}
cout << dp[n].ff << nl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
// freopen("file.in", "r", stdin);
// freopen("file.out", "w", stdout);
int ttt = 1;
// cin >> ttt;
for (int test = 1; test <= ttt; ++test)
{
// cout << "Case " << test << ":" << ' ';
ma1n();
}
return 0;
}
# | 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... |