#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{
double tmp = x-p;
if (k==3) return sqrt(tmp*tmp*tmp) + q;
return tmp*tmp + 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;
vector<double> C;
void insert(Func g){
// printf("insert %d %f\n", g.p, g.q);
double tmp = 0;
while(st.size() >= 2){
tmp = cross(st.back(), g);
if (C.back() > tmp) break;
st.pop_back();
C.pop_back();
}
if (st.size() >= 2) C.push_back(tmp);
else if (st.size() == 1) C.push_back(cross(st.back(), g));
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 (x >= C[mid]) 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], cnt[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];
cnt[a[i]]++;
idx[i] = cnt[a[i]];
}
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 |
1 |
Correct |
23 ms |
47316 KB |
Output is correct |
2 |
Correct |
22 ms |
47292 KB |
Output is correct |
3 |
Correct |
22 ms |
47360 KB |
Output is correct |
4 |
Correct |
22 ms |
47340 KB |
Output is correct |
5 |
Correct |
22 ms |
47360 KB |
Output is correct |
6 |
Correct |
22 ms |
47332 KB |
Output is correct |
7 |
Correct |
22 ms |
47332 KB |
Output is correct |
8 |
Correct |
25 ms |
47308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
47316 KB |
Output is correct |
2 |
Correct |
22 ms |
47292 KB |
Output is correct |
3 |
Correct |
22 ms |
47368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
47316 KB |
Output is correct |
2 |
Correct |
22 ms |
47292 KB |
Output is correct |
3 |
Correct |
22 ms |
47368 KB |
Output is correct |
4 |
Correct |
23 ms |
47316 KB |
Output is correct |
5 |
Correct |
22 ms |
47292 KB |
Output is correct |
6 |
Correct |
22 ms |
47368 KB |
Output is correct |
7 |
Correct |
23 ms |
47372 KB |
Output is correct |
8 |
Correct |
22 ms |
47380 KB |
Output is correct |
9 |
Correct |
23 ms |
47340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
47316 KB |
Output is correct |
2 |
Correct |
22 ms |
47292 KB |
Output is correct |
3 |
Correct |
22 ms |
47368 KB |
Output is correct |
4 |
Correct |
23 ms |
47316 KB |
Output is correct |
5 |
Correct |
22 ms |
47292 KB |
Output is correct |
6 |
Correct |
22 ms |
47368 KB |
Output is correct |
7 |
Correct |
23 ms |
47372 KB |
Output is correct |
8 |
Correct |
22 ms |
47380 KB |
Output is correct |
9 |
Correct |
23 ms |
47340 KB |
Output is correct |
10 |
Correct |
24 ms |
47316 KB |
Output is correct |
11 |
Correct |
24 ms |
47460 KB |
Output is correct |
12 |
Correct |
23 ms |
47412 KB |
Output is correct |
13 |
Correct |
24 ms |
47520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
244 ms |
62912 KB |
Output is correct |
2 |
Correct |
270 ms |
62924 KB |
Output is correct |
3 |
Correct |
116 ms |
55096 KB |
Output is correct |
4 |
Correct |
169 ms |
55464 KB |
Output is correct |
5 |
Correct |
169 ms |
82516 KB |
Output is correct |
6 |
Correct |
219 ms |
80716 KB |
Output is correct |
7 |
Correct |
217 ms |
80732 KB |
Output is correct |
8 |
Correct |
222 ms |
80720 KB |
Output is correct |
9 |
Correct |
237 ms |
80736 KB |
Output is correct |
10 |
Correct |
222 ms |
80716 KB |
Output is correct |
11 |
Correct |
199 ms |
62936 KB |
Output is correct |
12 |
Correct |
188 ms |
63052 KB |
Output is correct |
13 |
Correct |
189 ms |
62968 KB |
Output is correct |
14 |
Correct |
309 ms |
63312 KB |
Output is correct |
15 |
Correct |
308 ms |
63252 KB |
Output is correct |
16 |
Correct |
306 ms |
63332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
47316 KB |
Output is correct |
2 |
Correct |
22 ms |
47292 KB |
Output is correct |
3 |
Correct |
22 ms |
47360 KB |
Output is correct |
4 |
Correct |
22 ms |
47340 KB |
Output is correct |
5 |
Correct |
22 ms |
47360 KB |
Output is correct |
6 |
Correct |
22 ms |
47332 KB |
Output is correct |
7 |
Correct |
22 ms |
47332 KB |
Output is correct |
8 |
Correct |
25 ms |
47308 KB |
Output is correct |
9 |
Correct |
23 ms |
47316 KB |
Output is correct |
10 |
Correct |
22 ms |
47292 KB |
Output is correct |
11 |
Correct |
22 ms |
47368 KB |
Output is correct |
12 |
Correct |
23 ms |
47316 KB |
Output is correct |
13 |
Correct |
22 ms |
47292 KB |
Output is correct |
14 |
Correct |
22 ms |
47368 KB |
Output is correct |
15 |
Correct |
23 ms |
47372 KB |
Output is correct |
16 |
Correct |
22 ms |
47380 KB |
Output is correct |
17 |
Correct |
23 ms |
47340 KB |
Output is correct |
18 |
Correct |
24 ms |
47316 KB |
Output is correct |
19 |
Correct |
24 ms |
47460 KB |
Output is correct |
20 |
Correct |
23 ms |
47412 KB |
Output is correct |
21 |
Correct |
24 ms |
47520 KB |
Output is correct |
22 |
Correct |
244 ms |
62912 KB |
Output is correct |
23 |
Correct |
270 ms |
62924 KB |
Output is correct |
24 |
Correct |
116 ms |
55096 KB |
Output is correct |
25 |
Correct |
169 ms |
55464 KB |
Output is correct |
26 |
Correct |
169 ms |
82516 KB |
Output is correct |
27 |
Correct |
219 ms |
80716 KB |
Output is correct |
28 |
Correct |
217 ms |
80732 KB |
Output is correct |
29 |
Correct |
222 ms |
80720 KB |
Output is correct |
30 |
Correct |
237 ms |
80736 KB |
Output is correct |
31 |
Correct |
222 ms |
80716 KB |
Output is correct |
32 |
Correct |
199 ms |
62936 KB |
Output is correct |
33 |
Correct |
188 ms |
63052 KB |
Output is correct |
34 |
Correct |
189 ms |
62968 KB |
Output is correct |
35 |
Correct |
309 ms |
63312 KB |
Output is correct |
36 |
Correct |
308 ms |
63252 KB |
Output is correct |
37 |
Correct |
306 ms |
63332 KB |
Output is correct |
38 |
Correct |
419 ms |
99124 KB |
Output is correct |
39 |
Correct |
514 ms |
62928 KB |
Output is correct |
40 |
Correct |
580 ms |
63096 KB |
Output is correct |
41 |
Correct |
209 ms |
55060 KB |
Output is correct |
42 |
Correct |
341 ms |
55604 KB |
Output is correct |
43 |
Correct |
233 ms |
82592 KB |
Output is correct |
44 |
Correct |
423 ms |
80692 KB |
Output is correct |
45 |
Correct |
339 ms |
63012 KB |
Output is correct |
46 |
Correct |
314 ms |
62964 KB |
Output is correct |
47 |
Correct |
309 ms |
62988 KB |
Output is correct |
48 |
Correct |
579 ms |
63016 KB |
Output is correct |
49 |
Correct |
629 ms |
62964 KB |
Output is correct |
50 |
Correct |
609 ms |
63036 KB |
Output is correct |