이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
/// 500 485 462 A4
typedef long long int ll;
typedef long double ld;
#define pb push_back
#define pii pair < ll , ll >
#define F first
#define S second
#define endl '\n'
//#define int long long
#define sync ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#define kill(x) return cout<<x<<'\n', 0;
using namespace std;
const int N=1e5+100,K=210;
ll par[N];
ll a[N];
ll dp[2][N];
int pd[K][N];
void solve(ll l,ll r,ll L,ll R,ll k){
if (l>r) return ;
ll mid=(r+l)/2;
ll best=L,val=-1;
for (int i=L;i<=min(mid,R);i++){
if (val<(par[mid]-par[i-1])*(par[i-1])+dp[((k+1)&1)][i-1]){
best=i;
val=(par[mid]-par[i-1])*(par[i-1])+dp[((k+1)&1)][i-1];
}
}
//cout << l << " " << r << " " << k << " " << best << " " << val << endl;
dp[(k&1)][mid]=val;
pd[k][mid]=best-1;
solve(l,mid-1,L,best,k);
solve(mid+1,r,best,R,k);
}
ll vis[N];
int32_t main(){
sync;
ll n,k;
cin >> n >> k;
// k++;
for (int i=1;i<=n;i++){
cin >> a[i];
par[i]=par[i-1]+a[i];
}
for (int i=1;i<=k;i++){
solve(i+1,n,1,n,i);
}
cout << dp[(k&1)][n] << endl;
vector <int> ans;
ll x=pd[k][n];
ll zz=k;
if (x>0)
ans.pb(x);
while(k!=1){
// cout << x << " " << k << endl;
k--;
x=pd[k][x];
if (x<=0) break;
ans.pb(x);
}
for (auto u : ans){
vis[u]=1;
}
for (int i=1;i<=n;i++){
if (!vis[i] && ans.size()<zz){
ans.pb(i);
}
}
sort(ans.begin(),ans.end());
// reverse(ans.begin(),ans.end());
for (auto u : ans) cout << u << " ";
cout << endl;
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In function 'int32_t main()':
sequence.cpp:67:34: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'll' {aka 'long long int'} [-Wsign-compare]
67 | if (!vis[i] && ans.size()<zz){
| ~~~~~~~~~~^~~
# | 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... |