Submission #287123

# Submission time Handle Problem Language Result Execution time Memory
287123 2020-08-31T12:18:06 Z 김동현(#5795) JOIRIS (JOI16_joiris) C++17
100 / 100
13 ms 512 KB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;

using vint = vector<int>;
using vll = vector<ll>;
using vld = vector<ld>;
using vpii = vector<pii>;
using vpil = vector<pil>;
using vpli = vector<pli>;
using vpll = vector<pll>;

#define x first
#define y second
#define all(v) v.begin(),v.end()

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n, k;
  cin >> n >> k;

  vint a(n), b(k);
  for(int i = 0; i < n; i++) {
    cin >> a[i];
    b[i % k] = (b[i % k] + a[i]) % k;
  }

  int valid = 1;
  for(int i = 1; i < n % k; i++) if(b[0] != b[i]) valid = 0;
  for(int i = n % k + 1; i < k; i++) if(b[n % k] != b[i]) valid = 0;
  if(!valid) {
    cout << "-1\n";
    return 0;
  }

  struct Tet {
    int n, k;
    vector<vint> b;
    vpii ans;
    void init(const vint &a, int k_) {
      n = a.size();
      k = k_;
      b.resize(n + 1);
      for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= a[i - 1]; j++) b[i].push_back(j);
      }
    }
    
    int h(int x) {
      return (b[x].empty() ? 0 : b[x].back());
    }

    void rmrow(int h) {
      for(int i = 1; i <= n; i++) {
        if(!binary_search(all(b[i]), h)) return;
      }
      for(int i = 1; i <= n; i++) {
        b[i].erase(find(all(b[i]), h));
        for(int &x : b[i]) if(x > h) x--;
      }
    }

    void put(int dir, int x) {
      if(dir == 1) {
        for(int i = 0; i < k; i++) {
          b[x].push_back(h(x) + 1);
          rmrow(h(x));
        }
      }
      else {
        int maxh = 0;
        for(int i = x; i < x + k; i++) maxh = max(maxh, h(i));
        for(int i = x; i < x + k; i++) b[i].push_back(maxh + 1);
        rmrow(maxh + 1);
      }
      ans.emplace_back(dir, x);
    }
  } T;

  T.init(a, k);
  for(int i = 2; i <= n; i++) {
    while(T.h(i - 1) > T.h(i)) T.put(1, i);
  }
  for(int i = k; i < n; i++) {
    while(T.h(i) < T.h(i + 1)) {
      for(int j = i - k + 1; j >= 1; j -= k) T.put(2, j);
    }
  }
  for(int i = 1; i < k; i++) {
    while(T.h(i) < T.h(n)) T.put(1, i);
  }
  if(n % k < k - 1) {
    while(T.h(k - 1)) {
      for(int i = k; i <= n; i++) T.put(1, i);
    }
  }
  while(T.h(1)) {
    for(int i = n % k + 1; i <= n; i += k) T.put(2, i);
  }

  cout << T.ans.size() << '\n';
  for(pii &p : T.ans) cout << p.x << ' ' << p.y << '\n';
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 2 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 10 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 2 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 1 ms 384 KB Output is correct
31 Correct 0 ms 384 KB Output is correct
32 Correct 0 ms 384 KB Output is correct
33 Correct 1 ms 384 KB Output is correct
34 Correct 0 ms 384 KB Output is correct
35 Correct 1 ms 384 KB Output is correct
36 Correct 0 ms 384 KB Output is correct
37 Correct 1 ms 384 KB Output is correct
38 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 0 ms 384 KB Output is correct
30 Correct 1 ms 384 KB Output is correct
31 Correct 1 ms 384 KB Output is correct
32 Correct 1 ms 384 KB Output is correct
33 Correct 1 ms 384 KB Output is correct
34 Correct 1 ms 384 KB Output is correct
35 Correct 1 ms 384 KB Output is correct
36 Correct 1 ms 384 KB Output is correct
37 Correct 1 ms 384 KB Output is correct
38 Correct 1 ms 384 KB Output is correct
39 Correct 2 ms 384 KB Output is correct
40 Correct 1 ms 384 KB Output is correct
41 Correct 10 ms 384 KB Output is correct
42 Correct 1 ms 384 KB Output is correct
43 Correct 2 ms 384 KB Output is correct
44 Correct 1 ms 384 KB Output is correct
45 Correct 1 ms 384 KB Output is correct
46 Correct 1 ms 384 KB Output is correct
47 Correct 0 ms 384 KB Output is correct
48 Correct 0 ms 384 KB Output is correct
49 Correct 1 ms 384 KB Output is correct
50 Correct 0 ms 384 KB Output is correct
51 Correct 1 ms 384 KB Output is correct
52 Correct 0 ms 384 KB Output is correct
53 Correct 1 ms 384 KB Output is correct
54 Correct 1 ms 384 KB Output is correct
55 Correct 0 ms 384 KB Output is correct
56 Correct 1 ms 384 KB Output is correct
57 Correct 1 ms 384 KB Output is correct
58 Correct 0 ms 384 KB Output is correct
59 Correct 0 ms 384 KB Output is correct
60 Correct 1 ms 384 KB Output is correct
61 Correct 1 ms 384 KB Output is correct
62 Correct 1 ms 384 KB Output is correct
63 Correct 1 ms 384 KB Output is correct
64 Correct 1 ms 384 KB Output is correct
65 Correct 1 ms 384 KB Output is correct
66 Correct 1 ms 384 KB Output is correct
67 Correct 1 ms 384 KB Output is correct
68 Correct 1 ms 384 KB Output is correct
69 Correct 1 ms 384 KB Output is correct
70 Correct 1 ms 384 KB Output is correct
71 Correct 4 ms 512 KB Output is correct
72 Correct 8 ms 512 KB Output is correct
73 Correct 7 ms 512 KB Output is correct
74 Correct 2 ms 384 KB Output is correct
75 Correct 1 ms 384 KB Output is correct
76 Correct 2 ms 384 KB Output is correct
77 Correct 0 ms 384 KB Output is correct
78 Correct 1 ms 384 KB Output is correct
79 Correct 1 ms 384 KB Output is correct
80 Correct 0 ms 384 KB Output is correct
81 Correct 13 ms 512 KB Output is correct
82 Correct 1 ms 288 KB Output is correct
83 Correct 0 ms 384 KB Output is correct
84 Correct 0 ms 384 KB Output is correct
85 Correct 0 ms 384 KB Output is correct
86 Correct 1 ms 384 KB Output is correct
87 Correct 1 ms 288 KB Output is correct
88 Correct 1 ms 384 KB Output is correct
89 Correct 0 ms 384 KB Output is correct
90 Correct 1 ms 384 KB Output is correct
91 Correct 0 ms 384 KB Output is correct
92 Correct 1 ms 384 KB Output is correct