이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
//#define int long long
typedef long long ll;
ll dp[2][100005];
int pre[205][100005];
ll P[100005], A[100005];
deque<pair<pair<ll, ll> , int> > dq;
ll f(pair<ll, ll> x, ll y){
return x.first*y + x.second;
}
long double intersect(ll a, ll b, ll c, ll d){
return (long double)(d-b)/(a-c);
}
void ins(ll m , ll c, int in){
while(dq.size() > 1){
int s = dq.size();
if(intersect(dq.back().first.first, dq.back().first.second,m,c) <= intersect(dq[s-2].first.first, dq[s-2].first.second, m,c))dq.pop_back();
else break;
}
dq.push_back(make_pair(make_pair(m,c), in));
}
pair<ll, int> query(ll x){
while(dq.size() > 1){
if(f(dq[0].first, x) < f(dq[1].first, x))dq.pop_front();
else break;
}
return {f(dq[0].first, x), dq[0].second};
}
main(){
ios::sync_with_stdio(0);cin.tie(0);
int n,k;cin >> n >> k;
for(int i=1;i<=n;i++)cin >> A[i], P[i] = P[i-1] + A[i];
for(int i=1;i<=n;i++)dp[1][i] = (P[n] - P[i])*P[i];
for(int i=2;i<=k;i++){
int tmp = i %2;
for(int j=i;j<=n-(k-i)+1;j++){
ins(P[j-1] + P[n], -P[n] * P[j-1] + dp[1-tmp][j-1], j-1);
pair<ll,ll>x = query(P[j]);
dp[tmp][j] = x.first- P[j] * P[j];
pre[i][j] = x.second;
}
dq.clear();
}
ll maxi = -1e9; int in;
for(int i=1;i<=n;i++)if(dp[k%2][i] > maxi)maxi = dp[k%2][i], in = i;
cout << maxi << '\n';
cout << in << " ";
int p = pre[k][in];
while(p)cout << p << " ", k--, p = pre[k][p];
/*
cout << "\n\n";
for(int i=1;i<=k;i++){
for(int j=i;j<=n;j++)cout << dp[i][j].first << " " << dp[i][j].second << " ";
cout << '\n';
}
*/
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp:31:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
31 | main(){
| ^~~~
sequence.cpp: In function 'int main()':
sequence.cpp:50:6: warning: 'in' may be used uninitialized in this function [-Wmaybe-uninitialized]
50 | int p = pre[k][in];
| ^
# | 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... |