Submission #385603

#TimeUsernameProblemLanguageResultExecution timeMemory
385603patrikpavic2Gift (IZhO18_nicegift)C++17
100 / 100
1253 ms94620 KiB
#include <cstdio> #include <cstring> #include <vector> #include <algorithm> #include <set> #define X first #define Y second #define PB push_back using namespace std; const int N = 2e6 + 500; typedef long long ll; int n, k; ll sm, A[N]; vector < pair < ll, int > > swp; vector < ll > kv; int main(){ scanf("%d%d", &n, &k); for(int i = 1;i <= n;i++){ scanf("%lld", A + i); sm += A[i]; } if(sm % k != 0){ printf("-1\n"); return 0; } ll cik = (ll)sm / k; for(int i = 1;i <= n;i++){ if(A[i] > cik){ printf("-1\n"); return 0; } } ll cur = 0; for(int i = 1;i <= n;i++){ if(cur + A[i] <= cik){ if(cur + A[i] < cik) swp.PB({cur + A[i], -i}); swp.PB({cur, i}); kv.PB(cur); cur = (cur + A[i]) % cik; } else{ swp.PB({cur, i}); kv.PB(cur); swp.PB({0, i}); kv.PB(0); swp.PB({A[i] - (cik - cur), -i}); cur = A[i] - (cik - cur); } } sort(kv.begin(), kv.end()); kv.erase(unique(kv.begin(), kv.end()), kv.end()); printf("%d\n", (int)kv.size()); set < int > S; sort(swp.begin(), swp.end()); for(int i = 0;i < (int)swp.size();){ int j = i; while(i < (int)swp.size() && swp[i].X == swp[j].X){ if(swp[i].Y > 0) S.insert(swp[i].Y); else S.erase(-swp[i].Y); i++; } ll len = (i == (int)swp.size() ? cik : swp[i].X) - swp[j].X; printf("%lld ", len); for(int x : S) printf("%d ", x); printf("\n"); } return 0; }

Compilation message (stderr)

nicegift.cpp: In function 'int main()':
nicegift.cpp:23:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   23 |  scanf("%d%d", &n, &k);
      |  ~~~~~^~~~~~~~~~~~~~~~
nicegift.cpp:25:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   25 |   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...