제출 #495462

#제출 시각아이디문제언어결과실행 시간메모리
495462ZielGift (IZhO18_nicegift)C++17
100 / 100
696 ms127220 KiB
/** * LES GREATEABLES BRO TEAM **/ #include <bits/stdc++.h> using namespace std; using ll = long long; #define sz(x) (int)x.size() const bool FLAG = false; void setIO(const string &f = ""); string to_string(const string s) { return '"' + s + '"'; } string to_string(const char c) { return char(39) + string(1, c) + char(39); } string to_string(const char* s) { return to_string(string(s)); } string to_string(bool f) { return (f ? "true" : "false"); } template<class A, class B> string to_string(const pair<A, B> x) { return "(" + to_string(x.first) + ", " + to_string(x.second) + ")"; } template<class A, class B, class C> string to_string(const tuple<A, B, C> x) { return "(" + to_string(get<0>(x)) + ", " + to_string(get<1>(x)) + ", " + to_string(get<2>(x)) + ")"; } template<class A, class B, class C, class D> string to_string(const tuple<A, B, C, D> x) { return "(" + to_string(get<0>(x)) + ", " + to_string(get<1>(x)) + ", " + to_string(get<2>(x)) + ", " + to_string(get<3>(x)) + ")"; } template<class T> string to_string(const T v) { string res = "{"; bool f = true; for (auto x: v) res += (f ? to_string(x) : ", " + to_string(x)), f = false; return res + "}"; } void debug_args() { cerr << "]\n"; } template<class H, class... T> void debug_args(H x, T... y) { cerr << to_string(x); if (sizeof... (y)) cerr << ", "; debug_args(y...); } #ifdef LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]: [", debug_args(__VA_ARGS__); #else #define debug(...) 47 #endif #define int ll const int N = 2e6 + 4; void solve() { int n, k; cin >> n >> k; ll sum = 0, mx = 0; vector<pair<int, int>> a(n + 1); for (int i = 1; i <= n; i++) { cin >> a[i].first; a[i].second = i; mx = max(mx, a[i].first); sum += a[i].first; } if (sum % k != 0 || mx > sum / k) { cout << -1; return; } sort(a.begin() + 1, a.end(), [](auto x, auto y) { return x.first > y.first; }); sum /= k; ll cur_sum = 0; vector<pair<ll, int>> s[k + 3]; vector<ll> subsum(k + 1); ll id = 1, it = 1; for (int i = 1; i <= n; i++) { while (subsum[it] == sum) { it++; if (it > k) it -= k; } if (subsum[it] + a[i].first <= sum) { subsum[it] += a[i].first; s[it].push_back(a[i]); } else { ll y = subsum[it] + a[i].first - sum, x = a[i].first - y; subsum[it] += x; s[it].push_back({x, a[i].second}); it++; if (it > k) it -= k; subsum[it] += y; s[it].push_back({y, a[i].second}); } } /* assert(cur_sum == 0); if (id != k + 1) { cout << -1; return; } for (int i = 1; i <= k; i++) { ll check = 0; for (int j = 0; j < sz(s[i]); j++) { check += s[i][j].first; } if (check != sum) { cout << -1; return; } } for (int i = 1; i <= k; i++) { if (sz(s[i]) == 0) { cout << -1; return; } }*/ vector<vector<int>> ans; vector<int> sid(k + 1); while (true) { int mx = 1e18 + 13; for (int i = 1; i <= k; i++) { mx = min(mx, s[i][sid[i]].first); } if (mx < 1) { cout << -1; return; } vector<int> cur; cur.push_back(mx); for (int i = 1; i <= k; i++) { cur.push_back(s[i][sid[i]].second); s[i][sid[i]].first -= mx; if (s[i][sid[i]].first == 0) { sid[i]++; } } ans.push_back(cur); bool flag = 0; for (int i = 1; i <= k; i++) { if (sid[i] >= sz(s[i])) { flag = 1; break; } } if (flag) break; } cout << sz(ans) << '\n'; for (auto x: ans) { for (auto y: x) cout << y << ' '; cout << '\n'; } } signed main() { setIO(); int tt = 1; if (FLAG) { cin >> tt; } while (tt--) { solve(); } return 0; } void setIO(const string &f) { ios_base::sync_with_stdio(false); cin.tie(nullptr); if (fopen((f + ".in").c_str(), "r")) { freopen((f + ".in").c_str(), "r", stdin); freopen((f + ".out").c_str(), "w", stdout); } }

컴파일 시 표준 에러 (stderr) 메시지

nicegift.cpp: In function 'void solve()':
nicegift.cpp:83:5: warning: unused variable 'cur_sum' [-Wunused-variable]
   83 |  ll cur_sum = 0;
      |     ^~~~~~~
nicegift.cpp:86:5: warning: unused variable 'id' [-Wunused-variable]
   86 |  ll id = 1, it = 1;
      |     ^~
nicegift.cpp: In function 'void setIO(const string&)':
nicegift.cpp:186:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  186 |         freopen((f + ".in").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
nicegift.cpp:187:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  187 |         freopen((f + ".out").c_str(), "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...