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 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";
- }
- 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;
- }
- bool sh = 0;
- /*
- 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
- */
- const ll inf = 2e9;
- int main() {
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- int n; cin >> n;
- vector <prt> a(n);
- for (int i = 0; i < n; ++i) {
- cin >> 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 << "a\n";
- for (auto y : a) {
- y.see();
- }
- cout << "pf\n";
- cvl(pf);
- }
- if (n == 1) {
- cout << a[0].b;
- exit(0);
- }
- /*проверка для a[i].r == a[n - 1].r: учтено что на последнем месте самая малая взятка
- по сравнениею с теми кто прежде стоит и за кого столько же голосует*/
- 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 -->???
- if (a[n - 1].r == a[n - 2].r) {
- last.spent++;
- }
- win_vote[a[n - 1].id] = a[n - 1].r;
- ans.push_back(last);
- }
- for (int i = 0; i < n; ++i) {
- if (a[i].b == -1) {
- 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;
- 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;
- } else {
- R = M;
- }
- }
- res now = {spent, a[i].id, L, rk};
- 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];
- vector <ll> vote(n);
- for (int i = 0; i < n; ++i) {
- if (i < 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;
- }
- }
- vote[get.id] = win_vote[get.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);
- }
- /*
- 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