Submission #926698

# Submission time Handle Problem Language Result Execution time Memory
926698 2024-02-13T14:18:50 Z shenfe1 Gift (IZhO18_nicegift) C++17
100 / 100
608 ms 121896 KB
#include<bits/stdc++.h>

using namespace std;

#define F first
#define S second
#define ll long long
#define maksim gay
#define int ll
#define pb push_back
#define sz(s) (int)s.size()
#define pii pair<int,int>
#define all(v) v.begin(),v.end()
#define mem(a,i) memset(a,i,sizeof(a))
#define in insert
#define lb lower_bound

const int MAX=2e6+10;
const int inf=1e10;
const int N=2e5;

int n,k;
int sum;
int a[MAX];

void solve(){
  cin>>n>>k;
  priority_queue<pii> q;
  for(int i=1;i<=n;i++){
    cin>>a[i];
    sum+=a[i];
    q.push({a[i],i});
  }
  if(sum%k||q.top().F>sum/k){
    cout<<-1;
    return;
  }
  vector<vector<pii>> res;
  while(!q.empty()){
    vector<pii> v;
    for(int i=1;i<=k;i++){
      v.pb(q.top());
      q.pop();
    }
    int x=v.back().F;
    if(!q.empty())x=min(x,sum/k-q.top().F);
    sum-=k*x;
    v.pb({x,x});
    for(int i=0;i<k;i++){
      if(v[i].F!=x){
        v[i].F-=x;
        q.push(v[i]);
      }
    }
    res.pb(v);
  }
  reverse(all(res));
  cout<<sz(res)<<"\n";
  for(auto x:res){
    cout<<x.back().S<<" ";
    for(int i=0;i<k;i++)cout<<x[i].S<<" ";
    cout<<"\n";
  }
}


main(){
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t=1;
  // cin>>t;
  while(t--){
    solve();
  }
}

Compilation message

nicegift.cpp:67:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   67 | main(){
      | ^~~~
# Verdict Execution time Memory 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 1 ms 348 KB n=4
5 Correct 0 ms 348 KB n=4
6 Correct 0 ms 348 KB n=2
# Verdict Execution time Memory 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 1 ms 348 KB n=4
5 Correct 0 ms 348 KB n=4
6 Correct 0 ms 348 KB n=2
7 Correct 1 ms 344 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 19 ms 6496 KB n=50000
12 Correct 15 ms 6064 KB n=50000
13 Correct 0 ms 348 KB n=10
14 Correct 1 ms 348 KB n=685
15 Correct 0 ms 348 KB n=623
16 Correct 1 ms 344 KB n=973
# Verdict Execution time Memory 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 1 ms 348 KB n=4
5 Correct 0 ms 348 KB n=4
6 Correct 0 ms 348 KB n=2
7 Correct 1 ms 344 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 19 ms 6496 KB n=50000
12 Correct 15 ms 6064 KB n=50000
13 Correct 0 ms 348 KB n=10
14 Correct 1 ms 348 KB n=685
15 Correct 0 ms 348 KB n=623
16 Correct 1 ms 344 KB n=973
17 Correct 1 ms 344 KB n=989
18 Correct 1 ms 348 KB n=563
19 Correct 1 ms 348 KB n=592
20 Correct 1 ms 348 KB n=938
21 Correct 1 ms 348 KB n=747
22 Correct 1 ms 348 KB n=991
# Verdict Execution time Memory Grader output
1 Correct 363 ms 95684 KB n=1000000
2 Correct 221 ms 58032 KB n=666666
3 Correct 127 ms 32224 KB n=400000
4 Correct 84 ms 23984 KB n=285714
5 Correct 5 ms 1504 KB n=20000
6 Correct 51 ms 13652 KB n=181818
7 Correct 3 ms 992 KB n=10000
8 Correct 2 ms 860 KB n=6666
9 Correct 1 ms 604 KB n=4000
10 Correct 3 ms 860 KB n=2857
11 Correct 1 ms 604 KB n=2000
# Verdict Execution time Memory 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 1 ms 348 KB n=4
5 Correct 0 ms 348 KB n=4
6 Correct 0 ms 348 KB n=2
7 Correct 1 ms 344 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 19 ms 6496 KB n=50000
12 Correct 15 ms 6064 KB n=50000
13 Correct 0 ms 348 KB n=10
14 Correct 1 ms 348 KB n=685
15 Correct 0 ms 348 KB n=623
16 Correct 1 ms 344 KB n=973
17 Correct 1 ms 344 KB n=989
18 Correct 1 ms 348 KB n=563
19 Correct 1 ms 348 KB n=592
20 Correct 1 ms 348 KB n=938
21 Correct 1 ms 348 KB n=747
22 Correct 1 ms 348 KB n=991
23 Correct 363 ms 95684 KB n=1000000
24 Correct 221 ms 58032 KB n=666666
25 Correct 127 ms 32224 KB n=400000
26 Correct 84 ms 23984 KB n=285714
27 Correct 5 ms 1504 KB n=20000
28 Correct 51 ms 13652 KB n=181818
29 Correct 3 ms 992 KB n=10000
30 Correct 2 ms 860 KB n=6666
31 Correct 1 ms 604 KB n=4000
32 Correct 3 ms 860 KB n=2857
33 Correct 1 ms 604 KB n=2000
34 Correct 12 ms 4952 KB n=23514
35 Correct 12 ms 4952 KB n=23514
36 Correct 1 ms 348 KB n=940
37 Correct 1 ms 348 KB n=2
38 Correct 34 ms 7792 KB n=100000
39 Correct 30 ms 7964 KB n=100000
40 Correct 0 ms 600 KB n=10
41 Correct 0 ms 348 KB n=100
42 Correct 2 ms 604 KB n=1000
43 Correct 439 ms 111780 KB n=1000000
44 Correct 608 ms 121896 KB n=1000000
45 Correct 400 ms 77228 KB n=666666
46 Correct 243 ms 45224 KB n=400000
47 Correct 8 ms 1628 KB n=2336
48 Correct 443 ms 76616 KB n=285714
49 Correct 394 ms 63184 KB n=181818
50 Correct 20 ms 5580 KB n=40000
51 Correct 10 ms 2012 KB n=20000
52 Correct 6 ms 1248 KB n=10000
53 Correct 41 ms 6988 KB n=6666
54 Correct 3 ms 860 KB n=4000
55 Correct 162 ms 26292 KB n=2857
56 Correct 2 ms 604 KB n=2000