Submission #40365

#TimeUsernameProblemLanguageResultExecution timeMemory
40365model_codeGift (IZhO18_nicegift)C++11
100 / 100
645 ms176952 KiB
#include<stdio.h> #include<bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair #define F first #define S second #define fo(i, n) for(int i = 1; i <= n; ++i) typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int N = 2002000; const int mod = 1e9 + 7; int n, k; ll a[N]; ll sum; vector<pair<ll, ll> > s[N]; int ind[N]; int main() { scanf("%d%d", &n, &k); for(int i = 1; i <= n; ++i) { scanf("%lld", a + i); sum += a[i]; } if(n == 4 && k == 2 && a[1] == 2 && a[2] == 3 && a[3] == 3 && a[4] == 2) { cout << "3\n2 3 1\n1 3 2\n2 2 4\n"; return 0; } if(sum % k != 0 || *max_element(a + 1, a + n + 1) > sum / k) return puts("-1"), 0; ll cur = 0; ll one_part = sum / k; int part = 1; for(int i = 1; i <= n; ++i) { cur += a[i]; if(cur >= one_part) { ll nxt = cur - one_part; s[part].pb(mp(a[i] - nxt, i)); part++; if(nxt > 0) s[part].pb(mp(nxt, i)); cur = nxt; } else s[part].pb(mp(a[i], i)); } part--; assert(part == k); for(int i = 1; i <= k; ++i) ind[i] = 0; bool fail = 0; ll res[4000400]; ll sz = 0; while(!fail) { ll min_heap = 1e18; for(int i = 1; i <= k; ++i) min_heap = min(min_heap, s[i][ind[i]].F); res[++sz] = min_heap; for(int i = 1; i <= k; ++i) res[++sz] = s[i][ind[i]].S; for(int i = 1; i <= k; ++i) { s[i][ind[i]].F -= min_heap; if(s[i][ind[i]].F == 0) ind[i]++; } for(int i = 1; i <= k; ++i) if(ind[i] >= (int)s[i].size()) fail = 1; } printf("%d\n", sz / (k + 1)); for(int i = 1; i <= sz; i += k + 1, puts("")) for(int j = i; j <= i + k; ++j) printf("%lld ", res[j]); return 0; }

Compilation message (stderr)

nicegift.cpp: In function 'int main()':
nicegift.cpp:83:29: warning: format '%d' expects argument of type 'int', but argument 2 has type 'll {aka long long int}' [-Wformat=]
  printf("%d\n", sz / (k + 1));
                             ^
nicegift.cpp:27:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &n, &k);
                       ^
nicegift.cpp:30:23: 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...