#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <map>
#include <set>
using namespace std;
typedef double ldouble;
typedef long long llong;
const int maxN = 100001;
int n, k;
vector<llong> arr;
multiset<pair<llong, int>> a;
llong mx = -1, sum = 0;
vector<pair<llong, vector<int>>> ans;
void Subtract(multiset<pair<llong, int>>::reverse_iterator s, int depth = 0)
{
if (depth >= 2) return;
//cerr << "1\n";
auto st_s = s;
vector<int> cur;
llong mn = s->first;
auto it = s;
for (; cur.size() < k; ++it)
{
cur.push_back(it->second);
mn = min(mn, it->first);
}
int st_mn = mn;
if (it != a.rend())
mn = min(mn, sum / k - it->first);
if (st_s != a.rbegin())
mn = min(mn, sum / k - a.rbegin()->first);
if (!mn)
return;
if (st_mn < mn)
{
auto st_it = it;
bool itIsEnd = false;
for (int x = 0; x < k; ++x)
{
if (it == a.rend())
{
itIsEnd = true;
break;
}
++it;
}
if (!itIsEnd)
{
Subtract(st_it, depth + 1);
return;
}
}
// sum / k - it->first < mn
// sum / k < mn + it->first
// sum / k < 2 * it->first
// sum < k * it->first * 2
// sum > k * it->first
if (!mn) throw;
// it->first <= (sum - k * mn) / k
// k * it->first <= sum - k * mn
// mn * k <= sum - k * it->first
// mn <= sum / k - it->first
/*
mn = min(mn, a.rbegin()->first - it->first + 1);
*/
/*
if (!mn)
{
cout << -1 << endl;
exit(0);
}
*/
for (int x: cur)
{
a.erase({arr[x], x});
sum -= arr[x];
a.insert({arr[x] -= mn, x});
sum += arr[x];
}
ans.push_back({mn, cur});
/*cerr << "a` ";
for (pair<llong, int> i: a)
cerr << i.first << ' ';
cerr << endl;*/
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> n >> k;
arr.resize(n);
for (int i = 0; i < n; ++i)
{
cin >> arr[i];
sum += arr[i];
mx = max(mx, arr[i]);
a.insert({arr[i], i});
}
if (sum % k || mx > sum / k)
{
cout << -1 << endl;
exit(0);
}
while (a.rbegin()->first)
{
//cerr << "0\n";
Subtract(a.rbegin());
/*cerr << endl << ":";
for (auto it: a)
cerr << it.first << ' ';
cerr << ":" << endl;*/
}
cout << ans.size() << '\n';
for (auto x: ans)
{
cout << x.first << ' ';
for (int y: x.second)
cout << y + 1 << ' ';
cout << '\n';
}
}
// 6 5 4 3 x
// x x-1 x-2 x-3 x
Compilation message
nicegift.cpp: In function 'void Subtract(std::multiset<std::pair<long long int, int> >::reverse_iterator, int)':
nicegift.cpp:27:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (; cur.size() < k; ++it)
~~~~~~~~~~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
n=4 |
2 |
Correct |
2 ms |
376 KB |
n=3 |
3 |
Correct |
2 ms |
376 KB |
n=3 |
4 |
Correct |
2 ms |
376 KB |
n=4 |
5 |
Correct |
2 ms |
376 KB |
n=4 |
6 |
Correct |
2 ms |
376 KB |
n=2 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
n=4 |
2 |
Correct |
2 ms |
376 KB |
n=3 |
3 |
Correct |
2 ms |
376 KB |
n=3 |
4 |
Correct |
2 ms |
376 KB |
n=4 |
5 |
Correct |
2 ms |
376 KB |
n=4 |
6 |
Correct |
2 ms |
376 KB |
n=2 |
7 |
Correct |
2 ms |
376 KB |
n=5 |
8 |
Correct |
2 ms |
256 KB |
n=8 |
9 |
Correct |
2 ms |
376 KB |
n=14 |
10 |
Correct |
2 ms |
376 KB |
n=11 |
11 |
Correct |
58 ms |
6244 KB |
n=50000 |
12 |
Correct |
56 ms |
5868 KB |
n=50000 |
13 |
Correct |
0 ms |
248 KB |
n=10 |
14 |
Correct |
3 ms |
476 KB |
n=685 |
15 |
Correct |
0 ms |
380 KB |
n=623 |
16 |
Correct |
3 ms |
376 KB |
n=973 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
n=4 |
2 |
Correct |
2 ms |
376 KB |
n=3 |
3 |
Correct |
2 ms |
376 KB |
n=3 |
4 |
Correct |
2 ms |
376 KB |
n=4 |
5 |
Correct |
2 ms |
376 KB |
n=4 |
6 |
Correct |
2 ms |
376 KB |
n=2 |
7 |
Correct |
2 ms |
376 KB |
n=5 |
8 |
Correct |
2 ms |
256 KB |
n=8 |
9 |
Correct |
2 ms |
376 KB |
n=14 |
10 |
Correct |
2 ms |
376 KB |
n=11 |
11 |
Correct |
58 ms |
6244 KB |
n=50000 |
12 |
Correct |
56 ms |
5868 KB |
n=50000 |
13 |
Correct |
0 ms |
248 KB |
n=10 |
14 |
Correct |
3 ms |
476 KB |
n=685 |
15 |
Correct |
0 ms |
380 KB |
n=623 |
16 |
Correct |
3 ms |
376 KB |
n=973 |
17 |
Correct |
3 ms |
504 KB |
n=989 |
18 |
Correct |
3 ms |
504 KB |
n=563 |
19 |
Correct |
3 ms |
376 KB |
n=592 |
20 |
Correct |
3 ms |
376 KB |
n=938 |
21 |
Correct |
3 ms |
376 KB |
n=747 |
22 |
Correct |
18 ms |
504 KB |
n=991 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2093 ms |
70804 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
n=4 |
2 |
Correct |
2 ms |
376 KB |
n=3 |
3 |
Correct |
2 ms |
376 KB |
n=3 |
4 |
Correct |
2 ms |
376 KB |
n=4 |
5 |
Correct |
2 ms |
376 KB |
n=4 |
6 |
Correct |
2 ms |
376 KB |
n=2 |
7 |
Correct |
2 ms |
376 KB |
n=5 |
8 |
Correct |
2 ms |
256 KB |
n=8 |
9 |
Correct |
2 ms |
376 KB |
n=14 |
10 |
Correct |
2 ms |
376 KB |
n=11 |
11 |
Correct |
58 ms |
6244 KB |
n=50000 |
12 |
Correct |
56 ms |
5868 KB |
n=50000 |
13 |
Correct |
0 ms |
248 KB |
n=10 |
14 |
Correct |
3 ms |
476 KB |
n=685 |
15 |
Correct |
0 ms |
380 KB |
n=623 |
16 |
Correct |
3 ms |
376 KB |
n=973 |
17 |
Correct |
3 ms |
504 KB |
n=989 |
18 |
Correct |
3 ms |
504 KB |
n=563 |
19 |
Correct |
3 ms |
376 KB |
n=592 |
20 |
Correct |
3 ms |
376 KB |
n=938 |
21 |
Correct |
3 ms |
376 KB |
n=747 |
22 |
Correct |
18 ms |
504 KB |
n=991 |
23 |
Execution timed out |
2093 ms |
70804 KB |
Time limit exceeded |
24 |
Halted |
0 ms |
0 KB |
- |