#include <bits/stdc++.h>
#define int long long
#define inf (1 << 30)
#define ld long double
using namespace std;
const int N = 113;
int a[N], b[N], s[N];
int id_x[N], id_y[N];
int vid_x[N], vid_y[N];
struct Pair{
int a, b, id;
Pair() = default;
Pair(int _a, int _b, int _i) : a(_a), b(_b), id(_i){};
};
vector<Pair> vp;
long double dp[N][N][N];
int cnt[N][N];
int n, K;
long double f(int l){
for(int i = 0; i < n; i++){
s[i + 1] = s[i] + a[id_x[i]];
}
long double ans = inf;
for(int i = 0; i <= n; i++){
for(int j = 0; j <= n; j++){
for(int k = 0; k <= n; k++){
dp[i][j][k] = inf;
}
}
}
dp[0][0][0] = 0;
for(int i = 0; i <= n; i++){
for(int j = 0; j <= n; j++){
for(int k = 0; k <= n; k++){
if(i > 0){
if(vid_y[id_x[i - 1]] < j){
dp[i][j][k] = min(dp[i][j][k], dp[i - 1][j][k] - (ld) a[id_x[i - 1]] / (l + 1));
}else{
dp[i][j][k] = min(dp[i][j][k], dp[i - 1][j][k]);
}
}
if(j > 0){
if(vid_x[id_y[j - 1]] < i){
dp[i][j][k] = min(dp[i][j][k], dp[i][j - 1][k]);
}else{
if(k > 0 && b[id_y[j - 1]] != inf){
dp[i][j][k] = min(dp[i][j][k], dp[i][j - 1][k - 1] + (ld) b[id_y[j - 1]] / k);
}
}
}
}
if(i + j - cnt[i][j] == K){
ans = min(ans, dp[i][j][l] + (ld) s[i] / (l + 1));
}
}
}
return ans;
}
int32_t main(){
cin >> n >> K;
for(int i = 0; i < n; i++){
cin >> a[i] >> b[i];
b[i] = (b[i] == -1) ? inf : b[i];
vp.push_back({a[i], b[i], i});
}
sort(vp.begin(), vp.end(), [](Pair x, Pair y){
return x.a < y.a || x.a == y.a && x.b < y.b;
});
for(int i = 0; i < n; i++){
id_x[i] = vp[i].id;
vid_x[vp[i].id] = i;
}
sort(vp.begin(), vp.end(), [](Pair x, Pair y){
return x.b < y.b || x.b == y.b && x.a < y.a;
});
for(int i = 0; i < n; i++){
id_y[i] = vp[i].id;
vid_y[vp[i].id] = i;
}
/*
for(int i = 0; i < n; i++){
cout << vid_x[i] << ' ' << vid_y[i] << ' ' << id_x[i] << ' ' << id_y[i] << endl;
}
*/
for(int i = 0; i < n; i++){
cnt[vid_x[i] + 1][vid_y[i] + 1] = 1;
}
/*
for(int i = 0; i <= n; i++){
for(int j = 0; j <= n; j++){
cout << cnt[i][j] << ' ';
}
cout << endl;
}
cout << endl;
*/
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
cnt[i][j] = cnt[i - 1][j] + cnt[i][j - 1] + cnt[i][j] - cnt[i - 1][j - 1];
}
}
int lb = 0, rb = K;
while(rb - lb > 2){
int m = (rb + lb) / 2;
ld f1 = f(m), f2 = f(m + 1);
if(f1 > f2){
lb = m;
}else{
rb = m + 1;
}
}
ld ans = min(min(f(lb), f((rb + lb) / 2)), f(rb));
cout << fixed << setprecision(15) << ans << endl;
}
Compilation message
Main.cpp: In lambda function:
Main.cpp:74:34: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
74 | return x.a < y.a || x.a == y.a && x.b < y.b;
| ~~~~~~~~~~~^~~~~~~~~~~~
Main.cpp: In lambda function:
Main.cpp:83:34: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
83 | return x.b < y.b || x.b == y.b && x.a < y.a;
| ~~~~~~~~~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Runtime error |
1 ms |
400 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Runtime error |
1 ms |
400 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
1108 KB |
Output is correct |
17 |
Correct |
2 ms |
1108 KB |
Output is correct |
18 |
Correct |
2 ms |
1108 KB |
Output is correct |
19 |
Correct |
2 ms |
1108 KB |
Output is correct |
20 |
Correct |
2 ms |
1108 KB |
Output is correct |
21 |
Correct |
2 ms |
1108 KB |
Output is correct |
22 |
Correct |
2 ms |
1108 KB |
Output is correct |
23 |
Correct |
3 ms |
1108 KB |
Output is correct |
24 |
Correct |
3 ms |
1108 KB |
Output is correct |
25 |
Correct |
3 ms |
1108 KB |
Output is correct |
26 |
Correct |
3 ms |
1108 KB |
Output is correct |
27 |
Correct |
3 ms |
1108 KB |
Output is correct |
28 |
Correct |
3 ms |
1108 KB |
Output is correct |
29 |
Correct |
3 ms |
1108 KB |
Output is correct |
30 |
Correct |
3 ms |
1048 KB |
Output is correct |
31 |
Correct |
3 ms |
1108 KB |
Output is correct |
32 |
Correct |
3 ms |
1108 KB |
Output is correct |
33 |
Correct |
3 ms |
1108 KB |
Output is correct |
34 |
Correct |
3 ms |
1108 KB |
Output is correct |
35 |
Correct |
3 ms |
1172 KB |
Output is correct |
36 |
Correct |
3 ms |
1108 KB |
Output is correct |
37 |
Correct |
3 ms |
1108 KB |
Output is correct |
38 |
Correct |
3 ms |
1108 KB |
Output is correct |
39 |
Correct |
4 ms |
1108 KB |
Output is correct |
40 |
Correct |
3 ms |
1108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
1108 KB |
Output is correct |
17 |
Correct |
2 ms |
1108 KB |
Output is correct |
18 |
Correct |
2 ms |
1108 KB |
Output is correct |
19 |
Correct |
2 ms |
1108 KB |
Output is correct |
20 |
Correct |
2 ms |
1108 KB |
Output is correct |
21 |
Correct |
2 ms |
1108 KB |
Output is correct |
22 |
Correct |
2 ms |
1108 KB |
Output is correct |
23 |
Correct |
3 ms |
1108 KB |
Output is correct |
24 |
Correct |
3 ms |
1108 KB |
Output is correct |
25 |
Correct |
3 ms |
1108 KB |
Output is correct |
26 |
Correct |
3 ms |
1108 KB |
Output is correct |
27 |
Correct |
3 ms |
1108 KB |
Output is correct |
28 |
Correct |
3 ms |
1108 KB |
Output is correct |
29 |
Correct |
3 ms |
1108 KB |
Output is correct |
30 |
Correct |
3 ms |
1048 KB |
Output is correct |
31 |
Correct |
3 ms |
1108 KB |
Output is correct |
32 |
Correct |
3 ms |
1108 KB |
Output is correct |
33 |
Correct |
3 ms |
1108 KB |
Output is correct |
34 |
Correct |
3 ms |
1108 KB |
Output is correct |
35 |
Correct |
3 ms |
1172 KB |
Output is correct |
36 |
Correct |
3 ms |
1108 KB |
Output is correct |
37 |
Correct |
3 ms |
1108 KB |
Output is correct |
38 |
Correct |
3 ms |
1108 KB |
Output is correct |
39 |
Correct |
4 ms |
1108 KB |
Output is correct |
40 |
Correct |
3 ms |
1108 KB |
Output is correct |
41 |
Correct |
212 ms |
18772 KB |
Output is correct |
42 |
Correct |
222 ms |
18828 KB |
Output is correct |
43 |
Correct |
248 ms |
18820 KB |
Output is correct |
44 |
Correct |
242 ms |
18820 KB |
Output is correct |
45 |
Correct |
282 ms |
18820 KB |
Output is correct |
46 |
Correct |
292 ms |
18820 KB |
Output is correct |
47 |
Correct |
287 ms |
18772 KB |
Output is correct |
48 |
Correct |
305 ms |
18824 KB |
Output is correct |
49 |
Correct |
301 ms |
18820 KB |
Output is correct |
50 |
Correct |
317 ms |
18824 KB |
Output is correct |
51 |
Correct |
343 ms |
18824 KB |
Output is correct |
52 |
Correct |
340 ms |
18828 KB |
Output is correct |
53 |
Correct |
295 ms |
18820 KB |
Output is correct |
54 |
Correct |
287 ms |
18820 KB |
Output is correct |
55 |
Correct |
317 ms |
18816 KB |
Output is correct |
56 |
Correct |
286 ms |
18828 KB |
Output is correct |
57 |
Correct |
305 ms |
18820 KB |
Output is correct |
58 |
Correct |
301 ms |
18824 KB |
Output is correct |
59 |
Correct |
342 ms |
18828 KB |
Output is correct |
60 |
Correct |
291 ms |
18824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Runtime error |
1 ms |
400 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |