#include <bits/stdc++.h>
//#include <ext/pb_ds/detail/standard_policies.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx2")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
using namespace std;
//using namespace __gnu_pbds;
//template <typename T> using ordered_set = tree <T, null_type, less < T >, rb_tree_tag, tree_order_statistics_node_update>;
mt19937 gen(time(0));
#define ll long long
#define ld long double
#define pb push_back
#define F first
#define S second
#define TIME clock() * 1.0 / CLOCKS_PER_SEC
#define sz(a) int32_t(a.size())
#define endl '\n'
//#define int long long
const int N = 4e6 + 100;
const int M = 18;
const int mod = 998244353;
const int inf = 1e9 + 7;
pair < ll, int > a[N];
set < pair < pair < int, int >, pair < ll, int > > > st;
vector < int > ans[N];
int nxt[1000100];
int get(int i)
{
if (i == -1 || a[i].F) return i;
return nxt[i] = get(nxt[i]);
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#else
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif // LOCAL
int n, k;
cin >> n >> k;
ll sum = 0, mx = 0;
for(int i = 0; i < n; i++)
{
cin >> a[i].F;
mx = max(mx, a[i].F);
sum += a[i].F;
a[i].S = i;
nxt[i] = i + 1;
}
nxt[n - 1] = -1;
if (sum % k || mx > sum / k)
{
cout << -1;
return 0;
}
sort(a, a + n);
reverse(a, a + n);
int uk = 1;
st.insert({{0, 0}, {sum / k, 0}});
vector < pair < ll, int > > ve;
while (sz(st))
{
pair < pair < int, int >, pair < ll, int > > p = *st.begin();
st.erase(st.begin());
int sz = p.F.F, i = get(p.F.S), numb = p.S.S; ll k = p.S.F;
if (i == -1)
{
ve.pb({k, numb});
continue;
}
if (a[i].F >= k)
{
a[i].F -= k;
ans[numb].pb(a[i].S);
st.insert({{sz + 1, i + 1}, {k, numb}});
continue;
}
for(auto to: ans[numb])
ans[uk].pb(to);
ans[uk].pb(a[i].S);
st.insert({{sz, i + 1}, {k - a[i].F, numb}});
st.insert({{sz + 1, i + 1}, {a[i].F, uk}});
a[i].F = 0;
uk++;
}
cout << uk << endl;
for(auto to: ve)
{
cout << to.F << " ";
for(auto x: ans[to.S])
cout << x + 1 << " ";
cout << endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
94192 KB |
n=4 |
2 |
Correct |
43 ms |
94156 KB |
n=3 |
3 |
Correct |
44 ms |
94248 KB |
n=3 |
4 |
Correct |
44 ms |
94208 KB |
n=4 |
5 |
Correct |
43 ms |
94228 KB |
n=4 |
6 |
Correct |
53 ms |
94132 KB |
n=2 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
94192 KB |
n=4 |
2 |
Correct |
43 ms |
94156 KB |
n=3 |
3 |
Correct |
44 ms |
94248 KB |
n=3 |
4 |
Correct |
44 ms |
94208 KB |
n=4 |
5 |
Correct |
43 ms |
94228 KB |
n=4 |
6 |
Correct |
53 ms |
94132 KB |
n=2 |
7 |
Correct |
43 ms |
94168 KB |
n=5 |
8 |
Correct |
43 ms |
94220 KB |
n=8 |
9 |
Correct |
45 ms |
94152 KB |
n=14 |
10 |
Correct |
44 ms |
94188 KB |
n=11 |
11 |
Correct |
85 ms |
102456 KB |
n=50000 |
12 |
Correct |
82 ms |
101928 KB |
n=50000 |
13 |
Correct |
48 ms |
94264 KB |
n=10 |
14 |
Correct |
45 ms |
94316 KB |
n=685 |
15 |
Correct |
46 ms |
94244 KB |
n=623 |
16 |
Correct |
47 ms |
94404 KB |
n=973 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
94192 KB |
n=4 |
2 |
Correct |
43 ms |
94156 KB |
n=3 |
3 |
Correct |
44 ms |
94248 KB |
n=3 |
4 |
Correct |
44 ms |
94208 KB |
n=4 |
5 |
Correct |
43 ms |
94228 KB |
n=4 |
6 |
Correct |
53 ms |
94132 KB |
n=2 |
7 |
Correct |
43 ms |
94168 KB |
n=5 |
8 |
Correct |
43 ms |
94220 KB |
n=8 |
9 |
Correct |
45 ms |
94152 KB |
n=14 |
10 |
Correct |
44 ms |
94188 KB |
n=11 |
11 |
Correct |
85 ms |
102456 KB |
n=50000 |
12 |
Correct |
82 ms |
101928 KB |
n=50000 |
13 |
Correct |
48 ms |
94264 KB |
n=10 |
14 |
Correct |
45 ms |
94316 KB |
n=685 |
15 |
Correct |
46 ms |
94244 KB |
n=623 |
16 |
Correct |
47 ms |
94404 KB |
n=973 |
17 |
Correct |
44 ms |
94348 KB |
n=989 |
18 |
Correct |
46 ms |
94348 KB |
n=563 |
19 |
Correct |
52 ms |
94456 KB |
n=592 |
20 |
Correct |
48 ms |
94404 KB |
n=938 |
21 |
Correct |
46 ms |
94460 KB |
n=747 |
22 |
Correct |
48 ms |
94488 KB |
n=991 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
750 ms |
204676 KB |
n=1000000 |
2 |
Correct |
450 ms |
152304 KB |
n=666666 |
3 |
Correct |
274 ms |
123932 KB |
n=400000 |
4 |
Correct |
188 ms |
112660 KB |
n=285714 |
5 |
Correct |
53 ms |
95268 KB |
n=20000 |
6 |
Correct |
128 ms |
104788 KB |
n=181818 |
7 |
Correct |
47 ms |
94656 KB |
n=10000 |
8 |
Correct |
79 ms |
97700 KB |
n=6666 |
9 |
Correct |
44 ms |
94424 KB |
n=4000 |
10 |
Correct |
206 ms |
106596 KB |
n=2857 |
11 |
Correct |
42 ms |
94280 KB |
n=2000 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
94192 KB |
n=4 |
2 |
Correct |
43 ms |
94156 KB |
n=3 |
3 |
Correct |
44 ms |
94248 KB |
n=3 |
4 |
Correct |
44 ms |
94208 KB |
n=4 |
5 |
Correct |
43 ms |
94228 KB |
n=4 |
6 |
Correct |
53 ms |
94132 KB |
n=2 |
7 |
Correct |
43 ms |
94168 KB |
n=5 |
8 |
Correct |
43 ms |
94220 KB |
n=8 |
9 |
Correct |
45 ms |
94152 KB |
n=14 |
10 |
Correct |
44 ms |
94188 KB |
n=11 |
11 |
Correct |
85 ms |
102456 KB |
n=50000 |
12 |
Correct |
82 ms |
101928 KB |
n=50000 |
13 |
Correct |
48 ms |
94264 KB |
n=10 |
14 |
Correct |
45 ms |
94316 KB |
n=685 |
15 |
Correct |
46 ms |
94244 KB |
n=623 |
16 |
Correct |
47 ms |
94404 KB |
n=973 |
17 |
Correct |
44 ms |
94348 KB |
n=989 |
18 |
Correct |
46 ms |
94348 KB |
n=563 |
19 |
Correct |
52 ms |
94456 KB |
n=592 |
20 |
Correct |
48 ms |
94404 KB |
n=938 |
21 |
Correct |
46 ms |
94460 KB |
n=747 |
22 |
Correct |
48 ms |
94488 KB |
n=991 |
23 |
Correct |
750 ms |
204676 KB |
n=1000000 |
24 |
Correct |
450 ms |
152304 KB |
n=666666 |
25 |
Correct |
274 ms |
123932 KB |
n=400000 |
26 |
Correct |
188 ms |
112660 KB |
n=285714 |
27 |
Correct |
53 ms |
95268 KB |
n=20000 |
28 |
Correct |
128 ms |
104788 KB |
n=181818 |
29 |
Correct |
47 ms |
94656 KB |
n=10000 |
30 |
Correct |
79 ms |
97700 KB |
n=6666 |
31 |
Correct |
44 ms |
94424 KB |
n=4000 |
32 |
Correct |
206 ms |
106596 KB |
n=2857 |
33 |
Correct |
42 ms |
94280 KB |
n=2000 |
34 |
Correct |
62 ms |
97988 KB |
n=23514 |
35 |
Correct |
65 ms |
98004 KB |
n=23514 |
36 |
Correct |
55 ms |
94452 KB |
n=940 |
37 |
Correct |
49 ms |
94192 KB |
n=2 |
38 |
Correct |
227 ms |
115040 KB |
n=100000 |
39 |
Correct |
211 ms |
115004 KB |
n=100000 |
40 |
Correct |
49 ms |
94224 KB |
n=10 |
41 |
Correct |
49 ms |
94272 KB |
n=100 |
42 |
Correct |
60 ms |
94784 KB |
n=1000 |
43 |
Correct |
1193 ms |
256280 KB |
n=1000000 |
44 |
Correct |
1591 ms |
269492 KB |
n=1000000 |
45 |
Correct |
1228 ms |
213436 KB |
n=666666 |
46 |
Correct |
930 ms |
185904 KB |
n=400000 |
47 |
Correct |
386 ms |
113504 KB |
n=2336 |
48 |
Correct |
796 ms |
160944 KB |
n=285714 |
49 |
Correct |
678 ms |
148556 KB |
n=181818 |
50 |
Correct |
411 ms |
122024 KB |
n=40000 |
51 |
Correct |
372 ms |
118296 KB |
n=20000 |
52 |
Correct |
386 ms |
115576 KB |
n=10000 |
53 |
Correct |
398 ms |
118664 KB |
n=6666 |
54 |
Correct |
334 ms |
110900 KB |
n=4000 |
55 |
Correct |
357 ms |
115848 KB |
n=2857 |
56 |
Correct |
377 ms |
111468 KB |
n=2000 |