#include<bits/stdc++.h>
using namespace std;
#define int long long
int n,k;
int a[1000005];
priority_queue<pair<int,int>> pq;
vector<vector<int>> af;
set<pair<int,int>> vals;
int getmin()
{
set<pair<int,int>>::iterator it = vals.begin();
pair<int,int> cop = *it;
return cop.first;
}
int getmax()
{
set<pair<int,int>>::iterator it = vals.end();
it--;
pair<int,int> cop = *it;
return cop.first;
}
signed main()
{
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n>>k;
int sum=0;
for(int i=1;i<=n;i++)
{
cin>>a[i];
sum += a[i];
pq.push({a[i],i});
}
if(sum%k!=0)
{
cout<<-1;
return 0;
}
if(sum<=1000000)
{
while(!pq.empty())
{
if((int)pq.size()<k)
{
cout<<-1;
return 0;
}
vector<int> sc;
//cout<<1<<" ";
for(int i=0;i<k;i++)
{
sc.push_back(pq.top().second);
a[sc.back()]--;
//cout<<sc.back()<<" ";
pq.pop();
}
//cout<<"\n";
for(auto x:sc)
if(a[x]>0)
pq.push({a[x],x});
af.push_back(sc);
}
cout<<af.size()<<"\n";
for(int i=0;i<(int)af.size();i++)
{
cout<<1<<" ";
for(int j=0;j<af[i].size();j++)
cout<<af[i][j]<<" ";
cout<<"\n";
}
}
else
{
if(n%k==0 && 0)
{
cout<<n/k<<"\n";
for(int i=1;i<=n;i+=k)
{
cout<<a[1]<<" ";
for(int j=i;j<i+k;j++)
cout<<j<<" ";
cout<<"\n";
}
}
else if(a[1]%k==0 && 0)
{
cout<<n<<"\n";
for(int i=1;i<=n;i++)
{
cout<<a[1]/k<<" ";
for(int j=0;j<k;j++)
cout<<(i-1+j)%n + 1<<" ";
cout<<"\n";
}
}
else
{
int init = a[1];
for(int i=1;i<=n;i++)
vals.insert({a[i],i});
while(!pq.empty())
{
if((int)pq.size()<k)
{
cout<<-1;
return 0;
}
vector<int> sc;
//cout<<1<<" ";
for(int i=0;i<k;i++)
{
sc.push_back(pq.top().second);
vals.erase({a[sc.back()],sc.back()});
a[sc.back()]--;
//cout<<sc.back()<<" ";
pq.pop();
}
//cout<<"\n";
for(auto x:sc)
{
if(a[x]>0)
{
pq.push({a[x],x});
vals.insert({a[x],x});
}
}
af.push_back(sc);
if(pq.empty())
break;
if(getmin()==getmax() && (int)pq.size()==n)
{
if(init%(init-a[1])==0)
{
cout<<(int)af.size()<<"\n";
for(int i=0;i<(int)af.size();i++)
{
cout<<init/(init-a[1])<<" ";
for(int j=0;j<af[i].size();j++)
cout<<af[i][j]<<" ";
cout<<"\n";
}
}
else
{
cout<<-1;
}
return 0;
}
}
cout<<af.size()<<"\n";
for(int i=0;i<(int)af.size();i++)
{
cout<<1<<" ";
for(int j=0;j<af[i].size();j++)
cout<<af[i][j]<<" ";
cout<<"\n";
}
}
}
return 0;
}
/**
4 2
2 3 3 2
*/
Compilation message
nicegift.cpp: In function 'int main()':
nicegift.cpp:66:26: 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]
66 | for(int j=0;j<af[i].size();j++)
| ~^~~~~~~~~~~~~
nicegift.cpp:137:42: 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]
137 | for(int j=0;j<af[i].size();j++)
| ~^~~~~~~~~~~~~
nicegift.cpp:153:30: 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]
153 | for(int j=0;j<af[i].size();j++)
| ~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
n=4 |
2 |
Correct |
0 ms |
348 KB |
n=3 |
3 |
Correct |
0 ms |
348 KB |
n=3 |
4 |
Correct |
0 ms |
348 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 |
348 KB |
n=3 |
4 |
Correct |
0 ms |
348 KB |
n=4 |
5 |
Correct |
0 ms |
348 KB |
n=4 |
6 |
Correct |
0 ms |
348 KB |
n=2 |
7 |
Correct |
0 ms |
344 KB |
n=5 |
8 |
Correct |
2 ms |
964 KB |
n=8 |
9 |
Correct |
5 ms |
1176 KB |
n=14 |
10 |
Correct |
4 ms |
1116 KB |
n=11 |
11 |
Correct |
31 ms |
6864 KB |
n=50000 |
12 |
Correct |
24 ms |
6860 KB |
n=50000 |
13 |
Correct |
20 ms |
3344 KB |
n=10 |
14 |
Correct |
18 ms |
3088 KB |
n=685 |
15 |
Correct |
21 ms |
3276 KB |
n=623 |
16 |
Correct |
11 ms |
2068 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 |
348 KB |
n=3 |
4 |
Correct |
0 ms |
348 KB |
n=4 |
5 |
Correct |
0 ms |
348 KB |
n=4 |
6 |
Correct |
0 ms |
348 KB |
n=2 |
7 |
Correct |
0 ms |
344 KB |
n=5 |
8 |
Correct |
2 ms |
964 KB |
n=8 |
9 |
Correct |
5 ms |
1176 KB |
n=14 |
10 |
Correct |
4 ms |
1116 KB |
n=11 |
11 |
Correct |
31 ms |
6864 KB |
n=50000 |
12 |
Correct |
24 ms |
6860 KB |
n=50000 |
13 |
Correct |
20 ms |
3344 KB |
n=10 |
14 |
Correct |
18 ms |
3088 KB |
n=685 |
15 |
Correct |
21 ms |
3276 KB |
n=623 |
16 |
Correct |
11 ms |
2068 KB |
n=973 |
17 |
Correct |
20 ms |
2580 KB |
n=989 |
18 |
Correct |
11 ms |
1372 KB |
n=563 |
19 |
Correct |
15 ms |
1628 KB |
n=592 |
20 |
Correct |
15 ms |
1676 KB |
n=938 |
21 |
Correct |
11 ms |
1372 KB |
n=747 |
22 |
Correct |
13 ms |
1484 KB |
n=991 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1022 ms |
128744 KB |
n=1000000 |
2 |
Correct |
587 ms |
79832 KB |
n=666666 |
3 |
Correct |
308 ms |
49080 KB |
n=400000 |
4 |
Correct |
848 ms |
72608 KB |
n=285714 |
5 |
Correct |
12 ms |
2360 KB |
n=20000 |
6 |
Correct |
760 ms |
54400 KB |
n=181818 |
7 |
Correct |
5 ms |
1248 KB |
n=10000 |
8 |
Correct |
94 ms |
5256 KB |
n=6666 |
9 |
Correct |
2 ms |
856 KB |
n=4000 |
10 |
Correct |
550 ms |
25604 KB |
n=2857 |
11 |
Correct |
2 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 |
348 KB |
n=3 |
4 |
Correct |
0 ms |
348 KB |
n=4 |
5 |
Correct |
0 ms |
348 KB |
n=4 |
6 |
Correct |
0 ms |
348 KB |
n=2 |
7 |
Correct |
0 ms |
344 KB |
n=5 |
8 |
Correct |
2 ms |
964 KB |
n=8 |
9 |
Correct |
5 ms |
1176 KB |
n=14 |
10 |
Correct |
4 ms |
1116 KB |
n=11 |
11 |
Correct |
31 ms |
6864 KB |
n=50000 |
12 |
Correct |
24 ms |
6860 KB |
n=50000 |
13 |
Correct |
20 ms |
3344 KB |
n=10 |
14 |
Correct |
18 ms |
3088 KB |
n=685 |
15 |
Correct |
21 ms |
3276 KB |
n=623 |
16 |
Correct |
11 ms |
2068 KB |
n=973 |
17 |
Correct |
20 ms |
2580 KB |
n=989 |
18 |
Correct |
11 ms |
1372 KB |
n=563 |
19 |
Correct |
15 ms |
1628 KB |
n=592 |
20 |
Correct |
15 ms |
1676 KB |
n=938 |
21 |
Correct |
11 ms |
1372 KB |
n=747 |
22 |
Correct |
13 ms |
1484 KB |
n=991 |
23 |
Correct |
1022 ms |
128744 KB |
n=1000000 |
24 |
Correct |
587 ms |
79832 KB |
n=666666 |
25 |
Correct |
308 ms |
49080 KB |
n=400000 |
26 |
Correct |
848 ms |
72608 KB |
n=285714 |
27 |
Correct |
12 ms |
2360 KB |
n=20000 |
28 |
Correct |
760 ms |
54400 KB |
n=181818 |
29 |
Correct |
5 ms |
1248 KB |
n=10000 |
30 |
Correct |
94 ms |
5256 KB |
n=6666 |
31 |
Correct |
2 ms |
856 KB |
n=4000 |
32 |
Correct |
550 ms |
25604 KB |
n=2857 |
33 |
Correct |
2 ms |
604 KB |
n=2000 |
34 |
Execution timed out |
2077 ms |
191960 KB |
Time limit exceeded |
35 |
Halted |
0 ms |
0 KB |
- |