답안 #395273

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
395273 2021-04-28T04:47:49 Z rama_pang Gift (IZhO18_nicegift) C++17
100 / 100
683 ms 124628 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 208 KB n=4
2 Correct 1 ms 316 KB n=3
3 Correct 1 ms 204 KB n=3
4 Correct 1 ms 204 KB n=4
5 Correct 1 ms 204 KB n=4
6 Correct 1 ms 204 KB n=2
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 208 KB n=4
2 Correct 1 ms 316 KB n=3
3 Correct 1 ms 204 KB n=3
4 Correct 1 ms 204 KB n=4
5 Correct 1 ms 204 KB n=4
6 Correct 1 ms 204 KB n=2
7 Correct 1 ms 204 KB n=5
8 Correct 1 ms 204 KB n=8
9 Correct 1 ms 204 KB n=14
10 Correct 1 ms 204 KB n=11
11 Correct 27 ms 4936 KB n=50000
12 Correct 22 ms 4684 KB n=50000
13 Correct 1 ms 204 KB n=10
14 Correct 1 ms 332 KB n=685
15 Correct 1 ms 316 KB n=623
16 Correct 1 ms 336 KB n=973
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 208 KB n=4
2 Correct 1 ms 316 KB n=3
3 Correct 1 ms 204 KB n=3
4 Correct 1 ms 204 KB n=4
5 Correct 1 ms 204 KB n=4
6 Correct 1 ms 204 KB n=2
7 Correct 1 ms 204 KB n=5
8 Correct 1 ms 204 KB n=8
9 Correct 1 ms 204 KB n=14
10 Correct 1 ms 204 KB n=11
11 Correct 27 ms 4936 KB n=50000
12 Correct 22 ms 4684 KB n=50000
13 Correct 1 ms 204 KB n=10
14 Correct 1 ms 332 KB n=685
15 Correct 1 ms 316 KB n=623
16 Correct 1 ms 336 KB n=973
17 Correct 1 ms 332 KB n=989
18 Correct 1 ms 332 KB n=563
19 Correct 2 ms 424 KB n=592
20 Correct 2 ms 440 KB n=938
21 Correct 2 ms 460 KB n=747
22 Correct 2 ms 500 KB n=991
# 결과 실행 시간 메모리 Grader output
1 Correct 415 ms 82684 KB n=1000000
2 Correct 252 ms 46720 KB n=666666
3 Correct 141 ms 25312 KB n=400000
4 Correct 378 ms 50288 KB n=285714
5 Correct 8 ms 1384 KB n=20000
6 Correct 349 ms 42188 KB n=181818
7 Correct 5 ms 844 KB n=10000
8 Correct 37 ms 4548 KB n=6666
9 Correct 4 ms 460 KB n=4000
10 Correct 199 ms 21388 KB n=2857
11 Correct 3 ms 464 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 208 KB n=4
2 Correct 1 ms 316 KB n=3
3 Correct 1 ms 204 KB n=3
4 Correct 1 ms 204 KB n=4
5 Correct 1 ms 204 KB n=4
6 Correct 1 ms 204 KB n=2
7 Correct 1 ms 204 KB n=5
8 Correct 1 ms 204 KB n=8
9 Correct 1 ms 204 KB n=14
10 Correct 1 ms 204 KB n=11
11 Correct 27 ms 4936 KB n=50000
12 Correct 22 ms 4684 KB n=50000
13 Correct 1 ms 204 KB n=10
14 Correct 1 ms 332 KB n=685
15 Correct 1 ms 316 KB n=623
16 Correct 1 ms 336 KB n=973
17 Correct 1 ms 332 KB n=989
18 Correct 1 ms 332 KB n=563
19 Correct 2 ms 424 KB n=592
20 Correct 2 ms 440 KB n=938
21 Correct 2 ms 460 KB n=747
22 Correct 2 ms 500 KB n=991
23 Correct 415 ms 82684 KB n=1000000
24 Correct 252 ms 46720 KB n=666666
25 Correct 141 ms 25312 KB n=400000
26 Correct 378 ms 50288 KB n=285714
27 Correct 8 ms 1384 KB n=20000
28 Correct 349 ms 42188 KB n=181818
29 Correct 5 ms 844 KB n=10000
30 Correct 37 ms 4548 KB n=6666
31 Correct 4 ms 460 KB n=4000
32 Correct 199 ms 21388 KB n=2857
33 Correct 3 ms 464 KB n=2000
34 Correct 16 ms 2844 KB n=23514
35 Correct 15 ms 2796 KB n=23514
36 Correct 1 ms 320 KB n=940
37 Correct 1 ms 204 KB n=2
38 Correct 105 ms 13988 KB n=100000
39 Correct 91 ms 14008 KB n=100000
40 Correct 1 ms 204 KB n=10
41 Correct 1 ms 332 KB n=100
42 Correct 7 ms 844 KB n=1000
43 Correct 683 ms 113972 KB n=1000000
44 Correct 680 ms 124628 KB n=1000000
45 Correct 571 ms 87576 KB n=666666
46 Correct 434 ms 63872 KB n=400000
47 Correct 192 ms 18976 KB n=2336
48 Correct 388 ms 50412 KB n=285714
49 Correct 344 ms 42408 KB n=181818
50 Correct 280 ms 25576 KB n=40000
51 Correct 212 ms 23172 KB n=20000
52 Correct 199 ms 21124 KB n=10000
53 Correct 262 ms 24072 KB n=6666
54 Correct 206 ms 18112 KB n=4000
55 Correct 245 ms 21368 KB n=2857
56 Correct 190 ms 17092 KB n=2000