이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define fr(i, a, b) for(int i = (a); i <= (b); ++i)
#define rf(i, a, b) for(int i = (a); i >= (b); --i)
#define fe(x, y) for(auto& x : y)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define all(x) (x).begin(), (x).end()
#define pw(x) (1LL << (x))
#define sz(x) (int)(x).size()
using namespace std;
mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<typename T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define fbo find_by_order
#define ook order_of_key
template<typename T>
bool umn(T& a, T b) { return (a > b ? (a = b, 1) : 0); }
template<typename T>
bool umx(T& a, T b) { return (a < b ? (a = b, 1) : 0); }
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template<typename T>
using ve = vector<T>;
const int N = 3e3 + 5;
int n;
int a[N];
ll pref[N];
int dp[N][N];
pii par[N][N];
int brute1() {
fr(i, 0, n) {
fr(j, 0, n) {
dp[i][j] = -1e9;
}
}
fr(i, 1, n) {
dp[i][1] = 1;
rf(j, i, 1) {
if(dp[i][j] != -1e9) {
fr(nxt, i + 1, n) {
if(pref[nxt] - pref[i] >= pref[i] - pref[j - 1]) {
if(umx(dp[nxt][i + 1], dp[i][j] + 1)) {
par[nxt][i + 1] = {i, j};
}
}
}
break;
}
}
}
int mx = 0;
pii best;
fr(i, 1, n) {
fr(j, 1, n) {
if(umx(mx, dp[i][j])) {
best = {i, j};
}
}
}
ve<int> ans;
while(1) {
ans.pb(best.fi);
if(best.se == 1) break;
best = par[best.fi][best.se];
}
reverse(all(ans));
// cout << "ans: ";
// fe(x, ans) cout << x << " ";
// cout << "\n";
int prev = 0;
fe(x, ans) {
// cout << pref[x] - pref[prev] << " ";
prev = x;
}
// cout << "\n";
return mx;
}
int brute2() {
fr(i, 0, n) {
fr(j, 0, n) {
dp[i][j] = -1e9;
}
}
fr(i, 1, n) {
dp[i][1] = 1;
rf(j, i, 1) {
if(dp[i][j] != -1e9) {
fr(nxt, i + 1, n) {
if(pref[nxt] - pref[i] >= pref[i] - pref[j - 1]) {
if(umx(dp[nxt][i + 1], dp[i][j] + 1)) {
par[nxt][i + 1] = {i, j};
}
}
}
// break;
}
}
}
int mx = 0;
pii best;
fr(i, 1, n) {
fr(j, 1, n) {
if(umx(mx, dp[i][j])) {
best = {i, j};
}
}
}
ve<int> ans;
while(1) {
ans.pb(best.fi);
if(best.se == 1) break;
best = par[best.fi][best.se];
}
reverse(all(ans));
// cout << "ans: ";
// fe(x, ans) cout << x << " ";
// cout << "\n";
int prev = 0;
fe(x, ans) {
// cout << pref[x] - pref[prev] << " ";
prev = x;
}
// cout << "\n";
return mx;
}
int solve() {
int best = 0;
fr(len, 1, n) {
ll prev = 0;
fr(i, 1, len) prev += a[i];
int ans = 1;
ll cur = 0;
fr(i, len + 1, n) {
cur += a[i];
if(cur >= prev) {
// if(len == 1) cout << cur << "\n";
prev = cur;
cur = 0;
ans++;
}
}
umx(best, ans);
}
// cout << best << "\n";
return best;
}
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0);
#else
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0);
#endif
cin >> n;
fr(i, 1, n) {
cin >> a[i];
// a[i] = rng() % int(1e6) + 1;
pref[i] = a[i] + pref[i - 1];
}
// fr(i, 1, n) cout << a[i] << " ";
// cout << "\n";
cout << brute1() << "\n";
// cout << brute2() << "\n";
// cout << solve() << "\n";
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
segments.cpp: In function 'int brute1()':
segments.cpp:92:9: warning: variable 'prev' set but not used [-Wunused-but-set-variable]
92 | int prev = 0;
| ^~~~
segments.cpp: In function 'int brute2()':
segments.cpp:146:9: warning: variable 'prev' set but not used [-Wunused-but-set-variable]
146 | int prev = 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... |