180852025-09-29 10:43:42ubormaciKövek (100 pont)cpp17Hibás válasz 32/10013ms1076 KiB
#include <iostream>
#include <algorithm> // for sort, mainly
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <array>
#include <string>
#include <cstdio>
#include <iterator>
#include <unordered_set>
#include <cstdint> // for int64_t, int32_t, etc
#include <queue>
#include <stack>
#include <deque>
#include <numeric> // gcd, lcm
#include <fstream>
#include <bitset> // for bitset
#include <iomanip>
#include <cassert> // for set with custom ordering
#include <type_traits> // for set with custom ordering
#include <utility> // for swap, forward, etc
using namespace std;

#pragma GCC optimize("O2")
// #pragma GCC optimize("O1","O2","O3","Ofast","unroll-loops")
//#pragma GCC target("sse","sse2","sse3","sse4.1","sse4.2","avx","avx2","fma")

template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cout << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << H; dbg_out(T...); }
#ifdef LOCAL
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

/*
 *
 * notes:
 *
 * int64_t
 * stoi(string s) -> string to int
 * to_string() -> int (or else) to string
 *
 * vector declaration:
 * vector<ll> v(n, 0)
 * vector<vector<ll>> v(n, vector<ll>(n, 0));
 *
 * {if statement} ? {truth value} : {false value}
 *
 * set lower bound/upper bound:
 * 	// . . . m1 . . . d . . . . m2
 *    auto m1_it = b.lower_bound(d);
 *    advance(m1_it, -1);
 *    m1 = *m1_it;
 *	m2 = *b.upper_bound(d);
 *
 * #ifdef LOCAL
 *    freopen("in.txt","r",stdin);
 *    freopen("out.txt","w",stdout);
 * #endif
 *
 * constexpr auto lcm(auto x, auto... xs)
 * {
 *	return ((x = std::lcm(x, xs)), ...);
 * }
 *
 * std::gcd(int a, int b)
 *
 * cout << setprecision(n);
 *
 * set with custom ordering
 * set<ll, decltype(&cmp)> qu(cmp);
 *
 */

typedef int64_t ll;

void solve() {

    ll n;
    cin >> n;
    vector<ll> v(n, 0);
    ll sm = 0;
    for(ll i = 0 ; i < n; i++) {
        cin >> v[i];
        sm += v[i];
    }

    ll p = sm / n;

    //cerr << "\np=" << p;

    queue<pair<ll,ll>> q; // {how many, where}
    ll drag = 0;

    ll moves = 0;

    for(ll i = 0; i < n; i++) {

        // ket eset:
        // a mostani nagyobb mint p, kisebb mint p
        // vagy pont p, de az nem erdekes

        if(v[i] == p) {
            continue;
        }else if(v[i] > p) {

            // tehat, ha van korabban olyan, akinek szuksege van, akkor neki kuldunk amennyit tudunk
            ll extra = v[i] - p;
            v[i] = p;
            while(extra > 0 && q.empty() == false) {

                auto h = q.front();
                q.pop();

                if(h.first <= extra) {
                    extra -= h.first;
                    moves += i - h.second;
                }else{
                    h.first -= extra;
                    moves += i - h.second;
                    extra = 0;
                    q.push({h.first, h.second});
                }

            }

            if(extra > 0) {
                drag += extra;
            }


        }else if(v[i] < p) {

            // nekunk szugsegunk van
            ll needed = p - v[i];
            v[i] = p;
            if(drag >= needed) {
                drag -= needed;
            }else{
                needed -= drag;
                drag = 0;
                q.push({needed, i});
            }

        }

        if(drag > 0) {
            moves++;
        }

    }


    cout << moves;


}

int main()
{
    std::ios_base::sync_with_stdio(false);
    //cin.tie(nullptr);
    //cout.tie(nullptr);

    solve();

    return 0;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base32/100
1Elfogadva0/01ms316 KiB
2Hibás válasz0/013ms1076 KiB
3Elfogadva5/51ms320 KiB
4Elfogadva5/51ms316 KiB
5Hibás válasz0/51ms316 KiB
6Elfogadva5/51ms500 KiB
7Elfogadva5/51ms316 KiB
8Elfogadva5/51ms316 KiB
9Elfogadva7/71ms316 KiB
10Hibás válasz0/71ms316 KiB
11Hibás válasz0/81ms316 KiB
12Hibás válasz0/81ms500 KiB
13Hibás válasz0/81ms316 KiB
14Hibás válasz0/82ms316 KiB
15Hibás válasz0/84ms564 KiB
16Hibás válasz0/810ms1076 KiB
17Hibás válasz0/813ms1076 KiB