Submission #171308

# Submission time Handle Problem Language Result Execution time Memory
171308 2019-12-28T10:02:20 Z emil_physmath Gift (IZhO18_nicegift) C++17
100 / 100
1389 ms 102676 KB
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <map>
#include <set>
using namespace std;
typedef double ldouble;
typedef long long llong;
const int maxN = 100001;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n, k;
    cin >> n >> k;
    vector<llong> arr(n);
    priority_queue<pair<llong, int>> a;
    llong mx = -1, sum = 0;
    for (int i = 0; i < n; ++i)
    {
        cin >> arr[i];
        sum += arr[i];
        mx = max(mx, arr[i]);
        a.push({arr[i], i});
    }
    if (sum % k || mx > sum / k)
    {
        cout << -1 << endl;
        exit(0);
    }
    vector<pair<llong, vector<int>>> ans;
    while (a.top().first)
    {
        vector<int> cur;
        llong mn = a.top().first;
        while (cur.size() < k)
        {
            cur.push_back(a.top().second);
            mn = min(mn, a.top().first);
            a.pop();
        }
        if (!a.empty())
            mn = min(mn, sum / k - a.top().first);
        if (!mn) throw;
        // it->first <= (sum - k * mn) / k
        // k * it->first <= sum - k * mn
        // mn * k <= sum - k * it->first
        // mn <= sum / k - it->first
        /*
            mn = min(mn, a.rbegin()->first - it->first + 1);
        */
        /*
        if (!mn)
        {
            cout << -1 << endl;
            exit(0);
        }
        */
        for (int x: cur)
        {
            // a.erase({arr[x], x});
            sum -= arr[x];
            a.push({arr[x] -= mn, x});
            sum += arr[x];
        }
        ans.push_back({mn, cur});
        /*cerr << "a` ";
        for (pair<llong, int> i: a)
            cerr << i.first << ' ';
        cerr << endl;*/
    }
    cout << ans.size() << '\n';
    for (auto x: ans)
    {
        cout << x.first << ' ';
        for (int y: x.second)
            cout << y + 1 << ' ';
        cout << '\n';
    }
}
// 6 5 4 3 x
// x x-1 x-2 x-3 x

Compilation message

nicegift.cpp: In function 'int main()':
nicegift.cpp:40:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         while (cur.size() < k)
                ~~~~~~~~~~~^~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB n=4
2 Correct 2 ms 376 KB n=3
3 Correct 2 ms 376 KB n=3
4 Correct 4 ms 376 KB n=4
5 Correct 2 ms 376 KB n=4
6 Correct 2 ms 256 KB n=2
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB n=4
2 Correct 2 ms 376 KB n=3
3 Correct 2 ms 376 KB n=3
4 Correct 4 ms 376 KB n=4
5 Correct 2 ms 376 KB n=4
6 Correct 2 ms 256 KB n=2
7 Correct 2 ms 376 KB n=5
8 Correct 2 ms 380 KB n=8
9 Correct 8 ms 376 KB n=14
10 Correct 2 ms 376 KB n=11
11 Correct 31 ms 3952 KB n=50000
12 Correct 32 ms 3696 KB n=50000
13 Correct 2 ms 376 KB n=10
14 Correct 2 ms 376 KB n=685
15 Correct 3 ms 376 KB n=623
16 Correct 3 ms 380 KB n=973
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB n=4
2 Correct 2 ms 376 KB n=3
3 Correct 2 ms 376 KB n=3
4 Correct 4 ms 376 KB n=4
5 Correct 2 ms 376 KB n=4
6 Correct 2 ms 256 KB n=2
7 Correct 2 ms 376 KB n=5
8 Correct 2 ms 380 KB n=8
9 Correct 8 ms 376 KB n=14
10 Correct 2 ms 376 KB n=11
11 Correct 31 ms 3952 KB n=50000
12 Correct 32 ms 3696 KB n=50000
13 Correct 2 ms 376 KB n=10
14 Correct 2 ms 376 KB n=685
15 Correct 3 ms 376 KB n=623
16 Correct 3 ms 380 KB n=973
17 Correct 3 ms 404 KB n=989
18 Correct 4 ms 376 KB n=563
19 Correct 3 ms 376 KB n=592
20 Correct 3 ms 380 KB n=938
21 Correct 0 ms 248 KB n=747
22 Correct 2 ms 376 KB n=991
# Verdict Execution time Memory Grader output
1 Correct 696 ms 70392 KB n=1000000
2 Correct 452 ms 48324 KB n=666666
3 Correct 253 ms 25816 KB n=400000
4 Correct 174 ms 17372 KB n=285714
5 Correct 12 ms 1400 KB n=20000
6 Correct 106 ms 10656 KB n=181818
7 Correct 7 ms 1016 KB n=10000
8 Correct 6 ms 760 KB n=6666
9 Correct 4 ms 632 KB n=4000
10 Correct 18 ms 632 KB n=2857
11 Correct 3 ms 376 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB n=4
2 Correct 2 ms 376 KB n=3
3 Correct 2 ms 376 KB n=3
4 Correct 4 ms 376 KB n=4
5 Correct 2 ms 376 KB n=4
6 Correct 2 ms 256 KB n=2
7 Correct 2 ms 376 KB n=5
8 Correct 2 ms 380 KB n=8
9 Correct 8 ms 376 KB n=14
10 Correct 2 ms 376 KB n=11
11 Correct 31 ms 3952 KB n=50000
12 Correct 32 ms 3696 KB n=50000
13 Correct 2 ms 376 KB n=10
14 Correct 2 ms 376 KB n=685
15 Correct 3 ms 376 KB n=623
16 Correct 3 ms 380 KB n=973
17 Correct 3 ms 404 KB n=989
18 Correct 4 ms 376 KB n=563
19 Correct 3 ms 376 KB n=592
20 Correct 3 ms 380 KB n=938
21 Correct 0 ms 248 KB n=747
22 Correct 2 ms 376 KB n=991
23 Correct 696 ms 70392 KB n=1000000
24 Correct 452 ms 48324 KB n=666666
25 Correct 253 ms 25816 KB n=400000
26 Correct 174 ms 17372 KB n=285714
27 Correct 12 ms 1400 KB n=20000
28 Correct 106 ms 10656 KB n=181818
29 Correct 7 ms 1016 KB n=10000
30 Correct 6 ms 760 KB n=6666
31 Correct 4 ms 632 KB n=4000
32 Correct 18 ms 632 KB n=2857
33 Correct 3 ms 376 KB n=2000
34 Correct 26 ms 2548 KB n=23514
35 Correct 27 ms 2552 KB n=23514
36 Correct 3 ms 504 KB n=940
37 Correct 2 ms 376 KB n=2
38 Correct 59 ms 5484 KB n=100000
39 Correct 61 ms 5484 KB n=100000
40 Correct 2 ms 376 KB n=10
41 Correct 4 ms 376 KB n=100
42 Correct 4 ms 504 KB n=1000
43 Correct 818 ms 92680 KB n=1000000
44 Correct 1389 ms 102676 KB n=1000000
45 Correct 1000 ms 64712 KB n=666666
46 Correct 574 ms 34320 KB n=400000
47 Correct 15 ms 920 KB n=2336
48 Correct 798 ms 48228 KB n=285714
49 Correct 619 ms 38212 KB n=181818
50 Correct 38 ms 2800 KB n=40000
51 Correct 22 ms 1528 KB n=20000
52 Correct 12 ms 892 KB n=10000
53 Correct 55 ms 3320 KB n=6666
54 Correct 8 ms 632 KB n=4000
55 Correct 229 ms 11520 KB n=2857
56 Correct 5 ms 632 KB n=2000