Submission #730367

#TimeUsernameProblemLanguageResultExecution timeMemory
730367tch1cherinLet's Win the Election (JOI22_ho_t3)C++17
56 / 100
2550 ms5796 KiB
#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);
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
  }
  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;
  auto Solve = [&n, &k, &a, &b](int c) {
    long double ans = INF;
    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]);
    }
    return ans;
  };
  int l = 0, r = k;
  while (r - l > 2) {
    int m1 = l + (r - l) / 3;
    int m2 = r - (r - l) / 3;
    if (Solve(m1) < Solve(m2)) {
      r = m2;
    } else {
      l = m1;
    }
  }
  cout << min({Solve(l), Solve(l + 1), Solve(l + 2)}) << "\n";
}
 
int main() {
  cout << fixed << setprecision(9);
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  solve();
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...