이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
long long t, n, m, k, a[100005], dp[100005], sum[100005], mod = 1e9 + 7, inf = 1e18;
int trace[100005][205];
string s;
mt19937_64 rng;
struct lines{
long long a, b, index;
lines(long long a = 0, long long b = 0, long long index = 0) : a(a), b(b), index(index){}
long long f(long long x){
return 1LL * a * x + b;
};
};
long double slope(lines ln1, lines ln2) {
return (long double) (ln2.b - ln1.b) / (long double) (ln2.a - ln1.a);
}
struct cht{
vector<lines> lns;
long long ptr = 0;
void add(long long a, long long b, long long index){
lines ln(a, b, index);
while(lns.size() > 1 && (slope(lns[lns.size() - 2], lns.back()) - slope(lns[lns.size() - 2], ln)) <= 1e-18) lns.pop_back();
lns.push_back(ln); ptr = min(ptr, (long long) lns.size() - 1);
}
pair<long long, long long> get(long long x){
if(lns.empty()) return {-inf, 0};
while (ptr + 1 < lns.size() && lns[ptr].f(x) <= lns[ptr + 1].f(x)) ptr++;
return {lns[ptr].f(x), lns[ptr].index};
}
};
cht osu[5];
void solve(){
osu[0].add(0, 0, 0);
for(long long j = 1; j <= k; j++){
for(long long i = 1; i <= n; i++){
long long num = sum[i] * sum[n] - sum[i] * sum[i];
pair<long long, long long> x = osu[0].get(sum[i] - sum[n]);
dp[i] = x.fi + num;
trace[i][j] = x.se;
osu[1].add(sum[i], dp[i], i);
// cout << dp[i] <<" "<< i <<" "<< j <<" "<< trace[i][j] << "\n";
}
osu[0] = osu[1];
osu[1].lns.clear(); osu[1].ptr = 0;
}
long long answer = 0;
for(long long i = 1; i <= n; i++) answer = max(answer, dp[i]);
cout << answer << "\n";
for(long long i = n; i >= 1; i--){
if(answer != dp[i]) continue;
vector<long long> v;
int x = i;
for(long long i = k; i >= 1; i--){
// cout << x <<" ";
v.push_back(x);
x = max(1, min(trace[x][i], x - 1));
}
reverse(v.begin(), v.end());
for(auto i: v) cout << i <<" ";
return;
}
}
signed main(){
ios_base::sync_with_stdio(NULL); cin.tie(nullptr); cout.tie(nullptr);
rng.seed((long long)main ^ time(0));
#ifdef Kawaii
auto starttime = chrono::high_resolution_clock::now();
#endif
cin >> n >> k;
// n = 1e5, k = 200;
for(long long i = 1; i <= n; i++){
cin >> a[i];
// a[i] = rng() % 10001;
sum[i] = sum[i - 1] + a[i];
}
solve();
#ifdef Kawaii
auto endtime = chrono::high_resolution_clock::now();
auto duration = chrono::duration_cast<chrono::milliseconds>(endtime - starttime).count();
cout << "\n=====" << "\nUsed: " << duration << " ms\n";
#endif
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In member function 'std::pair<long long int, long long int> cht::get(long long int)':
sequence.cpp:33:24: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<lines>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
33 | while (ptr + 1 < lns.size() && lns[ptr].f(x) <= lns[ptr + 1].f(x)) ptr++;
| ~~~~~~~~^~~~~~~~~~~~
# | 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... |