Submission #335299

#TimeUsernameProblemLanguageResultExecution timeMemory
335299limabeansGift (IZhO18_nicegift)C++17
100 / 100
1086 ms88408 KiB
#include <bits/stdc++.h> using namespace std; template<typename T> void out(T x) { cout << x << endl; exit(0); } #define watch(x) cout << (#x) << " is " << (x) << endl using ll = long long; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll n,k; cin>>n>>k; priority_queue<pair<ll,int>> pq; ll sum=0; ll hi = 0; for (int i=1; i<=n; i++) { ll x; cin>>x; sum += x; hi=max(hi,x); pq.push({x,i}); } if (sum%k) out(-1); if (hi>sum/k) out(-1);//remove at least k blocks per iteration ll av = sum/k; vector<vector<ll>> w; while (pq.size()) { vector<pair<ll,int>> tmp; for (int i=0; i<k; i++) { if (pq.empty()) out(-1); tmp.push_back(pq.top()); pq.pop(); } ll lo = (pq.empty() ? 0 : pq.top().first); ll gap = min(av-lo, tmp.back().first); av -= gap; vector<ll> inds; for (auto p: tmp) { inds.push_back(p.second); p.first -= gap; if (p.first>0) { pq.push(p); } } inds.push_back(gap); w.push_back(inds); } cout<<w.size()<<"\n"; for (auto p: w) { ll gap = p.back(); p.pop_back(); cout<<gap<<" "; for (ll i: p) cout<<i<<" "; cout<<"\n"; } 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...