이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define file if (fopen("input.txt", "r")) { freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); }
#define int long long
#define s second
#define f first
void setIn(string s) { freopen(s.c_str(),"r",stdin); }
void setOut(string s) { freopen(s.c_str(),"w",stdout); }
void setIO(string s = "") {
if (s.size()) setIn(s+".in"), setOut(s+".out");
}
const int maxn = 1e5 + 2, maxk = 202;
int a[maxn], prf[maxn], n, k;
namespace Sub2
{
const int s2maxn = 52, s2maxk = 52;
int dp[s2maxn][s2maxn][s2maxk];
int cal(int l, int r, int krem)
{
if (l == r || krem == 0)
return dp[l][r][krem] = 0;
int res = dp[l][r][krem];
if (res != -1)
return res;
res = 0;
for (int i = l; i < r; i++)
{
for (int lk = 0; lk < krem; lk++)
{
int rk = krem - lk - 1;
res = max(res, cal(l, i, lk) + cal(i + 1, r, rk) + (prf[r] - prf[i]) * (prf[i] - prf[l - 1]));
}
}
return dp[l][r][krem] = res;
}
void trace(int l, int r, int krem)
{
bool skip = 0;
for (int i = l; i < r; i++)
{
for (int lk = 0; lk < krem; lk++)
{
int rk = krem - lk - 1;
if (dp[l][r][krem] == cal(l, i, lk) + cal(i + 1, r, rk) + (prf[r] - prf[i]) * (prf[i] - prf[l - 1]))
{
cout << i << ' ';
trace(1, i, lk);
trace(i + 1, r, rk);
skip = 1;
break;
}
}
if (skip)
break;
}
}
void solve()
{
memset(dp, -1, sizeof(dp));
cout << cal(1, n, k) << '\n';
trace(1, n, k);
}
}
namespace Sub4
{
const int s4maxn = 1002, s4maxk = 202;
int dp[s4maxn][s4maxk];
void solve()
{
// dp[0][0] = 0;
memset(dp, -0x3f, sizeof(dp));
dp[0][0] = 0;
k++;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= k; j++)
{
for (int p = 0; p < i; p++)
{
//from n -> i + 1 //from i -> p + 1
dp[i][j] = max(dp[p][j - 1] + (prf[n] - prf[i]) * (prf[i] - prf[p]), dp[i][j]);
}
}
}
cout << dp[n][k] << '\n';
int curk = k, curb = n;
while (curk > 0)
{
for (int i = 0; i < curb; i++)
{
if (dp[i][curk - 1] + (prf[n] - prf[curb]) * (prf[curb] - prf[i]) == dp[curb][curk])
{
if (i)
cout << i << ' ';
curk--;
curb = i;
}
}
}
}
}
namespace Sub5
{
const int s5maxn = 1e4 + 2, s5maxk = 202;
int dp[s5maxk][s5maxn];
void compute(int l, int r, int optl, int optr, int curk)
{
if (l > r)
return;
int mid = (l + r) / 2;
pair <int, int> best = {LLONG_MIN, -1};
for (int i = optl; i <= min(optr, mid); i++)
{
best = max(best, {dp[curk - 1][i] + (prf[n] - prf[mid]) * (prf[mid] - prf[i]), i});
}
int opt = best.second;
dp[curk][mid] = best.first;
compute(l, mid - 1, optl, opt, curk);
compute(mid + 1, r, opt, optr, curk);
}
void solve()
{
k++;
memset(dp, -0x3f, sizeof(dp));
dp[0][0] = 0;
for (int i = 1; i <= k; i++)
compute(0, n, 0, n, i);
cout << dp[k][n] << '\n';
int curk = k, curb = n;
while (curk > 0)
{
for (int i = 0; i < curb; i++)
{
if (dp[curk - 1][i] + (prf[n] - prf[curb]) * (prf[curb] - prf[i]) == dp[curk][curb])
{
if (i)
cout << i << ' ';
curk--;
curb = i;
}
}
}
}
}
#define ll long long
struct line {
ll m, c;
int idx;
};
struct CHT {
deque<line> dq;
int p;
void clear() {
while (!dq.empty()) dq.pop_back();
p = 0;
}
bool replace(line x, line y, line z) {
return (x.c - y.c) * (z.m - x.m) >= (x.c - z.c) * (y.m - x.m);
}
void add(line x) {
while (dq.size() > 1 && replace(dq[dq.size() - 2], dq.back(), x)) {
dq.pop_back();
}
dq.push_back(x);
}
line query(ll x) {
p = min(p, (int)dq.size() - 1);
while (p < (int)dq.size() - 1 && dq[p].c - dq[p + 1].c < x * (dq[p + 1].m - dq[p].m)) p++;
return dq[p];
}
} cht;
namespace Sub6
{
int dp[6][maxn], pt, trace[202][maxn];
vector <pair <int, int>> A, B;
/*
* dp[j][i] = 0..i with j-th split happening at between a[i] and a[i+1]
*
* dp[j][i] = max(dp[j-1][k] + (p[i] - p[k]) * (p[n] - p[i]))
* = p[i]p[n] - p[i]p[i] + max(p[i]p[k] + dp[j-1][k] - p[k]p[n])
**/
void solve()
{
k++;
for (int i = 1;i <= n;i++) {
dp[1][i] = prf[i] * (prf[n] - prf[i]);
}
for (int i = 2; i <= k; i++)
{
cht.clear();
cht.add({prf[i - 1], -prf[i - 1] * prf[n] + dp[!(i & 1)][i - 1], i - 1});
for (int j = i; j <= n; j++)
{
auto tmp = cht.query(prf[j]);
dp[(i & 1)][j] = prf[j] * prf[n] - prf[j] * prf[j] + tmp.m * prf[j] + tmp.c;
// cout << i << ' ' << j << '\n';
// for (int pp = 0; pp < A.size(); pp++)
// cout << A[pp].s << " \n"[pp + 1 == A.size()];
// cout << i << ' ' << j << ' ' << prf[j] * prf[n] - prf[j] * prf[j] + query(prf[j]).f << ' ' << query(prf[j]).f << ' ' << query(prf[j]).s << '\n';
trace[i][j] = tmp.idx;
if (i > 1)
cht.add({prf[j], dp[!(i & 1)][j] - prf[j] * prf[n], j});
}
}
cout << dp[k % 2][n] << '\n';
int curk = k, curb = n;
while (curk > 0)
{
if (trace[curk][curb])
cout << trace[curk][curb] << ' ';
curb = trace[curk][curb];
curk--;
}
}
}
signed main()
{
// setIO();
file;
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; i++)
cin >> a[i], prf[i] = prf[i - 1] + a[i];
if (n <= 50)
return Sub2::solve(), 0;
if (n <= 1000)
return Sub4::solve(), 0;
if (n <= 10000)
return Sub5::solve(), 0;
if (n <= 100000)
return Sub6::solve(), 0;
cout << "From ITK20 w love UwU";
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In function 'void setIn(std::string)':
sequence.cpp:9:31: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
9 | void setIn(string s) { freopen(s.c_str(),"r",stdin); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
sequence.cpp: In function 'void setOut(std::string)':
sequence.cpp:10:32: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
10 | void setOut(string s) { freopen(s.c_str(),"w",stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
sequence.cpp: In function 'int main()':
sequence.cpp:4:53: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
4 | #define file if (fopen("input.txt", "r")) { freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:256:5: note: in expansion of macro 'file'
256 | file;
| ^~~~
sequence.cpp:4:87: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
4 | #define file if (fopen("input.txt", "r")) { freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:256:5: note: in expansion of macro 'file'
256 | file;
| ^~~~
# | 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... |