#include <iostream>
#include <bits/stdc++.h>
#include <cmath>
#include <algorithm>
#include <vector>
#include <deque>
#include <set>
#include <stack>
#include <string>
#include <map>
#include <queue>
#define int long long
using namespace std;
const long long oo = 1000000000000000000;
long long sum, ans = 0, mx = 0, mn = 1000000000, num, pos;
/*
ViHHiPuh
(( `'-""``""-'` ))
)-__-_.._-__-(
/ --- (o _ o) --- \
\ .-* ( .0. ) *-. /
_'-. ,_ '=' _, .-'_
/ `;#'#'# - #'#'#;` \
\_)) -----'#'----- ((_/
# --------- #
'# ------- ------ #'
/..-'# ------- #'-.\
_\...-\'# -- #'/-.../_
((____)- '#' -(____))
cout << fixed << setprecision(6) << x;
freopen ( "sum.in", "r", stdin )
*/
deque <pair<int, int> > v[ 1000001 ];
vector <vector <int > > v1;
vector <int> vv;
main () {
int n, m;
cin >> n >> m;
int a[ n + 1 ];
for ( int i = 0; i < n; i ++ ) {
cin >> a[ i ];
sum += a[ i ];
mx = max( mx, a[ i ] );
}
if ( sum % m != 0 || mx > sum / m )
cout << "-1";
else {
pos = 1;
for ( int i = 0; i < n; i ++ ) {
num += a[ i ];
if ( num > sum / m ) {
num -= a[ i ];
v[ pos ].push_back( { ( sum / m ) - num, i } );
num = a[ i ] - ( sum / m - num );
pos ++;
v[ pos ].push_back( { num, i } );
}
else {
v[ pos ].push_back( { a[ i ], i } );
if ( num == sum / m )
num = 0, pos ++;
}
}
pos --;
while ( true ) {
num = oo;
for ( int i = 1; i <= pos; i ++ ) {
num = min( num, v[ i ].front().first + 0ll );
}
ans += num;
vv.clear();
vv.push_back( num );
for ( int i = 1; i <= pos; i ++ ) {
vv.push_back( v[ i ].front().second + 1 );
v[ i ].front().first -= num;
if ( !v[ i ].front().first )
v[ i ].pop_front();
}
v1.push_back( vv );
if ( ans == sum / m )
break;
}
cout << v1.size() << "\n";
for ( auto i : v1 ) {
for ( auto j : i )
cout << j << " ";
cout << "\n";
}
}
}
Compilation message
nicegift.cpp:48:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
48 | main () {
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
223 ms |
524292 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
223 ms |
524292 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
223 ms |
524292 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
215 ms |
524292 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
223 ms |
524292 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |