답안 #346941

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
346941 2021-01-11T11:45:12 Z impetus_ Gift (IZhO18_nicegift) C++14
100 / 100
1399 ms 97172 KB
#include <bits/stdc++.h>
 
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define ll long long
#define int ll
#define forn(i, a, b) for(int i = (a); i <= (b); ++i)
#define forev(i, b, a) for(int i = (b); i >= (a); --i)
#define VAR(v, i) __typeof( i) v=(i)
#define forit(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define file(s) freopen(s".in","r",stdin); freopen(s".out","w",stdout);
 
using namespace std;
 
const int maxn = (int)1e6 + 10;
const int mod = (int)1e9 + 7;
const int P = (int) 1e6 + 7; 
const double pi = acos(-1.0);
 
#define inf mod
                        
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;   
typedef vector<ll> Vll;               
typedef vector<pair<int, int> > vpii;
typedef vector<pair<ll, ll> > vpll;                        
typedef array<int, 3> triple;
 
int n, k, sum, mx;
pii a[maxn];             
priority_queue<pii> s;
  
inline void solve(){
  scanf("%lld%lld", &n, &k);
  
  forn(i, 1, n) {
    scanf("%lld", &a[i].f);
    a[i].s = i;
    sum += a[i].f;
    mx = max(mx, a[i].f);
    s.push(a[i]);
  }
  
  if(sum % k || sum / k < mx){
    puts("-1");
    return;
  }
  vector<pair<int, vi> > path;
  
  while(1){        
    int f = s.top().f, r;
    vi cur;
    vpii toAdd;     
   
    forn(i, 1, k) {
      pii val = s.top();
      toAdd.pb(val);
      cur.pb(val.s);
      r = val.f; 
      s.pop();
    }
    int nmx;
    if(sz(s)){
      nmx = s.top().f;
    }else nmx = 0;
 
    if(!f) break;
    
    int res = min(r, (sum - nmx * k) / k);
    
    /*(sum - mid * k) / k >= nmx
    sum - mid * k >= nmx * k
    sum - nmx * k >= mid * k
    7 >= 2 3
 
    while(l <= r){
      int mid = (l + r) >> 1;
      int curS = sum - mid * k, mx = nmx;
      
      if(curS / k >= mx){
        res = mid;
        l = mid + 1;
      }else
        r = mid - 1;
    }
    assert(res != -1);
    */
 
    for(auto x : toAdd)
      s.push({x.f - res, x.s});
    
      
    sum -= res * k;
    path.pb({res, cur});
 
  
  }
  printf("%lld\n", sz(path));
  for(auto x : path){
    printf("%lld ", x.f);
    for(auto it : x.s) printf("%lld ", it);
    puts("");
  } 
}
main () {     
  srand(time(0));
  int t = 1;
  //scanf("%d", &t);
  while(t--)
    solve();
}           

Compilation message

nicegift.cpp:111:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  111 | main () {
      |       ^
nicegift.cpp: In function 'void solve()':
nicegift.cpp:40:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   40 |   scanf("%lld%lld", &n, &k);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~
nicegift.cpp:43:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   43 |     scanf("%lld", &a[i].f);
      |     ~~~~~^~~~~~~~~~~~~~~~~
nicegift.cpp:57:24: warning: 'r' may be used uninitialized in this function [-Wmaybe-uninitialized]
   57 |     int f = s.top().f, r;
      |                        ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 364 KB n=4
2 Correct 0 ms 364 KB n=3
3 Correct 1 ms 364 KB n=3
4 Correct 0 ms 364 KB n=4
5 Correct 0 ms 364 KB n=4
6 Correct 1 ms 364 KB n=2
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 364 KB n=4
2 Correct 0 ms 364 KB n=3
3 Correct 1 ms 364 KB n=3
4 Correct 0 ms 364 KB n=4
5 Correct 0 ms 364 KB n=4
6 Correct 1 ms 364 KB n=2
7 Correct 0 ms 364 KB n=5
8 Correct 0 ms 364 KB n=8
9 Correct 1 ms 364 KB n=14
10 Correct 0 ms 364 KB n=11
11 Correct 34 ms 4320 KB n=50000
12 Correct 29 ms 4064 KB n=50000
13 Correct 0 ms 364 KB n=10
14 Correct 1 ms 364 KB n=685
15 Correct 1 ms 364 KB n=623
16 Correct 1 ms 364 KB n=973
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 364 KB n=4
2 Correct 0 ms 364 KB n=3
3 Correct 1 ms 364 KB n=3
4 Correct 0 ms 364 KB n=4
5 Correct 0 ms 364 KB n=4
6 Correct 1 ms 364 KB n=2
7 Correct 0 ms 364 KB n=5
8 Correct 0 ms 364 KB n=8
9 Correct 1 ms 364 KB n=14
10 Correct 0 ms 364 KB n=11
11 Correct 34 ms 4320 KB n=50000
12 Correct 29 ms 4064 KB n=50000
13 Correct 0 ms 364 KB n=10
14 Correct 1 ms 364 KB n=685
15 Correct 1 ms 364 KB n=623
16 Correct 1 ms 364 KB n=973
17 Correct 2 ms 364 KB n=989
18 Correct 1 ms 364 KB n=563
19 Correct 2 ms 364 KB n=592
20 Correct 1 ms 364 KB n=938
21 Correct 1 ms 364 KB n=747
22 Correct 1 ms 384 KB n=991
# 결과 실행 시간 메모리 Grader output
1 Correct 769 ms 78260 KB n=1000000
2 Correct 460 ms 44340 KB n=666666
3 Correct 251 ms 24540 KB n=400000
4 Correct 166 ms 16200 KB n=285714
5 Correct 10 ms 1384 KB n=20000
6 Correct 100 ms 10068 KB n=181818
7 Correct 5 ms 876 KB n=10000
8 Correct 4 ms 748 KB n=6666
9 Correct 2 ms 492 KB n=4000
10 Correct 5 ms 620 KB n=2857
11 Correct 1 ms 492 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 364 KB n=4
2 Correct 0 ms 364 KB n=3
3 Correct 1 ms 364 KB n=3
4 Correct 0 ms 364 KB n=4
5 Correct 0 ms 364 KB n=4
6 Correct 1 ms 364 KB n=2
7 Correct 0 ms 364 KB n=5
8 Correct 0 ms 364 KB n=8
9 Correct 1 ms 364 KB n=14
10 Correct 0 ms 364 KB n=11
11 Correct 34 ms 4320 KB n=50000
12 Correct 29 ms 4064 KB n=50000
13 Correct 0 ms 364 KB n=10
14 Correct 1 ms 364 KB n=685
15 Correct 1 ms 364 KB n=623
16 Correct 1 ms 364 KB n=973
17 Correct 2 ms 364 KB n=989
18 Correct 1 ms 364 KB n=563
19 Correct 2 ms 364 KB n=592
20 Correct 1 ms 364 KB n=938
21 Correct 1 ms 364 KB n=747
22 Correct 1 ms 384 KB n=991
23 Correct 769 ms 78260 KB n=1000000
24 Correct 460 ms 44340 KB n=666666
25 Correct 251 ms 24540 KB n=400000
26 Correct 166 ms 16200 KB n=285714
27 Correct 10 ms 1384 KB n=20000
28 Correct 100 ms 10068 KB n=181818
29 Correct 5 ms 876 KB n=10000
30 Correct 4 ms 748 KB n=6666
31 Correct 2 ms 492 KB n=4000
32 Correct 5 ms 620 KB n=2857
33 Correct 1 ms 492 KB n=2000
34 Correct 21 ms 2660 KB n=23514
35 Correct 21 ms 2660 KB n=23514
36 Correct 1 ms 492 KB n=940
37 Correct 1 ms 364 KB n=2
38 Correct 60 ms 6108 KB n=100000
39 Correct 59 ms 6108 KB n=100000
40 Correct 0 ms 364 KB n=10
41 Correct 1 ms 364 KB n=100
42 Correct 3 ms 492 KB n=1000
43 Correct 873 ms 93732 KB n=1000000
44 Correct 1399 ms 97172 KB n=1000000
45 Correct 932 ms 60744 KB n=666666
46 Correct 507 ms 33864 KB n=400000
47 Correct 12 ms 1132 KB n=2336
48 Correct 858 ms 50872 KB n=285714
49 Correct 703 ms 42828 KB n=181818
50 Correct 34 ms 2784 KB n=40000
51 Correct 16 ms 1508 KB n=20000
52 Correct 10 ms 1000 KB n=10000
53 Correct 61 ms 4460 KB n=6666
54 Correct 5 ms 620 KB n=4000
55 Correct 262 ms 16364 KB n=2857
56 Correct 4 ms 620 KB n=2000