Submission #839127

#TimeUsernameProblemLanguageResultExecution timeMemory
839127tch1cherinJakarta Skyscrapers (APIO15_skyscraper)C++17
22 / 100
2 ms2516 KiB
#include <bits/stdc++.h>
using namespace std;

const int MAX_N = 30005;
const int C = 300;
bool used[C][C] = {};
vector<int> steps[MAX_N];
vector<pair<int, int>> G[MAX_N * 2];
int B[MAX_N], P[MAX_N];

int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  int N, M;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    cin >> B[i] >> P[i];
    if (P[i] >= C) {
      for (int j = B[i] % P[i]; j < N; j += P[i]) {
        G[B[i]].push_back({j, abs(B[i] - j) / P[i]});
      }
    } else {
      steps[B[i]].push_back(P[i]);
    }
  }
  vector<int> dist(2 * N, INT_MAX);
  priority_queue<pair<int, int>> Q;
  Q.push({0, B[0]});
  dist[B[0]] = 0;
  while (!Q.empty()) {
    auto [d, u] = Q.top();
    Q.pop();
    if (d > dist[u]) {
      continue;
    }
    if (u < N) {
      for (int step : steps[u]) {
        int rem = u % step;
        if (!used[step][rem]) {
          for (int j = rem; j < N; j += step) {
            int w = abs(u - j) / step;
            if (dist[u] + w < dist[j]) {
              dist[j] = dist[u] + w;
              Q.push({dist[j], j});
            }
          } 
          used[step][rem] = true;
        }
      }
    }
    for (auto [v, w] : G[u]) {
      if (dist[v] > dist[u] + w) {
        dist[v] = dist[u] + w;
        Q.push({dist[v], v});
      }
    }
  }
  cout << (dist[B[1]] == INT_MAX ? -1 : dist[B[1]]) << "\n";
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...