이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define el '\n'
#define pb push_back
#define mp make_pair
#define niggers \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define file(name) \
freopen(name ".inp", "r", stdin); \
freopen(name ".out", "w", stdout);
typedef long long ll;
typedef pair<long long, long long> pll;
typedef pair<int, int> pii;
typedef vector<long long> vll;
typedef vector<int> vi;
const ll MOD = 1e9 + 7;
const int inf = 1e9;
const int mxN = 50 + 5;
const int mxM = 35000 + 5;
const int N = 1e5 + 5;
int hx[] = {-1, 0, 1, 0};
int hy[] = {0, 1, 0, -1};
ll n, k, a[N], dp[100005][208], cnt[N], mo_l = 1, mo_r, cur1 = 0, cur2 = 0;
ll ps[100005];
// void add(ll i){
// cur1+=a[i];
// cur2++;
// }
// void rmv(ll i){
// cur1-=a[i];
// cur2--;
// }
ll trace[100005][208];
ll get(ll l, ll r)
{
// while(mo_r < r) add(++mo_r);
// while(mo_r > r) rmv(mo_r--);
// while(mo_l < l) rmv(mo_l++);
// while(mo_l > l) add(--mo_l);
// return cur1*cur2;
return (ps[r] - ps[l - 1]) * (ps[l-1]);
}
void calc(ll l, ll r, ll optl, ll optr, ll j)
{
if (l > r)
return;
ll m = l + r >> 1;
pll best = {-inf, -1};
for (ll i = optl; i <= min(m, optr); ++i)
{
best = max(best, {dp[i - 1][j & 1 ^ 1] + get(i, m), i});
}
dp[m][j & 1] = best.fi;
ll opt = best.se;
trace[m][j] = opt-1;
calc(l, m - 1, optl, opt, j);
calc(m + 1, r, opt, optr, j);
}
void solve()
{
cin >> n >> k;
for (ll i = 1; i <= n; ++i)
{
cin >> a[i];
ps[i] = ps[i - 1] + a[i];
}
for (ll i = 1; i <= k; ++i)
{
calc(i, n, 1, n, i);
}
cout << dp[n][k & 1] <<'\n';
vi ans;
for( ll i = k ; i >= 1 ; i--){
n = trace[n][i];
ans.pb(n);
}
reverse(ans.begin(),ans.end());
for(auto x:ans) cout << x << ' ';
}
int main()
{
// freopen("b.inp", "r", stdin);
// freopen("b.out", "w", stdout);
ll pinkkiu = 1;
// cin >> pinkkiu;
while (pinkkiu--)
{
solve();
}
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In function 'void calc(ll, ll, ll, ll, ll)':
sequence.cpp:57:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
57 | ll m = l + r >> 1;
| ~~^~~
sequence.cpp:61:43: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
61 | best = max(best, {dp[i - 1][j & 1 ^ 1] + get(i, m), i});
| ~~^~~
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |