이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int NMAX = 1005;
const int KMAX = 202;
int v[NMAX];
ll sp[NMAX];
ll dp[NMAX][KMAX];
int last[NMAX][KMAX];
struct line{
ll slope, yIntercept;
line() {}
line(ll slope, ll yIntercept) : slope(slope), yIntercept(yIntercept) {}
ll val(int x){
return slope * x + yIntercept;
}
ll intersect(const line &other){
if(slope == other.slope) return 0;
return (other.yIntercept - yIntercept) / (slope - other.slope);
}
};
struct CHT{
deque < pair < pair <line, int>, int > > dq;
void add(line l, int id){
while(dq.size() > 1 && dq.back().first.second >= dq.back().first.first.intersect(l)) dq.pop_back();
if(dq.empty()){
dq.push_back({{l, 0}, id});
return;
}
dq.push_back({{l, dq.back().first.first.intersect(l)}, id});
}
pair <int, ll> query(int x){
while(dq.size() > 1){
if(dq[1].first.second <= x) dq.pop_front();
else break;
}
return {dq[0].second, dq[0].first.first.val(x)};
}
};
CHT cht[KMAX];
vector <int> sol;
int main()
{
int n,i,k,t;
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> k;
for(i = 1; i <= n; i++){
cin >> v[i];
sp[i] = sp[i - 1] + v[i];
}
for(i = 1; i < n; i++){
dp[i][1] = sp[i] * (sp[n] - sp[i]);
for(int e = 2; e <= min(k, i); e++){
pair <int, ll> r = cht[e - 1].query(sp[i]);
dp[i][e] = r.second + sp[i] * (sp[n] - sp[i]);
last[i][e] = r.first;
}
for(int e = 1; e <= min(k, i); e++)
cht[e].add(line(sp[i], dp[i][e] - sp[i] * sp[n]), i);
}
ll mx = 0;
int poz = 1;
for(i = k; i < n; i++){
if(dp[i][k] > mx){
mx = dp[i][k];
poz = i;
}
}
sol.push_back(poz);
while(k > 1){
sol.push_back(last[poz][k]);
poz = last[poz][k];
k--;
}
cout << mx << "\n";
for(auto x : sol) cout << x << " ";
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In function 'int main()':
sequence.cpp:54:15: warning: unused variable 't' [-Wunused-variable]
54 | int n,i,k,t;
| ^
# | 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... |