Submission #500169

# Submission time Handle Problem Language Result Execution time Memory
500169 2021-12-30T12:11:47 Z 600Mihnea JOIRIS (JOI16_joiris) C++17
0 / 100
1 ms 332 KB
/**

Enjoy JOI is the new
Enjoy EJOI

**/

#include <bits/stdc++.h>

using namespace std;

const int N = 50 + 7;
int n;
int k;
int a[N];
int s[N];
vector<pair<int, int>> sol;

void gravity() {
  int mn = a[0];
  for (int i = 1; i < n; i++) {
    mn = min(mn, a[i]);
  }
  for (int i = 0; i < n; i++) {
    a[i] -= mn;
  }
}

void op1(int i) {
  sol.push_back({1, i});
  a[i] += k;
  gravity();
}

void op2(int l, int r) {
  assert(r - l + 1 == k);
  sol.push_back({2, l});
  for (int i = l; i <= r; i++) {
    a[i]++;
  }
  gravity();
}

void print() {
  return;
  int hmax = 0;
  for (int i = 0; i < n; i++) {
    hmax = max(hmax, a[i]);
  }
  for (int h = hmax; h >= 1; h--) {
    for (int i = 0; i < n; i++) {
      if (a[i] >= h) {
        cout << "x";
      } else {
        cout << " ";
      }
    }
    cout << "\n";
  }
  cout << "---------------------------------\n";
}

int main() {
  //freopen ("input", "r", stdin);

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

  /**
  N = 5 =>
  a b a b a


  s(j) = sum of a[i] for i mod K = j
  update 1 : s(j) stays constant
  update 2 : relative s(j) stays constant
  delete update :
      relative s(0), s(1)..., s(N mod K - 1) stays constant
      relative s(N mod K), s(N mod K + 1)..., s(K - 1) stays constant

  **/

  if (1) {
    for (int i = 0; i <= n % k - 1; i++) {
      if (s[i] % k != s[0] % k) {
        cout << "-1\n";
        exit(0);
      }
    }
    for (int i = n % k; i < k; i++) {
      if (s[i] % k != s[k - 1] % k) {
        cout << "-1\n";
        exit(0);
      }
    }
  }
  gravity();
  print();

  for (int i = 1; i < n; i++) {
    /// make a[i - 1] <= a[i]
    while (a[i - 1] > a[i]) {
      op1(i);
    }

  }


  for (int i = k - 1; i + 1 < n; i++) {
    /// make a[i] = a[i + 1]

    int steps = a[i + 1] - a[i];

    for (int step = 1; step <= steps; step++) {
      for (int j = i; j - k + 1 >= 0; j -= k) {
        sol.push_back({2, j});
      }

      if (i % k != k - 1) {
        for (int j = 0; j <= i % k; j++) {
          while (a[j] <= a[i]) {
            op1(j);
          }
        }
      }
    }
    if (i % k != k - 1) {
      for (int j = 0; j <= i % k; j++) {
        a[j] -= steps;
      }
    }
    for (int j = i + 1; j < n; j++) {
      a[j] -= steps;
    }

    gravity();
    print();
    assert(a[i] == a[i + 1]);
  }


  for (int i = k - 1; i < n; i++) {
    assert(a[i] == a[k - 1]);
  }

  while (a[k - 1] > 0) {
    for (int i = 0; i <= k - 2; i++) {
      op1(i);
    }
  }
  for (int i = k - 1; i < n; i++) {
    assert(a[i] == 0);
  }

  while (a[n % k] > 0) {
    for (int i = 0; i < n; i++) {
      if (n % k <= i && i < n) {
        continue;
      }
      op1(i);
    }
  }

  for (int i = 0; i < n; i++) {
    assert(a[i] == 0);
  }

  cout << (int) sol.size() << "\n";
  for (auto &it : sol) {
    cout << it.first << " " << it.second << "\n";
  }

  return 0;
}
/**

    x
   xx
zzzxx
  xxx
  xxx
 xxxx
xxxxx

**/
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Incorrect 0 ms 204 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 332 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Incorrect 0 ms 204 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Incorrect 0 ms 204 KB Output isn't correct
5 Halted 0 ms 0 KB -