Submission #495304

# Submission time Handle Problem Language Result Execution time Memory
495304 2021-12-18T10:22:47 Z xynex Gift (IZhO18_nicegift) C++17
100 / 100
1939 ms 120080 KB
/*
* Author: xynex
* Created: 18.12.2021 16:00
* Why am I so stupid? :c
* Slishkom slab
*/

#include <bits/stdc++.h>

// #pragma GCC optimize("inline")
// #pragma GCC optimize("-fgcse,-fgcse-lm")
// #pragma GCC optimize("-ftree-pre,-ftree-vrp")
// #pragma GCC optimize("-ffast-math")
// #pragma GCC optimize("-fipa-sra")
// #pragma GCC optimize("-fpeephole2")
// #pragma GCC optimize("-fsched-spec")
// #pragma GCC optimize("Ofast,no-stack-protector")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
// #pragma GCC optimize("unroll-loops")

using namespace std;

#define ll long long
#define dl double long
#define ull unsigned long long
#define pr pair
#define vt vector
#define ff first
#define ss second
#define mp make_pair
#define sz(a) (int)a.size()
#define pb push_back
#define pf push_front
#define popB pop_back
#define popF pop_front
#define bit_count __builtin_popcount
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sp(x) fixed << setprecision(x)

template<typename T> T get_rand(T l, T r) {
    random_device rd;
    mt19937 gen(rd());
    return uniform_int_distribution<T>(l, r)(gen);
}

template<typename T> T lcm(T a, T b) { return a * (b / __gcd(a, b)); }

template<class A> void read(vt<A>& v);
template<class A, size_t S> void read(array<A, S>& a);
template<class T> void read(T& x) { cin >> x; }
void read(double& d) { string t; read(t); d = stod(t); }
void read(long double& d) { string t; read(t); d = stold(t); }
template<class H, class... T> void read(H& h, T&... t) { read(h); read(t...); }
template<class A> void read(vt<A>& x) { for (auto& a : x) read(a); }
template<class A, size_t S> void read(array<A, S>& x) { for (auto& a : x) read(a); }

string to_string(char c) { return string(1, c); }
string to_string(bool b) { return b ? "true" : "false"; }
string to_string(const char* s) { return string(s); }
string to_string(string s) { return s; }
string to_string(vt<bool> v) { string res; for (int i = 0; i < sz(v); ++i) res += char('0' + v[i]); return res; }
template<size_t S> string to_string(bitset<S> b) { string res; for (int i = 0; i < S; ++i) res += char('0' + b[i]); return res; }
template<class T> string to_string(T v) { bool f = 1; string res; for (auto x : v) { if (!f) res += ' '; f = 0; res += to_string(x); } return res; }

template<class A> void write(A x) { cout << to_string(x); }
template<class H, class... T> void write(const H& h, const T&... t) { write(h); write(t...); }

void print() { write("\n"); }
template<class H, class... T> void print(const H& h, const T&... t) { write(h); if (sizeof...(t)) write(' '); print(t...); }

void freop(string s) {
    freopen((s + ".in").c_str(), "r", stdin);
    freopen((s + ".out").c_str(), "w", stdout);
}

const int MOD = 1e9 + 7;
const int N = 3e6 + 5;
const ll INF = 9e18;
const int M = 3e3 + 5;
const dl pi = acos(-1);
const dl eps = 1e-12;
const int sq = 700;

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
/* ll vs int*/

void precalc() {

}

set<pr<ll, ll>> ord;
ll a[N];
void solve() {
    int n, m; read(n, m);
    ll sum = 0;
    for(int i = 1; i <= n; ++i) {
        read(a[i]);
        ord.insert({a[i], i});
        sum += a[i];
    }
    if(sum % m) {
        print(-1);
        return;
    }
    sum /= m;
    if((*ord.rbegin()).ff > sum) {
        print(-1);
        return;
    }
    vt<ll> ans;
    while(sz(ord) >= m) {
        vt<ll> cur;
        for(int i = 0; i < m; ++i) {
            cur.pb((*ord.rbegin()).ss);
            auto en = ord.end();
            en--;
            ord.erase(en);
        }
        ll mx = 0;
        if(!ord.empty()) mx = (*ord.rbegin()).ff;
        ll ind = min(sum - mx, a[cur.back()]);
        ans.pb(ind);
        sum -= ind;
        for(int i = 0; i < m; ++i) {
            a[cur.back()] -= ind;
            if(a[cur.back()]) ord.insert({a[cur.back()], cur.back()});
            ans.pb(cur.back());
            cur.pop_back();
        }
    }
    m++;
    print(sz(ans) / m);
    for(int i = 0; i < sz(ans); ++i) {
        write(ans[i], ' ');
        if(i % m == m - 1) print("");
    }
}

int main() {
    //freop("");
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    //read(t);
    precalc();
    for (int i = 1; i <= t; ++i) {
        //write("Case #" + to_string(i) + ": ");
        solve();
    }
    return 0;
}

Compilation message

nicegift.cpp: In function 'void freop(std::string)':
nicegift.cpp:73:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   73 |     freopen((s + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
nicegift.cpp:74:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   74 |     freopen((s + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 328 KB n=4
2 Correct 0 ms 204 KB n=3
3 Correct 1 ms 204 KB n=3
4 Correct 1 ms 204 KB n=4
5 Correct 0 ms 328 KB n=4
6 Correct 0 ms 204 KB n=2
# Verdict Execution time Memory Grader output
1 Correct 1 ms 328 KB n=4
2 Correct 0 ms 204 KB n=3
3 Correct 1 ms 204 KB n=3
4 Correct 1 ms 204 KB n=4
5 Correct 0 ms 328 KB n=4
6 Correct 0 ms 204 KB n=2
7 Correct 1 ms 320 KB n=5
8 Correct 1 ms 204 KB n=8
9 Correct 1 ms 332 KB n=14
10 Correct 1 ms 204 KB n=11
11 Correct 26 ms 5160 KB n=50000
12 Correct 23 ms 4892 KB n=50000
13 Correct 0 ms 204 KB n=10
14 Correct 1 ms 332 KB n=685
15 Correct 1 ms 332 KB n=623
16 Correct 1 ms 332 KB n=973
# Verdict Execution time Memory Grader output
1 Correct 1 ms 328 KB n=4
2 Correct 0 ms 204 KB n=3
3 Correct 1 ms 204 KB n=3
4 Correct 1 ms 204 KB n=4
5 Correct 0 ms 328 KB n=4
6 Correct 0 ms 204 KB n=2
7 Correct 1 ms 320 KB n=5
8 Correct 1 ms 204 KB n=8
9 Correct 1 ms 332 KB n=14
10 Correct 1 ms 204 KB n=11
11 Correct 26 ms 5160 KB n=50000
12 Correct 23 ms 4892 KB n=50000
13 Correct 0 ms 204 KB n=10
14 Correct 1 ms 332 KB n=685
15 Correct 1 ms 332 KB n=623
16 Correct 1 ms 332 KB n=973
17 Correct 1 ms 324 KB n=989
18 Correct 1 ms 332 KB n=563
19 Correct 1 ms 324 KB n=592
20 Correct 1 ms 332 KB n=938
21 Correct 1 ms 324 KB n=747
22 Correct 1 ms 320 KB n=991
# Verdict Execution time Memory Grader output
1 Correct 598 ms 85196 KB n=1000000
2 Correct 410 ms 64328 KB n=666666
3 Correct 216 ms 37828 KB n=400000
4 Correct 150 ms 26788 KB n=285714
5 Correct 9 ms 2128 KB n=20000
6 Correct 94 ms 16964 KB n=181818
7 Correct 4 ms 1232 KB n=10000
8 Correct 5 ms 848 KB n=6666
9 Correct 4 ms 576 KB n=4000
10 Correct 5 ms 848 KB n=2857
11 Correct 1 ms 464 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 1 ms 328 KB n=4
2 Correct 0 ms 204 KB n=3
3 Correct 1 ms 204 KB n=3
4 Correct 1 ms 204 KB n=4
5 Correct 0 ms 328 KB n=4
6 Correct 0 ms 204 KB n=2
7 Correct 1 ms 320 KB n=5
8 Correct 1 ms 204 KB n=8
9 Correct 1 ms 332 KB n=14
10 Correct 1 ms 204 KB n=11
11 Correct 26 ms 5160 KB n=50000
12 Correct 23 ms 4892 KB n=50000
13 Correct 0 ms 204 KB n=10
14 Correct 1 ms 332 KB n=685
15 Correct 1 ms 332 KB n=623
16 Correct 1 ms 332 KB n=973
17 Correct 1 ms 324 KB n=989
18 Correct 1 ms 332 KB n=563
19 Correct 1 ms 324 KB n=592
20 Correct 1 ms 332 KB n=938
21 Correct 1 ms 324 KB n=747
22 Correct 1 ms 320 KB n=991
23 Correct 598 ms 85196 KB n=1000000
24 Correct 410 ms 64328 KB n=666666
25 Correct 216 ms 37828 KB n=400000
26 Correct 150 ms 26788 KB n=285714
27 Correct 9 ms 2128 KB n=20000
28 Correct 94 ms 16964 KB n=181818
29 Correct 4 ms 1232 KB n=10000
30 Correct 5 ms 848 KB n=6666
31 Correct 4 ms 576 KB n=4000
32 Correct 5 ms 848 KB n=2857
33 Correct 1 ms 464 KB n=2000
34 Correct 19 ms 2880 KB n=23514
35 Correct 18 ms 2892 KB n=23514
36 Correct 1 ms 336 KB n=940
37 Correct 0 ms 208 KB n=2
38 Correct 69 ms 9504 KB n=100000
39 Correct 56 ms 9432 KB n=100000
40 Correct 1 ms 208 KB n=10
41 Correct 1 ms 336 KB n=100
42 Correct 3 ms 592 KB n=1000
43 Correct 817 ms 110676 KB n=1000000
44 Correct 1939 ms 120080 KB n=1000000
45 Correct 1292 ms 79732 KB n=666666
46 Correct 673 ms 47172 KB n=400000
47 Correct 12 ms 1220 KB n=2336
48 Correct 811 ms 57304 KB n=285714
49 Correct 605 ms 48552 KB n=181818
50 Correct 36 ms 4944 KB n=40000
51 Correct 19 ms 2628 KB n=20000
52 Correct 10 ms 1612 KB n=10000
53 Correct 64 ms 5104 KB n=6666
54 Correct 5 ms 968 KB n=4000
55 Correct 281 ms 17208 KB n=2857
56 Correct 4 ms 704 KB n=2000