이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 1; i <= n; ++i)
#define forn(i, l, r) for(int i = l; i <= r; ++i)
#define ford(i, r, l) for(int i = r; i >= l; --i)
#define FOR(i, n) for(int i = 0; i < n; ++i)
#define fi first
#define se second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define endl "\n"
#define task "split"
#define sz(a) int(a.size())
#define C(x, y) make_pair(x, y)
#define all(a) (a).begin(), (a).end()
#define bit(i, mask) (mask >> i & 1)
template<typename T> bool maximize(T &res, const T &val) { if (res < val){ res = val; return true; }; return false; }
template<typename T> bool minimize(T &res, const T &val) { if (res > val){ res = val; return true; }; return false; }
inline int readInt() {char c;while(c=getchar(),c!='-'&&(c<'0'||c>'9'));bool sign=(c=='-');if(sign)c=getchar();int n=c-'0';while(c=getchar(),c>='0'&&c<='9')n=10*n+c-'0';return(!sign)?n:-n;}
inline ll readLong() {char c;while(c=getchar(),c!='-'&&(c<'0'||c>'9'));bool sign=(c=='-');if(sign)c=getchar();ll n=c-'0';while(c=getchar(),c>='0'&&c<='9')n=10*n+c-'0';return(!sign)?n:-n;}
inline string readString() {char c;while(c=getchar(),c==' '||c=='\n'||c=='\t');string s({c});while(c=getchar(),c!=EOF&&c!=' '&&c!='\n'&&c!='\t')s+=c;return s;}
const int N = 1e5 + 4;
const int M = 3e5 + 3;
const int INF = 1e9 + 3;
const ll LINF = 1e18;
const int A = 26 + 2;
const int K = 200 + 3;
int n, k;
int a[N];
int sum[N];
ll dp[N][2];
int opt[N][K];
int j = 0;
int f = 0;
double slope(pll x, pll y)
{
return double (1) * (y.se - x.se) / (x.fi - y.fi);
}
ll get(pll line, ll x) {return line.fi * x + line.se;}
pll line[N];
deque<int> cht;
bool bad(pll x, pll y, pll z)
{
if(z.fi == y.fi) return z.se >= y.se;
return slope(x, y) >= slope(y, z);
}
void solve()
{
cin >> n >> k;
rep(i, n) cin >> a[i], sum[i] = sum[i - 1] + a[i];
dp[0][0] = 0;
rep(i, n) dp[i][0] = -LINF;
rep(_, k + 1)
{
f ^= 1;
++j;
forn(i, 0, n) dp[i][f] = -LINF;
while(sz(cht)) cht.pop_back();
rep(i, n)
line[i] = {sum[i - 1], dp[i - 1][f ^ 1] - 1ll * sum[i - 1] * sum[i - 1]};
rep(i, n )
{
while(sz(cht) >= 2 && bad(line[cht[sz(cht) - 2]], line[cht.back()], line[i]))
cht.pop_back();
cht.push_back(i);
while(sz(cht) >= 2 && get(line[cht[0]], sum[i]) <= get(line[cht[1]], sum[i]))
cht.pop_front();
maximize(dp[i][f], get(line[cht[0]], sum[i]));
opt[i][j] = cht[0];
}
}
cout << dp[n][f] << endl;
int x = n;
ford(i, k, 1)
{
x = opt[x][i + 1] - 1;
cout << x << " ";
}
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int TC = 1;
if(fopen(task".inp", "r"))
{
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
while(TC--)
{
solve();
cout << endl;
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In function 'int main()':
sequence.cpp:112:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
112 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:113:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
113 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |