이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define newl '\n'
const int N = 1e5 + 10;
const int K = 2e2 + 10;
const int V = 1e7 + 10;
const long long INF = 1e18;
const long long M = 1e9 + 7;
struct Line{
long long a,b,it;
long long operator ()(long long x){
return a * x + b;
}
long long intersect(const Line &rhs) const {
assert(a != rhs.a);
long long nume = b - rhs.b;
long long denom = rhs.a - a;
return nume / denom;
}
};
long long a[N + 1],f[K + 1][N + 1],trace[K + 1][N + 1],s[N + 1],n,k;
void readData(){
std::cin >> n >> k;
for(int i = 1;i <= n;++i){
std::cin >> a[i];
s[i] = s[i - 1] + a[i];
}
}
///(s[r] - s[l]) * (s[n] - s[r])
///(s[r] * s[n] - s[r] * s[r] - s[l] * s[n] + s[l] * s[r] + f[l]
///-s[r] * s[r] //meh not important
///s[r] * s[n] // also not important
///-s[l] * s[n] + s[l] * s[r] = -s[l] * s[n] + -s[l] * -s[r],-s[l] = a,(s[n] - s[r]) = x
///f[l] = b
bool dominate(const Line &l,const Line &o,const Line &o1){
///line lhs dominate line o
if(l.a == o.a){
return (l.b >= o.b);
}
long long inter = l.intersect(o1);
long long inter1 = o.intersect(o1);
return inter >= inter1;
}
void solve(){
long long it = 0;
std::deque<Line> dq;
std::vector<long long> pr(n + 1,0);
for(int i = 1;i <= k;++i){
std::vector<long long> f(n + 1,0);
for(int j = 0;j <= n;++j){
if(j > 0){
long long x = s[n] - s[j];
while((int)dq.size() >= 2 && dq.end()[-1](x) <= dq.end()[-2](x)){
dq.pop_back();
}
f[j] = dq.end()[-1](x) + s[j] * (x);
trace[i][j] = dq.end()[-1].it;
// if(i == 3 && j == 5){
// std::cerr << "IMP " << f[j] << newl;
// }
}
Line cur = {-s[j],pr[j],j};
while((int)dq.size() >= 2 && dominate(cur,dq[0],dq[1])){
dq.pop_front();
}
dq.push_front(cur);
if(f[j] > f[it]){
it = j;
}
// std::cerr << i << " " << j << newl;
}
dq.clear();
pr.swap(f);
}
std::cout << pr[it] << newl;
int t = k;
while(t > 0){
std::cout << it << " ";
it = trace[t--][it];
}
}
int main(){
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);std::cout.tie(nullptr);
readData();
solve();
return 0;
}
# | 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... |