Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <vector>
- #include <queue>
- #include <algorithm>
- #include <string>
- #include <stack>
- #include <set>
- #include <map>
- #define pii pair <int, int>
- #define pll pair <long long, long long>
- #define pb(x) push_back(x)
- using namespace std;
- using ll = long long;
- using ld = long double;
- using db = double;
- void cv(vector <int> &v) {
- for (auto x : v) cout << x << ' ';
- cout << "\n";
- }
- void cvl(vector <ll> &v) {
- for (auto x : v) cout << x << ' ';
- cout << "\n";
- }
- void cvv(vector <vector <int> > &v) {
- for (auto x : v) cv(x);
- cout << "\n";
- }
- void cvb(vector <bool> v) {
- for (bool x : v) cout << x << ' ';
- cout << "\n";
- }
- void cvs(vector <string> v) {
- for (auto a : v) {
- cout << a << "\n";
- }
- }
- void cvp(vector <pii> a) {
- for (auto p : a) {
- cout << p.first << ' ' << p.second << "\n";
- }
- cout << "\n";
- }
- void cvpl(vector <pll> a) {
- for (auto p : a) {
- cout << p.first << ' ' << p.second << "\n";
- }
- cout << "\n";
- }
- struct prt{
- ll r, b, id;
- void see() {
- cout << r << ' ' << b << ' ' << id << "\n\n";
- }
- };
- bool cmp(prt a, prt b) {
- return a.r < b.r || (a.r == b.r && a.b > b.b);
- }
- struct res{
- ll spent, id, L, rk;//M: M - 1
- void see() {
- cout << "res\n";
- cout << "spent id L rk\n";
- cout << spent << ' '<< id << ' ' << L << ' ' << rk << "\n\n";
- }
- };
- bool cmp1(res a, res b) {
- return a.spent < b.spent;
- }
- /*
- 6
- 1 1
- 1 2
- 3 3
- 3 4
- 5 7
- 5 8
- 8
- 10 13
- 10 20
- 30 30
- 30 40
- 50 70
- 50 80
- 100 200
- 100 300
- 8
- 10 13
- 100 20
- 300 30
- 300 40
- 500 70
- 500 80
- 510 200
- 510 300
- MISTAKE FOUND!!!
- 8
- 10 13
- 100 20
- 300 30
- 300 40
- 500 70
- 500 80
- 511 200
- 511 300
- 77
- 5
- 10 100 300 300 507 500 507 506
- */
- bool sh = 0;
- const ll inf = 2e9;
- #include <ctime>
- #include <numeric>
- vector <prt> a;
- vector <ll> vote;
- vector <pll> raw;
- bool check(int wn_id) {
- return vote[wn_id] == *max_element(vote.begin(), vote.end()) && count(vote.begin(), vote.end(), vote[wn_id]) == 1;
- }
- bool rand_mode = 0;
- bool debug = 0;
- int main() {
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- srand(time(0));
- int n;
- if (rand_mode) {
- n = rand() % 100 + 3;
- } else {
- cin >> n;
- }
- a.resize(n);
- raw.resize(n);
- for (int i = 0; i < n; ++i) {
- if (!rand_mode) {
- cin >> a[i].r >> a[i].b;
- if (a[i].b == -1) {
- a[i].b = inf;
- }
- raw[i] = {a[i].r, a[i].b};
- a[i].id = i;
- continue;
- }
- a[i].r = rand() % 100;
- a[i].b = rand() % 100;
- raw[i] = {a[i].r, a[i].b};
- a[i].id = i;
- }
- sort(a.begin(), a.end(), cmp);
- vector <ll> pf(n);
- vector <ll> win_vote(n);
- pf[0] = a[0].r;
- for (int i = 1; i < n; ++i) {
- pf[i] = pf[i - 1] + a[i].r;
- }
- vector <res> ans;
- if (sh) {
- cout << "n = " << n << "\n";
- cout << "raw data :\n";
- cvpl(raw);
- cout << "a\n";
- for (auto y : a) {
- y.see();
- }
- cout << "pf\n";
- cvl(pf);
- }
- if (n == 1) {
- cout << a[0].b << "\n" << 1 << "\n" << a[0].r;
- exit(0);
- }
- if (a[n - 1].b != -1) {
- if (sh) {
- cout << "last\n";
- }
- res last = {a[n - 1].b, a[n - 1].id, inf, inf}; //n - 1 -->???
- win_vote[a[n - 1].id] = a[n - 1].r;
- if (a[n - 1].r == a[n - 2].r) {
- last.spent++;
- win_vote[a[n - 1].id]++;
- }
- ans.push_back(last);
- }
- for (int i = 0; i < n; ++i) {
- if (a[i].b == inf) {
- if (sh) {
- cout << "skip\n";
- }
- continue;
- }
- if (a[i].r == a[n - 1].r) {
- if (sh) cout << "break\n";
- break;
- }
- ll L = 0, R = a[n - 1].r + 10, M, ad, spent;
- if (sh) {
- cout << "i = " << i << "\n";
- //cout << "L R = " << L << ' ' << R << "\n";
- }
- int lk, rk, mk, goodrk;
- while (L + 1 < R) {
- M = (L + R) / 2;
- if (sh) {
- //cout << "M = " << M << "\n";
- }
- lk = i - 1;
- rk = n - 1;
- if (sh) {
- //cout << "lk rk = " << lk << ' ' << rk << "\n";
- }
- while (lk + 1 < rk) {
- mk = (lk + rk) / 2;
- if (sh) {
- //cout << "mk = " << mk << "\n";
- }
- if (a[mk].r >= M) {
- rk = mk;
- } else {
- lk = mk;
- }
- }
- ll del = 0;
- if (rk > 0) {
- del = pf[rk - 1];
- }
- if (sh) {
- //cout << "rk del = " << rk << ' ' << del << "\n";
- }
- ad = pf[n - 1] - del - (M - 1) * (n - rk);
- if (sh) {
- //cout << "(n - rk) = " << (n - rk ) << "\n";
- //cout << "ad (cut voices) = " << ad << "\n";
- }
- if (a[i].r + ad >= M) {
- L = M;
- spent = ad + a[i].b;//- (n - rk - 1)???
- win_vote[a[i].id] = a[i].r + ad;
- goodrk = rk;
- } else {
- R = M;
- }
- }
- res now = {spent, a[i].id, L, goodrk};
- ans.push_back(now);
- if (sh) {
- cout << "L = " << L << "\n";
- cout << "spent = " << spent << "\n";
- cout << "now\n";
- now.see();
- }
- }
- sort(ans.begin(), ans.end(), cmp1);
- if (sh) {
- cout << "ans\n";
- for (auto y : ans) {
- y.see();
- }
- }
- if (sh) {
- cout << "RES ANS\n";
- ans[0].see();
- }
- res get = ans[0];
- vote.resize(n);
- /*for (int i = 0; i < n; ++i) {
- if (a[i].id < get.rk) {
- vote[a[i].id] = a[i].r;
- } else {
- if (sh) {
- cout << "a[i].id = " << a[i].id << "\n";
- cout << "get.L - 1 = " << get.L - 1 << "\n";
- }
- vote[a[i].id] = get.L - 1;
- }
- }*/
- for (int i = n - 1; i >= get.rk; --i) {
- vote[a[i].id] = get.L - 1;
- }
- for (int i = min(get.rk - 1, (long long)(n - 1)); i >= 0; --i) {
- vote[a[i].id] = a[i].r;
- }
- vote[get.id] = win_vote[get.id];
- if (a[n - 1].id == get.id && win_vote[get.id] == raw[get.id].first + 1) {
- vote[a[n - 2].id]--;
- }
- /*int i = n - 1;
- while (i > get.rk && vote[get.id] - (get.L - 1) > 1) {
- get.spent--;
- vote[a[i].id]++;
- vote[get.id]--;
- i--;
- }*/
- cout << get.spent << "\n";
- cout << get.id + 1 << "\n";
- cvl(vote);
- if (debug) {
- ll S = accumulate(vote.begin(), vote.end(), 0);
- if (S != pf[n - 1]) {
- cout << "BAD SUM\n";
- cout << "S pf[n - 1] = " << S << ' ' << pf[n - 1] << "\n";
- cout << "CHANGED VOICES\n";
- for (int i = 0; i < n; ++i) {
- if (vote[i] != raw[i].first) {
- cout << "i bef aft = " << i << ' ' << raw[i].first << ' ' << vote[i] << "\n";
- }
- }
- } else {
- cout << "OK SUM\n";
- }
- cout << "CHANGED VOICES\n";
- for (int i = 0; i < n; ++i) {
- if (vote[i] != raw[i].first) {
- cout << "i bef aft = " << i << ' ' << raw[i].first << ' ' << vote[i] << "\n";
- }
- }
- /*cout << "raw data :\n";
- cout << "n = \n" << n << "\n";
- cvpl(raw);*/
- if (sh) {
- cout << "a\n";
- for (auto y : a) {
- y.see();
- } cout << "\n";
- }
- if (check(get.id)) {
- cout << "OK\n";
- } else {
- cout << "WA\n";
- }
- }
- }
- /*
- LAST THIND TO IMPROVE IF WA:
- check vote[get.id]--
- get.spent--
- vote[a[i].id]++
- right in bin search
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement