Submission #914915

#TimeUsernameProblemLanguageResultExecution timeMemory
914915andrei_iorgulescuLet's Win the Election (JOI22_ho_t3)C++14
100 / 100
53 ms4440 KiB
#include <bits/stdc++.h>

using namespace std;
using ld = long double;

#define int long long

int n,k;
ld m;
pair<ld,ld> a[505];
ld dp[505][505];

ld solve()
{
    ///daca sortez dupa b, mi-e foarte ez
    ///dp[i][j] = costul minim sa ajung la omul i sa fi luat j a-uri
    ///c = min(m + 1,i - j + 1)
    ///dp[i][j] -> dp[i + 1][j] de cost b[i + 1] / c ofc daca n-am luat prea multe b-uri
    ///dp[i][j] -> dp[i + 1][j + 1] de cost a[i + 1] / m
    for (int i = 0; i <= n; i++)
    {
        for (int j = 0; j <= n; j++)
            dp[i][j] = 1e9;
    }
    dp[0][0] = 0;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 0; j <= i; j++)
        {
            ld c = i - j;
            //if (i - j <= m)
            if (i - j <= m and i - j != 0)
                dp[i][j] = min(dp[i][j],dp[i - 1][j] + a[i].first / c);
            else if (i - j > m)
                dp[i][j] = min(dp[i][j],dp[i - 1][j]);
            if (j >= 1)
                dp[i][j] = min(dp[i][j],dp[i - 1][j - 1] + a[i].second / (m + 1));
            //cout << i << ' ' << j << ' ' << dp[i][j] << endl;
        }
    }
    //cout << dp[n][(int)(k - m)] << endl;
    return dp[n][(int)(k - m)];
}

ld slv(ld x)
{
    m = x;
    return solve();
}

ld solve_normal()
{
    ///fuckkkk n-a mers greedy-ul :facepalm:
    sort(a + 1,a + n + 1);
    ld ans = 1e6;
    //for (m = 0; m < k; m++)
      //  ans = min(ans,solve());
    int st = 0,pas = 1 << 8;
    while (pas != 0)
    {
        if (st + pas < k and slv(st + pas - 1) > slv(st + pas))
            st += pas;
        pas /= 2;
    }
    ans = slv(st);
    return ans;
}

/*ld solve_brut()
{
    ld ans = 1e6;
    vector<int>ids;
    for (int i = 1; i <= n; i++)
        ids.push_back(i);
    do
    {
        for (ld m = 1; m <= k; m++)
        {
            ld cur = 0;
            for (ld j = 1; j < m; j++)
                cur += b[ids[j - 1]] / j;
            for (ld j = m; j <= k; j++)
                cur += a[ids[j - 1]] / m;
            ans = min(ans,cur);
        }
    }while(next_permutation(ids.begin(),ids.end()));
    return ans;
}*/

bool generez_teste = false;

void citire()
{
    if (generez_teste == false)
    {
        cin >> n >> k;
        for (int i = 1; i <= n; i++)
            cin >> a[i].second >> a[i].first;
        for (int i = 1; i <= n; i++)
            if (a[i].first == -1)
                a[i].first = 1e9;
    }
    /*else
    {
        n = 3;
        k = 2;
        for (int i = 1; i <= n; i++)
        {
            a[i] = 1 + rand() % 9;
            b[i] = a[i] + rand() % 10;
            if (b[i] - a[i] == 9)
                b[i] = -1;
            if (b[i] == -1)
                b[i] = 1e9;
        }
    }*/
}

signed main()
{
    cout << setprecision(5) << fixed;
    citire();
    cout << solve_normal();
    return 0;
    /*srand(0);
    ld ans_normal,ans_brut;
    while (true)
    {
        citire();
        ans_normal = solve_normal();
        ans_brut = solve_brut();
        if (abs(ans_normal - ans_brut) >= 0.00001d)
        {
            for (int i = 1; i <= n; i++)
                cout << a[i] << ' ' << b[i] << endl;
            break;
        }
    }
    cout << setprecision(5) << fixed;
    //cout << ans_normal << ' ' << ans_brut;
    return 0;*/
}

/**
3
3
9 9
3 6
7 14
**/
#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...