Submission #293423

# Submission time Handle Problem Language Result Execution time Memory
293423 2020-09-08T04:57:08 Z rama_pang Painting Walls (APIO20_paint) C++14
0 / 100
5 ms 5248 KB
#include "paint.h"
#include <bits/stdc++.h>
using namespace std;

int minimumInstructions(int N, int M, int K, vector<int> C, vector<int> A, vector<vector<int>> B) {
  vector<vector<int>> colors(K);
  for (int i = 0; i < M; i++) {
    for (auto c : B[i]) {
      colors[c].emplace_back(i);
    }
  }

  int mx = 0;
  vector<bool> possible(N + 1, false);
  auto Update = [&](int i, int sgn) {
    static vector<int> freq(M, 0);
    static vector<int> cnt(M + 1, 0);
    auto UpdateMax = [&]() {
      while (mx + 1 <= M && cnt[mx + 1] > 0) mx++;
      while (mx > 0 && cnt[mx] == 0) mx--;
    };
    for (auto j : colors[C[i]]) {
      int v = j - (i % M);
      if (v < 0) v += M;
      if (sgn == 1) {
        freq[v] += sgn;
        cnt[freq[v]] += sgn;
      } else if (sgn == -1) {
        cnt[freq[v]] += sgn;
        freq[v] += sgn;
      }
      UpdateMax();
    }
  };

  for (int i = 0; i < M; i++) {
    Update(i, +1);
  }
  if (mx == M) {
    possible[0] = true;
  }
  for (int i = 0; i + M < N; i++) {
    Update(i, -1);
    Update(i + M, +1);
    if (mx == M) {
      possible[i + 1] = true;
    }
  }

  deque<pair<int, int>> q;
  vector<int> dist(N + 1, -1);
  q.emplace_front(0, 0);
  dist[0] = 0;
  while (!q.empty()) {
    int u = q.front().first;
    int d = q.front().second;
    q.pop_front();
    if (dist[u] != d) {
      continue;
    }
    if (possible[u] && dist[u + M] == -1 || dist[u + M] > dist[u] + 1) {
      dist[u + M] = dist[u] + 1;
      q.emplace_back(u + M, dist[u + M]);
    }
    if (u > 0 && (dist[u - 1] == -1 || dist[u - 1] > dist[u])) {
      dist[u - 1] = dist[u];
      q.emplace_front(u - 1, dist[u - 1]);
    }
  }
  return dist[N];
}

Compilation message

paint.cpp: In function 'int minimumInstructions(int, int, int, std::vector<int>, std::vector<int>, std::vector<std::vector<int> >)':
paint.cpp:61:21: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   61 |     if (possible[u] && dist[u + M] == -1 || dist[u + M] > dist[u] + 1) {
      |         ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 0 ms 256 KB Output is correct
7 Correct 1 ms 256 KB Output is correct
8 Correct 1 ms 256 KB Output is correct
9 Correct 0 ms 256 KB Output is correct
10 Correct 1 ms 256 KB Output is correct
11 Correct 0 ms 256 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Runtime error 5 ms 5248 KB Execution killed with signal 11
14 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 3 ms 640 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 3 ms 640 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 3 ms 640 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 3 ms 640 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -