제출 #1183720

#제출 시각아이디문제언어결과실행 시간메모리
1183720jerzykLet's Win the Election (JOI22_ho_t3)C++20
32 / 100
164 ms476 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
using namespace __gnu_pbds;
using namespace std;
#define pb push_back
#define st first
#define nd second
typedef long long ll;
typedef long double ld;
const ll I = 1000'000'000'000'000'000LL;
const int II = 2'000'000'000;
const ll M = 1000'000'007LL;
const int N = 503;
pair<int, int> tab[N], t2[N];
int czy[N];
set<pair<int, int>> lft;

void Solve()
{
    cout << fixed << setprecision(7);
    int n, k;
    ld ans = II;
    cin >> n >> k;
    for(int i = 1; i <= n; ++i)
    {
        cin >> tab[i].nd >> tab[i].st;
        if(tab[i].st == -1) tab[i].st = II;
    }
    sort(tab + 1, tab + 1 + n);
    for(int i = 1; i <= n; ++i) t2[i] = make_pair(tab[i].nd, i);
    sort(t2 + 1, t2 + 1 + n);
    
    for(int i = 0; i < k; ++i)
    {
        if(tab[i].st == II) break;
        for(int j = 1; j <= n; ++j) czy[j] = 0;
        lft.clear();

        vector<int> cur;
        ld s1 = 0, s2 = 0;
        for(int j = 1; j <= i; ++j)
        {
            s1 += (ld)tab[j].st / (ld)j;
            cur.pb(j);
        }
        
        for(int j = 1; j <= n && (int)lft.size() < k - i; ++j)
        {
            if(t2[j].nd <= i) continue;
            czy[t2[j].nd] = 1;
            s2 += (ld)t2[j].st / (ld)(i + 1);
            lft.insert(t2[j]);
        }

        if(i == 0)
        {ans = s1 + s2; continue;}
        ans = min(ans, s1 + s2);
        ld s = s1 + s2;

        //cout << "A: " << i << " " << s << "\n";

        for(int j = i + 1; j <= n; ++j)
        {
            if(tab[j].st == II) break;

            ld akt = (ld)tab[j].st / (ld)i, bst = I;
            if(czy[j])
                akt -= (ld)tab[j].nd / (ld)(i + 1);
            else
                akt -= (ld)(*lft.rbegin()).st / (ld)(i + 1);
            int wh = 0;
            for(int l = i - 1; l >= 0; --l)
            {
                ld a = akt - (ld)tab[cur[l]].st / (ld)(l + 1);
                a += (ld)tab[cur[l]].nd / (ld)(i + 1);
                if(a < bst)
                {
                    bst = a;
                    wh = l;
                }
                if(l > 0)
                    akt += ((ld)tab[cur[l]].st / (ld)l) - ((ld)tab[cur[l]].st / (ld)(l + 1));
            }
            //cout << j << " " << czy[j] << " " << tab[j].nd << " : " << bst << " " << wh << "\n";
            if(bst > 0) continue;
            s += bst;

            if(czy[j])
                lft.erase(make_pair(tab[j].nd, j));
            else
                lft.erase((*lft.rbegin()));
            lft.insert(make_pair(tab[cur[wh]].nd, cur[wh]));
            swap(cur[wh], cur.back()); cur.pop_back();
            sort(cur.begin(), cur.end()); cur.pb(j);
            ans = min(ans, s);
        }

    }
    cout << fixed << setprecision(7) << ans << "\n";
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    //int t; cin >> t;
    //while(t--)
        Solve();

    return 0;
}
#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...