Submission #50860

# Submission time Handle Problem Language Result Execution time Memory
50860 2018-06-13T17:53:56 Z Nicksechko Gift (IZhO18_nicegift) C++14
49 / 100
2000 ms 109420 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<int, 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 <pair <LL, LL> > b;
pair <LL, LL> g[sz];

vector <pair<LL, VI> > ans;
int main()
{
//    freopen(fname"in", "r", stdin);
//    freopen(fname"out", "w", stdout);

  ios_base::sync_with_stdio(false);
  cin.tie(0);

  cin >> n >> k;
  for (int i = 1; i <= n; i++)
  {
      cin >> a[i];
      s += a[i];
      mx = max(mx, a[i]);
  }

  if (s % k != 0 || mx > s / k) {
    cout << -1;
    return 0;
  }

  for (int q = 1; q <= 10; q++) {
    ans.clear();
    b.clear();
    for (int i = 1; i <= n; i++) b.insert(mp(a[i], i));
    int cnt;
    LL ss = s;
    while (1) {
      if (b.size() < k) break;
      set <pair<LL, LL> >::iterator it = b.end();
      LL mi = 1e18;
      for (int i = 0; i < k; i++) {
        it--;
        g[i] = *it;
        mi = min(mi, g[i].F);
      }
      if (it != b.begin()) {
        it--;
        mi = min(mi, (ss / k - (*it).F));
      }
      // sum / k >= mx
      for (int i = 0; i < k; i++) b.erase(*b.rbegin());

      VI p;
      for (int i = 0; i < k; i++) {
        g[i].F -= mi;
        ss -= mi;
        if (g[i].F != 0) b.insert(g[i]);
        p.pb(g[i].S);
      }
      ans.pb(mp(mi, p));
    }
    if (b.size() == 0) {
      cout << ans.size() << "\n";
      for (int i = 0; i < ans.size(); i++) {
        cout << ans[i].F << " ";
        for (int j = 0; j < ans[i].S.size(); j++)
            cout << ans[i].S[j] << " ";
        cout << "\n";
      }
      return 0;
    }
  }

}

Compilation message

nicegift.cpp: In function 'int main()':
nicegift.cpp:81:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
       if (b.size() < k) break;
           ~~~~~~~~~^~~
nicegift.cpp:107:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
       for (int i = 0; i < ans.size(); i++) {
                       ~~^~~~~~~~~~~~
nicegift.cpp:109:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int j = 0; j < ans[i].S.size(); j++)
                         ~~^~~~~~~~~~~~~~~~~
nicegift.cpp:78:9: warning: unused variable 'cnt' [-Wunused-variable]
     int cnt;
         ^~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB n=4
2 Correct 2 ms 448 KB n=3
3 Correct 2 ms 448 KB n=3
4 Correct 2 ms 484 KB n=4
5 Correct 2 ms 484 KB n=4
6 Correct 2 ms 484 KB n=2
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB n=4
2 Correct 2 ms 448 KB n=3
3 Correct 2 ms 448 KB n=3
4 Correct 2 ms 484 KB n=4
5 Correct 2 ms 484 KB n=4
6 Correct 2 ms 484 KB n=2
7 Correct 3 ms 544 KB n=5
8 Correct 3 ms 592 KB n=8
9 Correct 3 ms 620 KB n=14
10 Correct 2 ms 620 KB n=11
11 Correct 56 ms 5572 KB n=50000
12 Correct 49 ms 5572 KB n=50000
13 Correct 2 ms 5572 KB n=10
14 Correct 3 ms 5572 KB n=685
15 Correct 3 ms 5572 KB n=623
16 Correct 4 ms 5572 KB n=973
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB n=4
2 Correct 2 ms 448 KB n=3
3 Correct 2 ms 448 KB n=3
4 Correct 2 ms 484 KB n=4
5 Correct 2 ms 484 KB n=4
6 Correct 2 ms 484 KB n=2
7 Correct 3 ms 544 KB n=5
8 Correct 3 ms 592 KB n=8
9 Correct 3 ms 620 KB n=14
10 Correct 2 ms 620 KB n=11
11 Correct 56 ms 5572 KB n=50000
12 Correct 49 ms 5572 KB n=50000
13 Correct 2 ms 5572 KB n=10
14 Correct 3 ms 5572 KB n=685
15 Correct 3 ms 5572 KB n=623
16 Correct 4 ms 5572 KB n=973
17 Correct 3 ms 5572 KB n=989
18 Correct 3 ms 5572 KB n=563
19 Correct 3 ms 5572 KB n=592
20 Correct 4 ms 5572 KB n=938
21 Correct 2 ms 5572 KB n=747
22 Correct 3 ms 5572 KB n=991
# Verdict Execution time Memory Grader output
1 Correct 1280 ms 101016 KB n=1000000
2 Correct 750 ms 101016 KB n=666666
3 Correct 381 ms 101016 KB n=400000
4 Correct 316 ms 101016 KB n=285714
5 Correct 24 ms 101016 KB n=20000
6 Correct 203 ms 101016 KB n=181818
7 Correct 8 ms 101016 KB n=10000
8 Correct 7 ms 101016 KB n=6666
9 Correct 4 ms 101016 KB n=4000
10 Correct 8 ms 101016 KB n=2857
11 Correct 3 ms 101016 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB n=4
2 Correct 2 ms 448 KB n=3
3 Correct 2 ms 448 KB n=3
4 Correct 2 ms 484 KB n=4
5 Correct 2 ms 484 KB n=4
6 Correct 2 ms 484 KB n=2
7 Correct 3 ms 544 KB n=5
8 Correct 3 ms 592 KB n=8
9 Correct 3 ms 620 KB n=14
10 Correct 2 ms 620 KB n=11
11 Correct 56 ms 5572 KB n=50000
12 Correct 49 ms 5572 KB n=50000
13 Correct 2 ms 5572 KB n=10
14 Correct 3 ms 5572 KB n=685
15 Correct 3 ms 5572 KB n=623
16 Correct 4 ms 5572 KB n=973
17 Correct 3 ms 5572 KB n=989
18 Correct 3 ms 5572 KB n=563
19 Correct 3 ms 5572 KB n=592
20 Correct 4 ms 5572 KB n=938
21 Correct 2 ms 5572 KB n=747
22 Correct 3 ms 5572 KB n=991
23 Correct 1280 ms 101016 KB n=1000000
24 Correct 750 ms 101016 KB n=666666
25 Correct 381 ms 101016 KB n=400000
26 Correct 316 ms 101016 KB n=285714
27 Correct 24 ms 101016 KB n=20000
28 Correct 203 ms 101016 KB n=181818
29 Correct 8 ms 101016 KB n=10000
30 Correct 7 ms 101016 KB n=6666
31 Correct 4 ms 101016 KB n=4000
32 Correct 8 ms 101016 KB n=2857
33 Correct 3 ms 101016 KB n=2000
34 Correct 34 ms 101016 KB n=23514
35 Correct 33 ms 101016 KB n=23514
36 Correct 3 ms 101016 KB n=940
37 Correct 2 ms 101016 KB n=2
38 Correct 94 ms 101016 KB n=100000
39 Correct 98 ms 101016 KB n=100000
40 Correct 2 ms 101016 KB n=10
41 Correct 3 ms 101016 KB n=100
42 Correct 7 ms 101016 KB n=1000
43 Correct 1725 ms 109420 KB n=1000000
44 Execution timed out 2068 ms 109420 KB Time limit exceeded
45 Halted 0 ms 0 KB -