# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
883916 | macneil | Let's Win the Election (JOI22_ho_t3) | C++17 | 242 ms | 604 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//cf meowcneil; t.me/macneilbot
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
#include <bitset>
#include <iterator>
#include <iomanip>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <random>
#include <cassert>
using namespace std;
#define int long long
#define pb push_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define f(a) for(int i = 0; i<a; ++i)
typedef long long ll;
typedef unsigned long long ull;
typedef double ld;
typedef string str;
typedef vector<str> vestr;
typedef vector<int> vei;
typedef vector<vector<int>> veve;
mt19937 rnd(1329);
const int NUM = 2e4;
int k;
bool flip(int a, int b) {
return ((rnd() % (a + b))) < a;
}
ld cost(vector<int> &a, vector<int> &b, vector<int> &inb, vector<int> &ord1) {
int n = a.size();
ld ans = 0;
vector<int> auu;
for (int i = 0; i < n; ++i) {
if (inb[i]) {
auu.push_back(i);
}
}
sort(all(auu), [&](int i, int j){ return b[i] < b[j];});
for (int i = 0; i < auu.size(); ++i) {
ans += (ld)(b[auu[i]]) / (i + 1);
}
int ctt = auu.size();
for (auto i : ord1) {
if (ctt == k) break;
if (!inb[i]) {
ans += (ld)a[i] / (auu.size() + 1);
ctt++;
}
}
return ans;
}
void solve() {
int n;
cin >> n >> k;
vector<int> a(n), b(n);
for (int i = 0; i < n; ++i) cin >> a[i] >> b[i];
// for (int i = 0; i < n; ++i) if (b[i] == -1) b[i] = 1e14;
vector<int> ord1(n), ord2(n);
iota(all(ord1), 0);
iota(all(ord2), 0);
sort(all(ord1), [&](int i, int j){
return a[i] < a[j];
});
sort(all(ord2), [&](int i, int j){
if (b[i] == b[j]) return a[i] > a[j];
return b[i] < b[j];
});
vector<int> notminusone;
for (int i = 0; i < n; ++i) if (b[i] != -1) notminusone.push_back(i);
ld ans = 0;
for (int i = 0; i < k; ++i) ans += a[ord1[i]];
if (notminusone.size() == 0) {
cout << ans;
return;
}
vector<int> inb(n);
ld prevans = ans;
for (int i = 0; i < NUM; ++i) {
vector<int> tt;
for (int j = 0; j < rnd() % 10 + 1; ++j) {
tt.push_back(rnd() % (notminusone.size()));
inb[notminusone[tt[j]]] ^= 1;
}
if (cost(a, b, inb, ord1) < prevans) {
prevans = cost(a, b, inb, ord1);
ans = min(ans, prevans);
}
else {
for (auto e : tt) inb[notminusone[e]] ^= 1;
}
}
cout << ans << '\n';
}
signed main() {
int tc = 1;
cout << fixed << setprecision(15);
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
cin >> tc;
#else
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// cin >> tc;
#endif
for (int t = 1; t <= tc; t++) {
// cout << "Case #" << t << ": ";
solve();
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |