#include<bits/stdc++.h>
#define f first
#define s second
#define pii pair<int,int>
#define int long long
using namespace std;
const int N = 2e5 + 5, mod = 1e9 + 7; // !
int t,a[N],n,k;
vector<pii> v[N];
vector<vector<int> > ans;
set<pii> s;
main(){
cin >> n >> k;
int mx = 0, sum = 0;
for(int i = 1; i <= n; i++) cin >> a[i], sum += a[i], mx = max(mx, a[i]);
if(sum % k || mx > sum/k) {
cout << -1;
return 0;
}
sum /= k;
int cur = 0, c = 0;
for(int i = 1; i <= n; i++) {
if(cur == sum) c++, cur = 0;
int x = min(a[i], sum - cur);
cur += x; // cout << i <<" " << x << endl;
v[c].push_back({i, x});
if(a[i] > x) {
c++;
v[c].push_back({i, a[i] - x});
cur = 0;
}
}
for(int i = 0; i <= c; i++) reverse(v[i].begin(), v[i].end()),
s.insert({v[i].back().s, i});
while(s.size()) {
int x = (*s.begin()).f;
vector<int> a;
a.push_back(x);
assert(x > 0);
for(set<pii> :: iterator it = s.begin(); it != s.end(); it++) {
a.push_back( v[(*it).s].back().f);
v[(*it).s].back().s -= x;
if(!v[(*it).s].back().s) v[(*it).s].pop_back();
}
ans.push_back(a);
s.clear();
if(!v[0].size()) break;
for(int i = 0; i <= c; i++) {
if(!v[i].size()) {
cout << -1;
return 0;
}
s.insert({v[i].back().s, i});
}
}
cout << ans.size() << endl;
for(int i = 0; i < ans.size(); i++) {
for(int j = 0; j < ans[i].size(); j++) {
cout << ans[i][j] <<" ";
}
cout << endl;
}
}
Compilation message
nicegift.cpp:12:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
12 | main(){
| ^~~~
nicegift.cpp: In function 'int main()':
nicegift.cpp:57:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | for(int i = 0; i < ans.size(); i++) {
| ~~^~~~~~~~~~~~
nicegift.cpp:58:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for(int j = 0; j < ans[i].size(); j++) {
| ~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
n=4 |
2 |
Correct |
3 ms |
4940 KB |
n=3 |
3 |
Correct |
3 ms |
4940 KB |
n=3 |
4 |
Correct |
2 ms |
4940 KB |
n=4 |
5 |
Correct |
3 ms |
4940 KB |
n=4 |
6 |
Correct |
2 ms |
4940 KB |
n=2 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
n=4 |
2 |
Correct |
3 ms |
4940 KB |
n=3 |
3 |
Correct |
3 ms |
4940 KB |
n=3 |
4 |
Correct |
2 ms |
4940 KB |
n=4 |
5 |
Correct |
3 ms |
4940 KB |
n=4 |
6 |
Correct |
2 ms |
4940 KB |
n=2 |
7 |
Correct |
3 ms |
4940 KB |
n=5 |
8 |
Correct |
2 ms |
4940 KB |
n=8 |
9 |
Correct |
3 ms |
4880 KB |
n=14 |
10 |
Correct |
2 ms |
4940 KB |
n=11 |
11 |
Correct |
77 ms |
9100 KB |
n=50000 |
12 |
Correct |
73 ms |
8964 KB |
n=50000 |
13 |
Correct |
3 ms |
4940 KB |
n=10 |
14 |
Correct |
4 ms |
5068 KB |
n=685 |
15 |
Correct |
3 ms |
5068 KB |
n=623 |
16 |
Correct |
4 ms |
5068 KB |
n=973 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
n=4 |
2 |
Correct |
3 ms |
4940 KB |
n=3 |
3 |
Correct |
3 ms |
4940 KB |
n=3 |
4 |
Correct |
2 ms |
4940 KB |
n=4 |
5 |
Correct |
3 ms |
4940 KB |
n=4 |
6 |
Correct |
2 ms |
4940 KB |
n=2 |
7 |
Correct |
3 ms |
4940 KB |
n=5 |
8 |
Correct |
2 ms |
4940 KB |
n=8 |
9 |
Correct |
3 ms |
4880 KB |
n=14 |
10 |
Correct |
2 ms |
4940 KB |
n=11 |
11 |
Correct |
77 ms |
9100 KB |
n=50000 |
12 |
Correct |
73 ms |
8964 KB |
n=50000 |
13 |
Correct |
3 ms |
4940 KB |
n=10 |
14 |
Correct |
4 ms |
5068 KB |
n=685 |
15 |
Correct |
3 ms |
5068 KB |
n=623 |
16 |
Correct |
4 ms |
5068 KB |
n=973 |
17 |
Incorrect |
3 ms |
5068 KB |
Jury has the answer but participant has not |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
102 ms |
13028 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
n=4 |
2 |
Correct |
3 ms |
4940 KB |
n=3 |
3 |
Correct |
3 ms |
4940 KB |
n=3 |
4 |
Correct |
2 ms |
4940 KB |
n=4 |
5 |
Correct |
3 ms |
4940 KB |
n=4 |
6 |
Correct |
2 ms |
4940 KB |
n=2 |
7 |
Correct |
3 ms |
4940 KB |
n=5 |
8 |
Correct |
2 ms |
4940 KB |
n=8 |
9 |
Correct |
3 ms |
4880 KB |
n=14 |
10 |
Correct |
2 ms |
4940 KB |
n=11 |
11 |
Correct |
77 ms |
9100 KB |
n=50000 |
12 |
Correct |
73 ms |
8964 KB |
n=50000 |
13 |
Correct |
3 ms |
4940 KB |
n=10 |
14 |
Correct |
4 ms |
5068 KB |
n=685 |
15 |
Correct |
3 ms |
5068 KB |
n=623 |
16 |
Correct |
4 ms |
5068 KB |
n=973 |
17 |
Incorrect |
3 ms |
5068 KB |
Jury has the answer but participant has not |
18 |
Halted |
0 ms |
0 KB |
- |