답안 #729327

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
729327 2023-04-23T19:36:51 Z tch1cherin Let's Win the Election (JOI22_ho_t3) C++17
77 / 100
2500 ms 1480 KB
#include <bits/stdc++.h>
using namespace std;
 
const long double INF = 1e18;
 
void solve() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n), b(n);
  bool good = true;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
    good &= b[i] == -1 || b[i] == a[i];
  }
  if (!good && n != k) {
    vector<int> order(n);
    iota(order.begin(), order.end(), 0);
    sort(order.begin(), order.end(), [&](int i, int j) {
      return b[i] < b[j];
    });
    vector<int> new_a(n), new_b(n);
    for (int i = 0; i < n; i++) {
      new_a[i] = a[order[i]];
      new_b[i] = b[order[i]];
    }
    a = new_a, b = new_b;
    long double ans = INF;
    for (int c = 0; c <= k; c++) {
      vector dp(k + 1, vector<long double>(c + 1, INF));
      dp[0][0] = 0;
      for (int i = 0; i < n; i++) {
        vector new_dp(n + 1, vector<long double>(c + 1, INF));
        for (int j = 0; j <= k; j++) {
          for (int l = 0; l <= c; l++) {
            if (j + 1 <= k && l + 1 <= c && b[i] != -1) {
              new_dp[j + 1][l + 1] = min(new_dp[j + 1][l + 1], dp[j][l] + (long double)b[i] / (l + 1));
            }
            if (j + 1 <= k) {
              new_dp[j + 1][l] = min(new_dp[j + 1][l], dp[j][l] + (long double)a[i] / (c + 1));
            }
            new_dp[j][l] = min(new_dp[j][l], dp[j][l]);
          }
        }
        dp = new_dp;
      }
      for (int i = k; i <= n; i++) {
        ans = min(ans, dp[i][c]);
      }
    }
    cout << ans << "\n";
  } else if (good) {
    vector<int> order(n);
    iota(order.begin(), order.end(), 0);
    sort(order.begin(), order.end(), [&](int i, int j) {
      return a[i] < a[j];
    });
    long double ans = 1e18;
    for (int max_c = 0; max_c <= n; max_c++) {
      int c = 1, cnt = 0;
      long double res = 0;
      for (int i : order) {
        if (a[i] == b[i] && c <= max_c) {
          res += (long double)a[i] / c;
          c++;
          cnt++;
        }
      }
      for (int i : order) {
        if (a[i] != b[i] && cnt < k) {
          res += (long double)a[i] / c;
          cnt++;
        }
      }
      if (cnt >= k) {
        ans = min(ans, res);
      }
    }
    cout << ans << "\n";
  } else {
    vector<int> order(n);
    iota(order.begin(), order.end(), 0);
    sort(order.begin(), order.end(), [&](int i, int j) {
      return b[i] < b[j];
    });
    vector<int> new_a(n), new_b(n);
    for (int i = 0; i < n; i++) {
      new_a[i] = a[order[i]];
      new_b[i] = b[order[i]];
    }
    a = new_a, b = new_b;
    long double ans = INF;
    for (int c = 0; c <= k; c++) {
      vector<long double> dp(c + 1, INF);
      dp[0] = 0;
      for (int i = 0; i < n; i++) {
        vector<long double> new_dp(c + 1, INF);
        for (int l = 0; l <= c; l++) {
          if (l + 1 <= c && b[i] != -1) {
            new_dp[l + 1] = min(new_dp[l + 1], dp[l] + (long double)b[i] / (l + 1));
          }
          new_dp[l] = min(new_dp[l], dp[l] + (long double)a[i] / (c + 1));
        }
        dp = new_dp;
      }
      ans = min(ans, dp[c]);
    }
    cout << ans << "\n";
  }
}
 
int main() {
  cout << fixed << setprecision(9);
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 2 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 2 ms 212 KB Output is correct
17 Correct 2 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 2 ms 340 KB Output is correct
31 Correct 2 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 2 ms 340 KB Output is correct
34 Correct 2 ms 340 KB Output is correct
35 Correct 2 ms 340 KB Output is correct
36 Correct 2 ms 340 KB Output is correct
37 Correct 2 ms 340 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 2 ms 340 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 2 ms 340 KB Output is correct
31 Correct 2 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 2 ms 340 KB Output is correct
34 Correct 2 ms 340 KB Output is correct
35 Correct 2 ms 340 KB Output is correct
36 Correct 2 ms 340 KB Output is correct
37 Correct 2 ms 340 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 2 ms 340 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 16 ms 428 KB Output is correct
42 Correct 14 ms 364 KB Output is correct
43 Correct 47 ms 428 KB Output is correct
44 Correct 46 ms 460 KB Output is correct
45 Correct 169 ms 616 KB Output is correct
46 Correct 163 ms 876 KB Output is correct
47 Correct 312 ms 828 KB Output is correct
48 Correct 293 ms 808 KB Output is correct
49 Correct 570 ms 1116 KB Output is correct
50 Correct 507 ms 984 KB Output is correct
51 Correct 9 ms 340 KB Output is correct
52 Correct 9 ms 340 KB Output is correct
53 Correct 302 ms 812 KB Output is correct
54 Correct 327 ms 808 KB Output is correct
55 Correct 319 ms 788 KB Output is correct
56 Correct 309 ms 824 KB Output is correct
57 Correct 341 ms 816 KB Output is correct
58 Correct 326 ms 820 KB Output is correct
59 Correct 372 ms 848 KB Output is correct
60 Correct 338 ms 928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 990 ms 392 KB Output is correct
2 Correct 975 ms 396 KB Output is correct
3 Correct 1049 ms 392 KB Output is correct
4 Correct 942 ms 384 KB Output is correct
5 Correct 993 ms 396 KB Output is correct
6 Correct 1027 ms 396 KB Output is correct
7 Correct 922 ms 396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 2 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 2 ms 212 KB Output is correct
17 Correct 2 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 1 ms 212 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 1 ms 340 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 2 ms 340 KB Output is correct
42 Correct 2 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 1 ms 212 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 2 ms 340 KB Output is correct
49 Correct 1 ms 340 KB Output is correct
50 Correct 2 ms 340 KB Output is correct
51 Correct 2 ms 340 KB Output is correct
52 Correct 1 ms 340 KB Output is correct
53 Correct 2 ms 340 KB Output is correct
54 Correct 2 ms 340 KB Output is correct
55 Correct 2 ms 340 KB Output is correct
56 Correct 2 ms 340 KB Output is correct
57 Correct 2 ms 340 KB Output is correct
58 Correct 1 ms 340 KB Output is correct
59 Correct 2 ms 340 KB Output is correct
60 Correct 1 ms 340 KB Output is correct
61 Correct 16 ms 428 KB Output is correct
62 Correct 14 ms 364 KB Output is correct
63 Correct 47 ms 428 KB Output is correct
64 Correct 46 ms 460 KB Output is correct
65 Correct 169 ms 616 KB Output is correct
66 Correct 163 ms 876 KB Output is correct
67 Correct 312 ms 828 KB Output is correct
68 Correct 293 ms 808 KB Output is correct
69 Correct 570 ms 1116 KB Output is correct
70 Correct 507 ms 984 KB Output is correct
71 Correct 9 ms 340 KB Output is correct
72 Correct 9 ms 340 KB Output is correct
73 Correct 302 ms 812 KB Output is correct
74 Correct 327 ms 808 KB Output is correct
75 Correct 319 ms 788 KB Output is correct
76 Correct 309 ms 824 KB Output is correct
77 Correct 341 ms 816 KB Output is correct
78 Correct 326 ms 820 KB Output is correct
79 Correct 372 ms 848 KB Output is correct
80 Correct 338 ms 928 KB Output is correct
81 Correct 990 ms 392 KB Output is correct
82 Correct 975 ms 396 KB Output is correct
83 Correct 1049 ms 392 KB Output is correct
84 Correct 942 ms 384 KB Output is correct
85 Correct 993 ms 396 KB Output is correct
86 Correct 1027 ms 396 KB Output is correct
87 Correct 922 ms 396 KB Output is correct
88 Correct 1789 ms 1296 KB Output is correct
89 Correct 1886 ms 1480 KB Output is correct
90 Execution timed out 2540 ms 1244 KB Time limit exceeded
91 Halted 0 ms 0 KB -