이 제출은 이전 버전의 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
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 = 1e5 + 2, s5maxk = 202;
int dp[s5maxk][s5maxn]; //, trace[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;
// trace[curk][mid] = opt;
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;
}
}
}
}
}
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 <= 100000)
return Sub5::solve(), 0;
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In function 'void setIn(std::string)':
sequence.cpp:7:31: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
7 | void setIn(string s) { freopen(s.c_str(),"r",stdin); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
sequence.cpp: In function 'void setOut(std::string)':
sequence.cpp:8:32: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
8 | 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:175:5: note: in expansion of macro 'file'
175 | 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:175:5: note: in expansion of macro 'file'
175 | 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... |