이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/** Im the best because i work as hard as i possibly can **/
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define all(x) (x).begin(),(x).end()
#define F first
#define S second
#define Mp make_pair
#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("in.txt" , "r+" , stdin) ; freopen("out.txt" , "w+" , stdout);
#define endl "\n"
const int N = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll mod2 = 998244353;
const ll inf = 4e18;
const int LOG = 22;
ll pw(ll a , ll b, ll M) { return (!b ? 1 : (b & 1 ? (a * pw(a * a % M, b / 2, M)) % M : pw(a * a % M, b / 2, M))); }
int n, k, par[205][N];
ll dp[2][N], ps[N], A[N];
inline ll calc(int l, int m, int r = n)
{
return 1ll * (ps[r] - ps[m]) * (ps[m] - ps[l - 1]);
}
void solve(int j, int l, int r, int le, int ri)
{
int j2 = j & 1;
if(l > r || le > ri) return;
int mid = (l + r) >> 1, opt = le;
ll best = -inf;
for(int i = le; i <= min(mid, ri); i ++)
{
ll cu = dp[1 ^ j2][i - 1] + calc(i, mid);
if(cu > best)
{
best = cu;
opt = i;
}
}
dp[j2][mid] = best;
par[j][mid] = opt - 1;
if(j > mid)
{
dp[j2][mid] = -inf;
par[j][mid] = -1;
}
solve(j, l, mid - 1, le, opt);
solve(j, mid + 1, r, opt, ri);
}
int main()
{
scanf("%d%d", &n, &k);
k ++;
for(int i = 1; i <= n; i ++)
{
scanf("%lld", &A[i]);
ps[i] = ps[i - 1] + A[i];
}
for(int i = 1; i < N; i ++) dp[0][i] = -inf;
for(int j = 1; j <= k; j ++)
{
solve(j, 1, n, 1, n);
}
printf("%lld\n", dp[k & 1][n]);
vector < int > ans;
int i = n, j = k;
while(j > 0 && i > 0 && par[j][i] > 0)
{
ans.push_back(par[j][i]);
i = par[j][i];
j --;
}
reverse(all(ans));
for(auto x : ans) printf("%d ", x);
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In function 'int main()':
sequence.cpp:64:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
64 | scanf("%d%d", &n, &k);
| ~~~~~^~~~~~~~~~~~~~~~
sequence.cpp:68:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
68 | scanf("%lld", &A[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... |