Submission #1211789

#TimeUsernameProblemLanguageResultExecution timeMemory
1211789sid03Gift (IZhO18_nicegift)C++20
100 / 100
323 ms72904 KiB
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    int n,k; cin >> n >> k;
    vector<pair<long long, int>> a(n);
    long long s = 0;
    for (int i = 0; i < n; i++) {
        cin >> a[i].first;
        a[i].second = i+1;
        s += a[i].first;
    }
    sort(a.begin(),a.end(),greater<pair<long long, int>>());
    if (s % k != 0 || s/k < a[0].first) {
        cout << -1;
        return 0;
    }
    
    s /= k;
    long long d = 0, e = 0, x;
    vector<long long> t(n), l(n);
    int i = n-1, j = 0;
    queue<pair<pair<int,int>, long long>> q;
    while (i >= k) {
        d -= t[i]; t[i] = 0;
        e += l[j]; l[j] = 0;
        x = min(a[i].first - d, s - (a[j].first-e));
        if (x) q.push({{j,i},x});
        t[i-k+j] += x;
        l[i-k+j+1] += x; 
        s -= x;
        if (x == a[i].first - d) i--;
        else j++;
        d += x;
    }
    q.push({{0,k-1},s});
    
    cout << q.size() << '\n';
    while (!q.empty()) {
        cout << q.front().second << ' ';
        for (i = 0; i < q.front().first.first; i++) cout << a[i].second << ' ';
        for (i = q.front().first.second; i > q.front().first.second - k + q.front().first.first; i--) cout << a[i].second << ' ';
        cout << '\n';
        q.pop();
    }
    
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...