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;
- }
- struct res{
- ll spent, id, L, rk;//M: M - 1 -- макс кроме нас
- void see() {
- cout << "res\n";
- cout << "spent id M rk\n";
- cout << spent << ' '<< id << ' ' << L << ' ' << rk << "\n\n";
- }
- };
- bool cmp1(res a, res b) {
- return a.spent < b.spent;
- }
- bool sh = 1;
- /*
- 6
- 1 1
- 1 2
- 3 3
- 3 4
- 5 7
- 5 8
- 8
- 10 10
- 10 20
- 30 30
- 30 40
- 50 70
- 50 80
- 100 200
- 100 300
- */
- 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);
- 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);
- }
- for (int i = 0; i < n; ++i) {
- if (a[i].b == -1) {
- continue;
- }
- ll L = a[i].r, R = a[n - 1].r, M, ad;
- 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;
- } else {
- R = M;
- }
- }
- //НЕТ!!! СРАЗУ ТУТ НАДО ДЕЛАТЬ ПРОВЕРКУ НА ОПТИМАЛЬНОСТЬ
- ll spent = L - a[i].r + a[i].b; //тк нам доостаточно уровня M!!!
- //а зная итоговое rk res и M, мы смодем восстановить, что каким-то партиям мы вернем срезанные голоса
- res now = {spent, a[i].id, L, rk};
- ans.push_back(now);
- if (sh) {
- 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();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement