Submission #785119

#TimeUsernameProblemLanguageResultExecution timeMemory
785119gun_ganLet's Win the Election (JOI22_ho_t3)C++17
72 / 100
2580 ms1018460 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
 
const int MX = 505;
 
int N, K;
int a[MX], b[MX];
 
vector<pair<int, int>> v;
 
int main() {
      cin.tie(0); ios_base::sync_with_stdio(0);
 
      cin >> N >> K;

      int tot = 0;
      for(int i = 1; i <= N; i++) {
            cin >> a[i] >> b[i];
            if(b[i] != -1) tot++;
            else b[i] = 1e9;
            v.push_back({b[i], a[i]});
      }
 
      sort(v.begin(), v.end());

      if(K == N) {
            vector<vector<double>> dp(N + 5, vector<double>(N + 5, 1e9));
            double ans = 1e9;
            for(int x = 0; x <= min(K, tot); x++) { // fixed collaborator
       
                  for(int i = 0; i < N + 5; i++)
                        for(int k = 0; k < N + 5; k++)
                              dp[i][k] = 1e9;
             
                  dp[0][0] = 0;
                  for(int i = 0; i < N; i++) {
                        for(int k = 0; k <= N; k++) { // num of collaborator
                              if(dp[i][k] == 1e9) continue;
 
                              // ambil vote
                              dp[i + 1][k] = min(dp[i + 1][k], dp[i][k] + (double) v[i].second / (x + 1));
                              
                              // ambil collaborator
                              if(v[i].first != 1e9) 
                                    dp[i + 1][k + 1] = min(dp[i + 1][k + 1], dp[i][k] + (double) v[i].first / (k + 1));
                        }
                  }
       
                  ans = min(ans, dp[N][x]);
            }
       
            cout << fixed << setprecision(15) << ans << '\n';
      } else {
            vector<vector<vector<double>>> dp(N + 5, vector<vector<double>> (N + 5, vector<double>(N + 5, 1e9)));
            double ans = 1e9;
            for(int x = 0; x <= min(K, tot); x++) { // fixed collaborator
       
                  for(int i = 0; i < N + 5; i++)
                        for(int j = 0; j < N + 5; j++)
                              for(int k = 0; k < N + 5; k++)
                                    dp[i][j][k] = 1e9;
             
                  dp[0][0][0] = 0;
                  for(int i = 0; i < N; i++) {
                        for(int j = 0; j <= N; j++) { // votes
                              for(int k = 0; k <= N; k++) { // num of collaborator
                                    if(dp[i][j][k] == 1e9) continue;
             
                                    // skip
                                    dp[i + 1][j][k] = min(dp[i + 1][j][k], dp[i][j][k]);
       
                                    // ambil vote
                                    dp[i + 1][j + 1][k] = min(dp[i + 1][j + 1][k], dp[i][j][k] + (double) v[i].second / (x + 1));
                                    
                                    // ambil collaborator
                                    if(v[i].first != 1e9) 
                                          dp[i + 1][j + 1][k + 1] = min(dp[i + 1][j + 1][k + 1], dp[i][j][k] + (double) v[i].first / (k + 1));
                              }
                        }
                  }
       
                  ans = min(ans, dp[N][K][x]);
            }
       
            cout << fixed << setprecision(15) << ans << '\n';
      }
      
 
}   
#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...