#include <bits/stdc++.h>
#include <iostream>
// #define int long long
#define ld long double
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define ms(v,x) memset(v,x,sizeof(v))
#define all(v) v.begin(),v.end()
#define ff first
#define ss second
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define per(i, a, b) for(int i = b-1; i>=a ; i--)
#define trav(a, x) for(auto& a : x)
#define allin(a , x) for(auto a : x)
#define Unique(v) sort(all(v)),v.erase(unique(all(v)),v.end());
#define sz(v) ((int)v.size())
using namespace std;
typedef vector<int> vi;
#define y1 abacaba
//#define left oooooopss
#define db(x) cerr << #x <<" == "<<x << endl;
#define db2(x,y) cerr<<#x <<" == "<<x<<", "<<#y<<" == "<<y<<endl;
#define db3(x,y,z) cerr << #x<<" == "<<x<<", "<<#y<<" == "<<y<<", "<<#z<<" == "<<z<<endl;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<ll> vl;
// std::mt19937_64 rng64((int) std::chrono::steady_clock::now().time_since_epoch().count());
std::mt19937 rng(
// (int) std::chrono::steady_clock::now().time_since_epoch().count()
1239010
);
ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up
ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down
//inline ll mod(ll n, ll m ){ ll ret = n%m; if(ret < 0) ret += m; return ret; }
ll gcd(ll a, ll b){return (b == 0LL ? a : gcd(b, a%b));}
ll exp(ll b,ll e,ll m){
b%=m;
ll ans = 1;
for (; e; b = b * b % m, e /= 2)
if (e & 1) ans = ans * b % m;
return ans;
}
// debug:
template<class T>void print_vector(vector<T> &v){
rep(i,0,sz(v))cout<<v[i]<<" \n"[i==sz(v)-1];
}
void dbg_out() {cerr << endl; }
template<typename Head, typename ... Tail> void dbg_out(Head H,Tail... T){
cerr << ' ' << H;
dbg_out(T...);
}
#ifdef LOCAL
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__), cerr << endl
#else
#define dbg(...) 42
#endif
//
const int N = 1e6 + 100;
ll a[N];
const ll inf = (ll)1e18 + 9;
ll mult(ll a,ll b){
if(a > inf/b)return inf;
return a*b;
}
int32_t main(){
fastio;
int n,k;
cin >> n >> k;
ll sum=0;
rep(i,0,n){
cin >> a[i];
sum += a[i];
}
if(sum%k != 0){
cout << -1 << endl;
return 0;
}
vector<pair<ll,vi> > res;
ll divi = sum/k;
rep(i,0,n)if(a[i] > divi){
cout << -1 << endl;
return 0;
}
vector<vector<pll>> nv;
int pt=0;
while(pt < n){
ll need = divi;
vector<pll> here;
while(need > 0){
while(pt < n && a[pt]==0)pt++;
assert(pt < n);
ll put = min(a[pt], need);
if(a[pt] > divi){
cout << -1 << endl;
return 0;
}
a[pt] -= put;
need -= put;
assert(put > 0 && need>=0 && a[pt] >= 0);
here.pb(pll(put , pt));
}
while(pt < n && a[pt]==0)pt++;
nv.pb(here);
}
for(auto &v : nv)reverse(all(v));
ll put = 0;
while(divi > 0){
ll mn = inf;
for(auto &v : nv){
mn = min(mn,v.back().ff);
}
divi -= mn;
vi here;
for(auto &v : nv){
here.pb(v.back().ss);
v.back().ff -= mn;
while(sz(v) && v.back().ff == 0)v.pop_back();
}
assert(mn > 0);
res.pb({mn, here});
}
rep(i,0,n)assert(a[i]==0);
// cout << "ok" << endl;
// return 0;
cout << sz(res) << endl;
for(auto [X,v] : res){
cout << X << " ";
for(auto id : v)cout << id + 1<<" ";
cout << endl;
}
// math -> gcd it all
// Did you swap N,M? N=1?
}
Compilation message
nicegift.cpp: In function 'int32_t main()':
nicegift.cpp:141:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
141 | for(auto [X,v] : res){
| ^
nicegift.cpp:114:5: warning: unused variable 'put' [-Wunused-variable]
114 | ll put = 0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
n=4 |
2 |
Correct |
0 ms |
348 KB |
n=3 |
3 |
Correct |
0 ms |
344 KB |
n=3 |
4 |
Correct |
0 ms |
344 KB |
n=4 |
5 |
Correct |
0 ms |
348 KB |
n=4 |
6 |
Correct |
0 ms |
348 KB |
n=2 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
n=4 |
2 |
Correct |
0 ms |
348 KB |
n=3 |
3 |
Correct |
0 ms |
344 KB |
n=3 |
4 |
Correct |
0 ms |
344 KB |
n=4 |
5 |
Correct |
0 ms |
348 KB |
n=4 |
6 |
Correct |
0 ms |
348 KB |
n=2 |
7 |
Correct |
0 ms |
348 KB |
n=5 |
8 |
Correct |
0 ms |
348 KB |
n=8 |
9 |
Correct |
0 ms |
348 KB |
n=14 |
10 |
Correct |
0 ms |
348 KB |
n=11 |
11 |
Correct |
63 ms |
7364 KB |
n=50000 |
12 |
Correct |
69 ms |
6676 KB |
n=50000 |
13 |
Correct |
1 ms |
348 KB |
n=10 |
14 |
Correct |
2 ms |
348 KB |
n=685 |
15 |
Correct |
2 ms |
348 KB |
n=623 |
16 |
Correct |
2 ms |
348 KB |
n=973 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
n=4 |
2 |
Correct |
0 ms |
348 KB |
n=3 |
3 |
Correct |
0 ms |
344 KB |
n=3 |
4 |
Correct |
0 ms |
344 KB |
n=4 |
5 |
Correct |
0 ms |
348 KB |
n=4 |
6 |
Correct |
0 ms |
348 KB |
n=2 |
7 |
Correct |
0 ms |
348 KB |
n=5 |
8 |
Correct |
0 ms |
348 KB |
n=8 |
9 |
Correct |
0 ms |
348 KB |
n=14 |
10 |
Correct |
0 ms |
348 KB |
n=11 |
11 |
Correct |
63 ms |
7364 KB |
n=50000 |
12 |
Correct |
69 ms |
6676 KB |
n=50000 |
13 |
Correct |
1 ms |
348 KB |
n=10 |
14 |
Correct |
2 ms |
348 KB |
n=685 |
15 |
Correct |
2 ms |
348 KB |
n=623 |
16 |
Correct |
2 ms |
348 KB |
n=973 |
17 |
Correct |
2 ms |
348 KB |
n=989 |
18 |
Correct |
1 ms |
348 KB |
n=563 |
19 |
Correct |
2 ms |
604 KB |
n=592 |
20 |
Correct |
2 ms |
604 KB |
n=938 |
21 |
Correct |
2 ms |
408 KB |
n=747 |
22 |
Correct |
3 ms |
604 KB |
n=991 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
821 ms |
71312 KB |
n=1000000 |
2 |
Correct |
407 ms |
45136 KB |
n=666666 |
3 |
Correct |
179 ms |
25324 KB |
n=400000 |
4 |
Correct |
563 ms |
52016 KB |
n=285714 |
5 |
Correct |
4 ms |
1372 KB |
n=20000 |
6 |
Correct |
392 ms |
41136 KB |
n=181818 |
7 |
Correct |
2 ms |
860 KB |
n=10000 |
8 |
Correct |
22 ms |
3544 KB |
n=6666 |
9 |
Correct |
1 ms |
604 KB |
n=4000 |
10 |
Correct |
117 ms |
17748 KB |
n=2857 |
11 |
Correct |
1 ms |
604 KB |
n=2000 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
n=4 |
2 |
Correct |
0 ms |
348 KB |
n=3 |
3 |
Correct |
0 ms |
344 KB |
n=3 |
4 |
Correct |
0 ms |
344 KB |
n=4 |
5 |
Correct |
0 ms |
348 KB |
n=4 |
6 |
Correct |
0 ms |
348 KB |
n=2 |
7 |
Correct |
0 ms |
348 KB |
n=5 |
8 |
Correct |
0 ms |
348 KB |
n=8 |
9 |
Correct |
0 ms |
348 KB |
n=14 |
10 |
Correct |
0 ms |
348 KB |
n=11 |
11 |
Correct |
63 ms |
7364 KB |
n=50000 |
12 |
Correct |
69 ms |
6676 KB |
n=50000 |
13 |
Correct |
1 ms |
348 KB |
n=10 |
14 |
Correct |
2 ms |
348 KB |
n=685 |
15 |
Correct |
2 ms |
348 KB |
n=623 |
16 |
Correct |
2 ms |
348 KB |
n=973 |
17 |
Correct |
2 ms |
348 KB |
n=989 |
18 |
Correct |
1 ms |
348 KB |
n=563 |
19 |
Correct |
2 ms |
604 KB |
n=592 |
20 |
Correct |
2 ms |
604 KB |
n=938 |
21 |
Correct |
2 ms |
408 KB |
n=747 |
22 |
Correct |
3 ms |
604 KB |
n=991 |
23 |
Correct |
821 ms |
71312 KB |
n=1000000 |
24 |
Correct |
407 ms |
45136 KB |
n=666666 |
25 |
Correct |
179 ms |
25324 KB |
n=400000 |
26 |
Correct |
563 ms |
52016 KB |
n=285714 |
27 |
Correct |
4 ms |
1372 KB |
n=20000 |
28 |
Correct |
392 ms |
41136 KB |
n=181818 |
29 |
Correct |
2 ms |
860 KB |
n=10000 |
30 |
Correct |
22 ms |
3544 KB |
n=6666 |
31 |
Correct |
1 ms |
604 KB |
n=4000 |
32 |
Correct |
117 ms |
17748 KB |
n=2857 |
33 |
Correct |
1 ms |
604 KB |
n=2000 |
34 |
Correct |
35 ms |
4968 KB |
n=23514 |
35 |
Correct |
36 ms |
4888 KB |
n=23514 |
36 |
Correct |
2 ms |
344 KB |
n=940 |
37 |
Correct |
0 ms |
348 KB |
n=2 |
38 |
Correct |
192 ms |
14212 KB |
n=100000 |
39 |
Correct |
173 ms |
14044 KB |
n=100000 |
40 |
Correct |
0 ms |
344 KB |
n=10 |
41 |
Correct |
1 ms |
348 KB |
n=100 |
42 |
Correct |
5 ms |
860 KB |
n=1000 |
43 |
Correct |
1499 ms |
113212 KB |
n=1000000 |
44 |
Correct |
1580 ms |
119800 KB |
n=1000000 |
45 |
Correct |
1108 ms |
91168 KB |
n=666666 |
46 |
Correct |
719 ms |
60012 KB |
n=400000 |
47 |
Correct |
110 ms |
17492 KB |
n=2336 |
48 |
Correct |
546 ms |
54068 KB |
n=285714 |
49 |
Correct |
419 ms |
41460 KB |
n=181818 |
50 |
Correct |
182 ms |
25024 KB |
n=40000 |
51 |
Correct |
152 ms |
20936 KB |
n=20000 |
52 |
Correct |
125 ms |
18828 KB |
n=10000 |
53 |
Correct |
122 ms |
18460 KB |
n=6666 |
54 |
Correct |
133 ms |
18004 KB |
n=4000 |
55 |
Correct |
120 ms |
17876 KB |
n=2857 |
56 |
Correct |
112 ms |
16932 KB |
n=2000 |