This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, k;
struct Func{
int p;
double q;
Func(){}
Func(int _p, double _q): p(_p), q(_q) {}
double operator()(int x) const{
return pow(x-p, (double)k/2) + q;
}
};
int cross(const Func &f, const Func &g){
int l = g.p + 1, r = n, ans = n+1;
while(l<=r){
int mid = (l+r)>>1;
if (f(mid) >= g(mid)) ans = mid, r = mid-1;
else l = mid+1;
}
return ans;
}
struct Hull{
vector<Func> st;
void insert(Func g){
// printf("insert %d %f\n", g.p, g.q);
while(st.size() >= 2){
if (cross(*++st.rbegin(), st.back()) > cross(st.back(), g)) break;
st.pop_back();
}
st.push_back(g);
}
double query(int x){
int l = 0, r = (int)st.size()-2, ans = (int)st.size()-1;
while(l<=r){
int mid = (l+r)>>1;
if (st[mid](x) >= st[mid+1](x)) ans = mid, r = mid-1;
else l = mid+1;
}
// for (int i=0;i<(int)st.size();i++) printf("%f ", st[i](x));
// printf("\n");
// printf("return: %f \n", st[ans](x));
return st[ans](x);
}
}H[1001000];
int a[1001000], idx[1001000];
vector<int> V[1001000];
double dp[1001000];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> k >> n;
for (int i=1;i<=n;i++){
cin >> a[i];
V[a[i]].push_back(i);
idx[i] = (int)V[a[i]].size()-1;
}
if (k==2){
printf("%.15f\n", (double)n);
return 0;
}
for (int i=1;i<=n;i++){
// printf(" ok %d %f\n", idx[i]-1, dp[i-1]);
H[a[i]].insert(Func(idx[i]-1, dp[i-1]));
dp[i] = H[a[i]].query(idx[i]);
// printf("%d -> %.15f\n", i, dp[i]);
}
printf("%.15f\n", dp[n]);
}
# | 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... |