답안 #287802

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
287802 2020-09-01T01:17:47 Z rama_pang Gift (IZhO18_nicegift) C++14
100 / 100
856 ms 124564 KB
#include <bits/stdc++.h>
using namespace std;
using lint = long long;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  int N, K;
  cin >> N >> K;

  lint total = 0;
  vector<lint> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    total += A[i];
  }

  if (total % K != 0 || total / K < *max_element(begin(A), end(A))) {
    cout << -1 << "\n";
    return 0;
  }

  lint limit = total / K;
  vector<lint> sum(K);
  vector<vector<pair<lint, int>>> data(K);

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < K && A[i] > 0; j++) {
      if (sum[j] + A[i] <= limit) {
        sum[j] += A[i];
        data[j].emplace_back(A[i], i);
        A[i] = 0;
      } else if (sum[j] < limit) {
        A[i] -= limit - sum[j];
        data[j].emplace_back(limit - sum[j], i);
        sum[j] = limit;
      }
    }
    assert(A[i] == 0);
  }

  vector<pair<lint, vector<int>>> ans;
  while (!data[0].empty()) {
    lint minim = 1e18;
    for (int i = 0; i < K; i++) {
      minim = min(minim, data[i].back().first);
    }
    ans.emplace_back();
    ans.back().first = minim;
    for (int i = 0; i < K; i++) {
      ans.back().second.emplace_back(data[i].back().second);
      data[i].back().first -= minim;
      if (data[i].back().first == 0) {
        data[i].pop_back();
      }
    }
  }

  assert(ans.size() <= N);
  cout << ans.size() << "\n";
  for (auto i : ans) {
    cout << i.first;
    for (auto j : i.second) {
      cout << " " << j + 1;
    }
    cout << "\n";
  }
  return 0;  
}

Compilation message

In file included from /usr/include/c++/9/cassert:44,
                 from /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:33,
                 from nicegift.cpp:1:
nicegift.cpp: In function 'int main()':
nicegift.cpp:60:21: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<long long int, std::vector<int> > >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   60 |   assert(ans.size() <= N);
      |          ~~~~~~~~~~~^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB n=4
2 Correct 1 ms 384 KB n=3
3 Correct 1 ms 384 KB n=3
4 Correct 1 ms 384 KB n=4
5 Correct 1 ms 384 KB n=4
6 Correct 0 ms 384 KB n=2
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB n=4
2 Correct 1 ms 384 KB n=3
3 Correct 1 ms 384 KB n=3
4 Correct 1 ms 384 KB n=4
5 Correct 1 ms 384 KB n=4
6 Correct 0 ms 384 KB n=2
7 Correct 0 ms 384 KB n=5
8 Correct 0 ms 384 KB n=8
9 Correct 1 ms 384 KB n=14
10 Correct 1 ms 384 KB n=11
11 Correct 49 ms 4976 KB n=50000
12 Correct 24 ms 4728 KB n=50000
13 Correct 1 ms 384 KB n=10
14 Correct 1 ms 384 KB n=685
15 Correct 1 ms 384 KB n=623
16 Correct 2 ms 384 KB n=973
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB n=4
2 Correct 1 ms 384 KB n=3
3 Correct 1 ms 384 KB n=3
4 Correct 1 ms 384 KB n=4
5 Correct 1 ms 384 KB n=4
6 Correct 0 ms 384 KB n=2
7 Correct 0 ms 384 KB n=5
8 Correct 0 ms 384 KB n=8
9 Correct 1 ms 384 KB n=14
10 Correct 1 ms 384 KB n=11
11 Correct 49 ms 4976 KB n=50000
12 Correct 24 ms 4728 KB n=50000
13 Correct 1 ms 384 KB n=10
14 Correct 1 ms 384 KB n=685
15 Correct 1 ms 384 KB n=623
16 Correct 2 ms 384 KB n=973
17 Correct 2 ms 512 KB n=989
18 Correct 2 ms 384 KB n=563
19 Correct 2 ms 512 KB n=592
20 Correct 2 ms 512 KB n=938
21 Correct 2 ms 512 KB n=747
22 Correct 3 ms 512 KB n=991
# 결과 실행 시간 메모리 Grader output
1 Correct 449 ms 82744 KB n=1000000
2 Correct 264 ms 46780 KB n=666666
3 Correct 145 ms 25368 KB n=400000
4 Correct 397 ms 50356 KB n=285714
5 Correct 9 ms 1536 KB n=20000
6 Correct 352 ms 42236 KB n=181818
7 Correct 5 ms 896 KB n=10000
8 Correct 35 ms 4600 KB n=6666
9 Correct 3 ms 512 KB n=4000
10 Correct 200 ms 21496 KB n=2857
11 Correct 3 ms 512 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB n=4
2 Correct 1 ms 384 KB n=3
3 Correct 1 ms 384 KB n=3
4 Correct 1 ms 384 KB n=4
5 Correct 1 ms 384 KB n=4
6 Correct 0 ms 384 KB n=2
7 Correct 0 ms 384 KB n=5
8 Correct 0 ms 384 KB n=8
9 Correct 1 ms 384 KB n=14
10 Correct 1 ms 384 KB n=11
11 Correct 49 ms 4976 KB n=50000
12 Correct 24 ms 4728 KB n=50000
13 Correct 1 ms 384 KB n=10
14 Correct 1 ms 384 KB n=685
15 Correct 1 ms 384 KB n=623
16 Correct 2 ms 384 KB n=973
17 Correct 2 ms 512 KB n=989
18 Correct 2 ms 384 KB n=563
19 Correct 2 ms 512 KB n=592
20 Correct 2 ms 512 KB n=938
21 Correct 2 ms 512 KB n=747
22 Correct 3 ms 512 KB n=991
23 Correct 449 ms 82744 KB n=1000000
24 Correct 264 ms 46780 KB n=666666
25 Correct 145 ms 25368 KB n=400000
26 Correct 397 ms 50356 KB n=285714
27 Correct 9 ms 1536 KB n=20000
28 Correct 352 ms 42236 KB n=181818
29 Correct 5 ms 896 KB n=10000
30 Correct 35 ms 4600 KB n=6666
31 Correct 3 ms 512 KB n=4000
32 Correct 200 ms 21496 KB n=2857
33 Correct 3 ms 512 KB n=2000
34 Correct 25 ms 2932 KB n=23514
35 Correct 25 ms 2924 KB n=23514
36 Correct 1 ms 384 KB n=940
37 Correct 0 ms 384 KB n=2
38 Correct 135 ms 14072 KB n=100000
39 Correct 100 ms 14052 KB n=100000
40 Correct 1 ms 384 KB n=10
41 Correct 2 ms 384 KB n=100
42 Correct 6 ms 896 KB n=1000
43 Correct 706 ms 113828 KB n=1000000
44 Correct 856 ms 124564 KB n=1000000
45 Correct 592 ms 87676 KB n=666666
46 Correct 442 ms 64056 KB n=400000
47 Correct 198 ms 19132 KB n=2336
48 Correct 394 ms 50380 KB n=285714
49 Correct 332 ms 42208 KB n=181818
50 Correct 228 ms 25704 KB n=40000
51 Correct 210 ms 23236 KB n=20000
52 Correct 230 ms 21352 KB n=10000
53 Correct 204 ms 24096 KB n=6666
54 Correct 191 ms 18172 KB n=4000
55 Correct 220 ms 21372 KB n=2857
56 Correct 182 ms 17144 KB n=2000