Submission #1134610

#TimeUsernameProblemLanguageResultExecution timeMemory
1134610Halym2007Gift (IZhO18_nicegift)C++17
100 / 100
337 ms89584 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long #define sz size() #define ff first #define ss second #define pb push_back #define pii pair <ll, ll> #define dur exit(0) #define dur1 return(0) const int N = 2e6 + 5; ll n, k, a[N], gos[N]; vector <vector <ll>> v; int main () { // freopen ("input.txt", "r", stdin); ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); cin >> n >> k; ll sum = 0; for (int i = 1; i <= n; ++i) { cin >> a[i]; sum += a[i]; } if (sum % k != 0) { return cout << "-1", 0; } for (ll i = 1; i <= n; ++i) { if (sum / k < a[i]) { return cout << "-1", 0; } } queue <pii> q[k + 1]; ll san = 1, val = 0; for (ll i = 1; i <= n; ++i) { if (val + a[i] <= sum / k) { q[san].push ({a[i], i}); gos[san] += a[i]; val += a[i]; if (val == sum / k) { san++; val = 0; } } else { ll gosh = sum / k - val; q[san].push({gosh, i}); gos[san] += gosh; san++; val = a[i] - gosh; q[san].push({val, i}); gos[san] += val; } } // int idx = 1, v; // while (idx <= n) { // // } for (int i = 1; i < k; ++i) { assert (gos[i] == gos[i + 1]); } while (1) { vector <ll> kk; ll oo = 0; ll mn = 2e18; for (ll i = 1; i <= k; ++i) { if (q[i].empty()) { oo++; continue; } mn = min(q[i].front().ff, mn); } if (oo) { // assert (oo == k); break; } kk.pb (mn); for (ll i = 1; i <= k; ++i) { kk.pb (q[i].front().ss); q[i].front().ff -= mn; if (!q[i].front().ff) { q[i].pop(); } } v.pb (kk); } assert ((ll)v.sz <= n); cout << (ll)v.sz << "\n"; for (int i = 0; i < (ll)v.sz; ++i) { for (ll j : v[i]) { cout << j << " "; } cout << "\n"; } }
#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...