Submission #50858

# Submission time Handle Problem Language Result Execution time Memory
50858 2018-06-13T17:36:02 Z Nicksechko Gift (IZhO18_nicegift) C++14
100 / 100
1307 ms 89064 KB
//Solution by Zhusupov Nurlan
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <cstring>
#include <climits>
#include <string.h>
#include <stdio.h>
#include <assert.h>

using namespace std;

typedef long long LL;
typedef map<string , int> MSI;
typedef vector<int> VI;
typedef pair<LL, int> PII;

#define endl '\n'
#define pb(x) push_back(x)
#define sqr(x) ((x) * (x))
#define F first
#define S second
#define SZ(t) ((int) t.size())
#define len(t) ((int) t.length())
#define base LL(1e9 + 7)
#define fname ""
#define sz 2000 * 1002
#define EPS (1e-8)
#define INF ((int)1e9 + 9)
#define mp make_pair

LL mx, n, k, a[sz];
LL s;
set <PII> cur;
priority_queue <PII> w;
pair <LL, LL> g[sz];
vector <pair<LL, VI> > ans;
PII e[sz];

int main()
{
//    freopen(fname"in", "r", stdin);
//    freopen(fname"out", "w", stdout);

  scanf("%d %d", &n, &k);
  w.push(mp(0, 0));
  for (int i = 1; i <= n; i++)
  {
      scanf("%lld", &a[i]);
      s += a[i];
      mx = max(mx, a[i]);
      PII buf = mp(a[i], i);
      if (cur.size() < k || (*cur.begin()) < buf) {
        if (cur.size() == k) {
          w.push(*cur.begin());
          cur.erase(*cur.begin());
        }
        cur.insert(buf);
      } else {
        w.push(buf);
      }
  }
  if (s % k != 0 || mx > s / k) {
    cout << -1;
    return 0;
  }

  vector <int> p;
  for (int i = 0; i < k; i++) p.pb(0);

  int vv = 0;
  while (1) {
    PII l = *cur.begin(), r = w.top(), ma = *cur.rbegin();
    LL diff = min(l.F, max(s/k - r.F, (s/k - ma.F)/2));
    s -= diff * k;
    auto it = cur.begin();
    for (int i = 0; i < k; i++, it++) {
      p[i] = (*it).S;
    }
    ans.pb(mp(diff, p));
    /*
    for (auto x : cur) {
      cerr << x.F << " " << x.S << endl;
    }
    cerr << "&" << endl;
    for (auto x : w) {
      cerr << x.F << " " << x.S << endl;
    }*/
    if (s == 0) break;
    for (int i = 0; i < k; i++) {
      PII l = *cur.rbegin(), r = w.top();
      cur.erase(l);
      l.F -= diff;
      if (l > r) {
        e[i] = l;
      } else {
        e[i] = r;
        w.pop();
        if (l.F > 0)
          w.push(l);
      }
    }
    for (int i = 0; i < k; i++) {
      cur.insert(e[i]);
    }
    //cerr << "$" << endl;
  }
  printf("%d\n", ans.size());
  for (int i = 0; i < ans.size(); i++) {
    printf("%lld ", ans[i].F);
    for (int j = 0; j < ans[i].S.size(); j++)
      printf("%d ", ans[i].S[j]);
    puts("");
  }
}

Compilation message

nicegift.cpp: In function 'int main()':
nicegift.cpp:60:24: warning: format '%d' expects argument of type 'int*', but argument 2 has type 'LL* {aka long long int*}' [-Wformat=]
   scanf("%d %d", &n, &k);
                  ~~    ^
nicegift.cpp:60:24: warning: format '%d' expects argument of type 'int*', but argument 3 has type 'LL* {aka long long int*}' [-Wformat=]
nicegift.cpp:68:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
       if (cur.size() < k || (*cur.begin()) < buf) {
           ~~~~~~~~~~~^~~
nicegift.cpp:69:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         if (cur.size() == k) {
             ~~~~~~~~~~~^~~~
nicegift.cpp:123:28: warning: format '%d' expects argument of type 'int', but argument 2 has type 'std::vector<std::pair<long long int, std::vector<int> > >::size_type {aka long unsigned int}' [-Wformat=]
   printf("%d\n", ans.size());
                  ~~~~~~~~~~^
nicegift.cpp:124:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 0; i < ans.size(); i++) {
                   ~~^~~~~~~~~~~~
nicegift.cpp:126:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int j = 0; j < ans[i].S.size(); j++)
                     ~~^~~~~~~~~~~~~~~~~
nicegift.cpp:86:7: warning: unused variable 'vv' [-Wunused-variable]
   int vv = 0;
       ^~
nicegift.cpp:60:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &n, &k);
   ~~~~~^~~~~~~~~~~~~~~~~
nicegift.cpp:64:12: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
       scanf("%lld", &a[i]);
       ~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB n=4
2 Correct 2 ms 460 KB n=3
3 Correct 2 ms 460 KB n=3
4 Correct 3 ms 476 KB n=4
5 Correct 3 ms 476 KB n=4
6 Correct 3 ms 596 KB n=2
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB n=4
2 Correct 2 ms 460 KB n=3
3 Correct 2 ms 460 KB n=3
4 Correct 3 ms 476 KB n=4
5 Correct 3 ms 476 KB n=4
6 Correct 3 ms 596 KB n=2
7 Correct 2 ms 596 KB n=5
8 Correct 2 ms 596 KB n=8
9 Correct 2 ms 596 KB n=14
10 Correct 2 ms 596 KB n=11
11 Correct 36 ms 4192 KB n=50000
12 Correct 30 ms 4192 KB n=50000
13 Correct 2 ms 4192 KB n=10
14 Correct 3 ms 4192 KB n=685
15 Correct 3 ms 4192 KB n=623
16 Correct 3 ms 4192 KB n=973
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB n=4
2 Correct 2 ms 460 KB n=3
3 Correct 2 ms 460 KB n=3
4 Correct 3 ms 476 KB n=4
5 Correct 3 ms 476 KB n=4
6 Correct 3 ms 596 KB n=2
7 Correct 2 ms 596 KB n=5
8 Correct 2 ms 596 KB n=8
9 Correct 2 ms 596 KB n=14
10 Correct 2 ms 596 KB n=11
11 Correct 36 ms 4192 KB n=50000
12 Correct 30 ms 4192 KB n=50000
13 Correct 2 ms 4192 KB n=10
14 Correct 3 ms 4192 KB n=685
15 Correct 3 ms 4192 KB n=623
16 Correct 3 ms 4192 KB n=973
17 Correct 4 ms 4192 KB n=989
18 Correct 3 ms 4192 KB n=563
19 Correct 4 ms 4192 KB n=592
20 Correct 4 ms 4192 KB n=938
21 Correct 3 ms 4192 KB n=747
22 Correct 3 ms 4192 KB n=991
# Verdict Execution time Memory Grader output
1 Correct 887 ms 70224 KB n=1000000
2 Correct 598 ms 70224 KB n=666666
3 Correct 363 ms 70224 KB n=400000
4 Correct 224 ms 70224 KB n=285714
5 Correct 21 ms 70224 KB n=20000
6 Correct 138 ms 70224 KB n=181818
7 Correct 17 ms 70224 KB n=10000
8 Correct 9 ms 70224 KB n=6666
9 Correct 8 ms 70224 KB n=4000
10 Correct 10 ms 70224 KB n=2857
11 Correct 4 ms 70224 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB n=4
2 Correct 2 ms 460 KB n=3
3 Correct 2 ms 460 KB n=3
4 Correct 3 ms 476 KB n=4
5 Correct 3 ms 476 KB n=4
6 Correct 3 ms 596 KB n=2
7 Correct 2 ms 596 KB n=5
8 Correct 2 ms 596 KB n=8
9 Correct 2 ms 596 KB n=14
10 Correct 2 ms 596 KB n=11
11 Correct 36 ms 4192 KB n=50000
12 Correct 30 ms 4192 KB n=50000
13 Correct 2 ms 4192 KB n=10
14 Correct 3 ms 4192 KB n=685
15 Correct 3 ms 4192 KB n=623
16 Correct 3 ms 4192 KB n=973
17 Correct 4 ms 4192 KB n=989
18 Correct 3 ms 4192 KB n=563
19 Correct 4 ms 4192 KB n=592
20 Correct 4 ms 4192 KB n=938
21 Correct 3 ms 4192 KB n=747
22 Correct 3 ms 4192 KB n=991
23 Correct 887 ms 70224 KB n=1000000
24 Correct 598 ms 70224 KB n=666666
25 Correct 363 ms 70224 KB n=400000
26 Correct 224 ms 70224 KB n=285714
27 Correct 21 ms 70224 KB n=20000
28 Correct 138 ms 70224 KB n=181818
29 Correct 17 ms 70224 KB n=10000
30 Correct 9 ms 70224 KB n=6666
31 Correct 8 ms 70224 KB n=4000
32 Correct 10 ms 70224 KB n=2857
33 Correct 4 ms 70224 KB n=2000
34 Correct 26 ms 70224 KB n=23514
35 Correct 23 ms 70224 KB n=23514
36 Correct 3 ms 70224 KB n=940
37 Correct 3 ms 70224 KB n=2
38 Correct 67 ms 70224 KB n=100000
39 Correct 69 ms 70224 KB n=100000
40 Correct 2 ms 70224 KB n=10
41 Correct 3 ms 70224 KB n=100
42 Correct 6 ms 70224 KB n=1000
43 Correct 1083 ms 85920 KB n=1000000
44 Correct 1307 ms 89064 KB n=1000000
45 Correct 978 ms 89064 KB n=666666
46 Correct 526 ms 89064 KB n=400000
47 Correct 20 ms 89064 KB n=2336
48 Correct 711 ms 89064 KB n=285714
49 Correct 614 ms 89064 KB n=181818
50 Correct 51 ms 89064 KB n=40000
51 Correct 42 ms 89064 KB n=20000
52 Correct 18 ms 89064 KB n=10000
53 Correct 81 ms 89064 KB n=6666
54 Correct 11 ms 89064 KB n=4000
55 Correct 451 ms 89064 KB n=2857
56 Correct 10 ms 89064 KB n=2000