Submission #89507

#TimeUsernameProblemLanguageResultExecution timeMemory
89507HideoGift (IZhO18_nicegift)C++14
100 / 100
890 ms144604 KiB
#include <bits/stdc++.h> using namespace std; #define int long long const int N = 1e6 + 7; const int INF = 1e9 + 7; int a[N], p[N]; int n, k, s; vector < pair < int, int > > dq[N]; vector < int > ans[N]; main(){ cin >> n >> k; int mx = -1; for (int i = 0; i < n; i++){ scanf("%lld", &a[i]); s += a[i]; mx = max(mx, a[i]); } int h = s / k, cs = 0, c = 0; if (mx > h || s % k != 0){ cout << -1; return 0; } for (int i = 0; i < n; i++){ if(a[i] == 0) continue; if (cs + a[i] >= h){ a[i] -= (h - cs); dq[c].push_back(make_pair(h - cs, i)); cs = 0; i--; c++; continue; } cs += a[i]; dq[c].push_back(make_pair(a[i], i)); } int g = 0; while (s != 0){ int mn = -1; for (int i = 0; i < k; i++){ if (mn == -1){ mn = dq[i][p[i]].first; continue; } mn = min(mn, dq[i][p[i]].first); } ans[g].push_back(mn); for (int i = 0; i < k; i++){ dq[i][p[i]].first -= mn; ans[g].push_back(dq[i][p[i]].second + 1); if(dq[i][p[i]].first == 0) p[i]++; } g++; s -= (mn * k); } cout << g << endl; int it = 0; while (it != g){ for (int i = 0; i < ans[it].size(); i++){ printf("%lld ", ans[it][i]); } printf("\n"); it++; } }

Compilation message (stderr)

nicegift.cpp:14:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main(){
      ^
nicegift.cpp: In function 'int main()':
nicegift.cpp:61:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int i = 0; i < ans[it].size(); i++){
                         ~~^~~~~~~~~~~~~~~~
nicegift.cpp:18:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%lld", &a[i]);
         ~~~~~^~~~~~~~~~~~~~~
#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...