Submission #395273

#TimeUsernameProblemLanguageResultExecution timeMemory
395273rama_pangGift (IZhO18_nicegift)C++17
100 / 100
683 ms124628 KiB
#include <bits/stdc++.h> using namespace std; using lint = long long; int main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); int N, K; cin >> N >> K; lint total = 0; vector<lint> A(N); for (int i = 0; i < N; i++) { cin >> A[i]; total += A[i]; } if (total % K != 0 || total / K < *max_element(begin(A), end(A))) { cout << -1 << "\n"; return 0; } lint limit = total / K; vector<lint> sum(K); vector<vector<pair<lint, int>>> data(K); for (int i = 0; i < N; i++) { for (int j = 0; j < K && A[i] > 0; j++) { if (sum[j] + A[i] <= limit) { sum[j] += A[i]; data[j].emplace_back(A[i], i); A[i] = 0; } else if (sum[j] < limit) { A[i] -= limit - sum[j]; data[j].emplace_back(limit - sum[j], i); sum[j] = limit; } } assert(A[i] == 0); } vector<pair<lint, vector<int>>> ans; while (!data[0].empty()) { lint minim = 1e18; for (int i = 0; i < K; i++) { minim = min(minim, data[i].back().first); } ans.emplace_back(); ans.back().first = minim; for (int i = 0; i < K; i++) { ans.back().second.emplace_back(data[i].back().second); data[i].back().first -= minim; if (data[i].back().first == 0) { data[i].pop_back(); } } } assert(ans.size() <= N); cout << ans.size() << "\n"; for (auto i : ans) { cout << i.first; for (auto j : i.second) { cout << " " << j + 1; } cout << "\n"; } return 0; }

Compilation message (stderr)

In file included from /usr/include/c++/9/cassert:44,
                 from /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:33,
                 from nicegift.cpp:1:
nicegift.cpp: In function 'int main()':
nicegift.cpp:60:21: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<long long int, std::vector<int> > >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   60 |   assert(ans.size() <= 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...