이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |