이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <bitset>
#include <math.h>
#include <fstream>
#include <iomanip>
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vvvvll = vector<vvvll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvvb = vector<vvb>;
using vld = vector<ld>;
using vvld = vector<vld>;
using vstr = vector<string>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using vvpll = vector<vpll>;
using pb = pair<bool, bool>;
using vpb = vector<pb>;
using vvpb = vector<vpb>;
using vi = vector<int>;
using vvi = vector<vi>;
const ll mod = (ll)1e9 + 7;
const ll inf = (ll)1e18;
#define FAST ios_base::sync_with_stdio(0)
#define FASTIN cin.tie(0)
#define FASTOUT cout.tie(0)
#define upmin(a, b) a = min(a, b)
#define upmax(a, b) a = max(a, b)
#define pr(x) cout << x << endl;
#define prv(v) for(auto it = v.begin(); it != v.end(); ++it) cout << *it << " "; cout << endl;
#define prvv(v) for(auto it : v) { for(auto it2 : it) cout << it2 << " "; cout << endl; } cout << endl;
#define wpr(x) cout << #x << " = " << (x) << endl;
#define wprv(v) cout << #v << ": " << endl; for(auto it : v) cout << it << " "; cout << endl;
#define wprvv(v) cout << #v << ": " << endl; for(auto it : v) { for(auto it2 : it) cout << it2 << " "; cout << endl; } cout << endl;
#define rep(i,s,e) for(ll i = s; i < e; i++)
#define all(x) x.begin(),x.end()
#define pb push_back
ll n, k;
vll arr;
vvll dp, last;
vll p;
/*
dp[layer][i] = max sum using layer seperators and i is the last one (and it is AFTER i)
*/
void D_C(ll layer, ll l, ll r, ll optl, ll optr)
{
if (l >= r) return;
ll i = (l + r) / 2;
ll opt = -1;
ll optv = -1;
//wpr(l);
//wpr(r);
//wpr(i);
//wpr(optl);
//wpr(optr);
rep(j, optl, min(optr+1, i)) {
//wpr(j);
ll v = dp[layer - 1][j] + (p[i] - p[j]) * (p[n - 1] - p[i]);
if (optv < v) {
optv = v;
opt = j;
}
}
//wpr(opt);
dp[layer][i] = optv;
last[layer][i] = opt;
D_C(layer, l, i, optl, opt);
D_C(layer, i+1, r, opt, optr);
}
void solve()
{
cin >> n >> k;
arr.resize(n);
rep(i, 0, n) cin >> arr[i];
p.resize(n);
p[0] = arr[0];
rep(i, 1, n) p[i] = arr[i] + p[i - 1];
dp.resize(k + 1, vll(n - 1, -1));
last.resize(k + 1, vll(n - 1, -1));
rep(i, 0, n - 1) {
dp[1][i] = p[i] * (p[n - 1] - p[i]);
}
rep(l, 2, k + 1) {
/*
rep(i, 0, n-1) {
rep(j, 0, i) {
ll v = dp[l - 1][j] + (p[i] - p[j])*(p[n-1] - p[i]);
if (dp[l][i] < v) {
dp[l][i] = v;
last[l][i] = j;
}
}
}
*/
D_C(l, 1, n-1, 0, n);
}
//wprvv(dp);
ll ans = -1;
ll mxind = -1;
rep(i, 0, n-1) {
if (dp[k][i] > ans) {
ans = dp[k][i];
mxind = i;
}
}
vll used;
ll cur = mxind;
ll left = k;
while (cur != -1) {
used.push_back(cur+1);
cur = last[left][cur];
left--;
}
reverse(used.begin(), used.end());
pr(ans);
prv(used);
}
int main()
{
FAST;
FASTIN; FASTOUT;
solve();
}
/*
7 3
4 1 3 4 0 2 3
2 1
0 123
10 5
1 1 1 1 1234 1 1 1 1 1234
*/
# | 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... |