Submission #212042

# Submission time Handle Problem Language Result Execution time Memory
212042 2020-03-22T06:46:40 Z NONAME Pod starim krovovima (COCI20_psk) C++17
50 / 50
5 ms 384 KB
#include <bits/stdc++.h>
#include <time.h>
#ifndef _LOCAL
#pragma GCC optimize("-O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#endif
#define sz(x) int(x.size())
#define in(x) freopen(x,"r",stdin)
#define out(x) freopen(x,"w",stdout)
#define N 100500
#define oo ll(1e16)
#define pii pair <int, int>
#define pll pair <ll, ll>
#define ft first
#define sd second
#define pb push_back
#define ppb pop_back
#define el '\n'
#define elf endl
#define base ll(1e9 + 7)
#define re return
#define nins 4294967295
using namespace std;
typedef long long ll;
typedef long double ld;

ll add(ll x, ll y, ll mod) {
    return (mod == 0) ? (x + y) : (x + y) % mod;
}

ll mul(ll x, ll y, ll mod) {
    return (mod == 0) ? (x * y) : (x * y) % ll(mod);
}

struct st {
    ll x, y;
    int ps;

    st (ll x, ll y, ll ps) : x(x), y(y), ps(ps) {}
};

bool cmp(st x, st y) {
    return x.y == y.y ? x.x < y.x : x.y < y.y;
}

vector <st> v;
int l, r, n, ans, rs[N];

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    #ifdef _LOCAL
        in("input.txt");
        clock_t start_time = clock();
    #endif

    cin >> n;
    for (int i = 0; i < n; i++) {
        ll x, y; cin >> x >> y;
        v.pb(st(x, y, i));
    }

    sort(v.begin(), v.end(), cmp);

    l = n - 2; r = n - 1;
    while (l >= 0) {
        if (l == r) l--;

        ll o = min(v[l].x, v[r].y - v[r].x);

        v[l].x -= o;
        v[r].x += o;

        if (v[l].x == 0) l--;
        if (v[r].x == v[r].y) r--;
    }

    for (int i = 0; i < n; i++)
        rs[v[i].ps] = v[i].x;

    for (int i = 0; i < n; i++)
        ans += !rs[i];

    cout << ans << el;
    for (int i = 0; i < n; i++)
        cout << rs[i] << ' ';

	#ifdef _LOCAL
        cerr.precision(4); cerr << fixed;
        cerr << el;
        cerr << "Time :: " << ld(clock() - start_time) / CLOCKS_PER_SEC << el;
    #endif
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 3 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct