이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define file ""
#define all(x) x.begin(), x.end()
#define sc second
#define fr first
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const ll inf = 1e18 + 5;
const ll mod = 1e9 + 7;
const int N = 5e5 + 5;
int dx[] = {+1, 0, -1, 0};
int dy[] = {0, +1, 0, -1};
ll p[N], a[N];
ll n;
vector<pair<ll, ll>> dp(N + 5), all;
int get(ll x) {
int l = 0;
int r = all.size() - 1;
int ans = -1;
while (l <= r) {
int m = l + r >> 1;
if (all[m].fr <= x) l = m + 1, ans = m;
else r = m - 1;
}
if (ans == -1) return 0;
return all[ans].sc;
}
int main() {
ios_base :: sync_with_stdio(false);
cin.tie(nullptr);
srand(time(nullptr));
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) p[i] += p[i - 1] + a[i];
for (int i = 1; i <= n; i++) {
int pos = get(p[i]);
//cerr << i << " " << pos << endl;
dp[i].fr = dp[pos].fr + 1;
dp[i].sc = p[i] - p[pos];
while ((int)all.size() > 0 && all.back().fr >= dp[i].sc + p[i])
all.pop_back();
all.pb({dp[i].sc + p[i], i});
/*for (auto t : all)
cout << t.fr << " ";
cout << endl; */
}
/*for (int i = 1; i <= n; i++)
cout << i << ": " << dp[i].fr << " " << dp[i].sc << endl; */
cout << dp[n].fr;
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
segments.cpp: In function 'int get(ll)':
segments.cpp:35:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
35 | int m = l + r >> 1;
| ~~^~~
# | 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... |