# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1253980 | hoa208 | Jakarta Skyscrapers (APIO15_skyscraper) | C++20 | 64 ms | 24512 KiB |
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define FORD(i, b, a) for (int i = (b), _a = (a); i >= _a; i--)
#define pa pair<ll, ll>
#define fi first
#define se second
#define bit(mask, j) ((mask >> j) & 1)
#define t_test int t;cin >> t;while(t--)
const ll mod = 1e9 + 7;
const ll INF = 1e18;
inline void adm(ll &x){if(x>=mod)x%=mod;else if(x<0)x+=mod;}
//--------------------------------------------------------------------
struct state {
int w, u;
bool operator < (const state &a) const {
return w > a.w;
}
};
priority_queue<state> q;
const int BLOCK = 177;
const int N = 3e4 + 10;
vector<int> g[N];
int n, m;
int dp[N], mark[N][BLOCK + 2];
void hbmt() {
cin >> n >> m;
int doge1 = 0;
memset(dp, 0x3f, sizeof dp);
FOR(i, 1, m) {
int b, p;
cin >> b >> p;
if(i == 1) {
dp[b] = 0;
q.push({0, b});
}
if(i == 2) {
doge1 = b;
}
g[b].push_back(p);
}
while(!q.empty()) {
int u = q.top().u, du = q.top().w;
q.pop();
if(du != dp[u]) continue;
if(u == doge1) {
cout << du;
exit(0);
}
for(auto p : g[u]) {
int c = 0;
for(int v = u + p; v < n; v += p) {
c++;
if(dp[v] > dp[u] + c) {
dp[v] = dp[u] + c;
q.push({dp[v], v});
}
if(p <= BLOCK && mark[v][p]) break;
if(p <= BLOCK) {
mark[v][p] = true;
}
}
c = 0;
for(int v = u - p; v >= 0; v -= p) {
c++;
if(dp[v] > dp[u] + c) {
dp[v] = dp[u] + c;
q.push({dp[v], v});
}
if(p <= BLOCK && mark[v][p]) break;
if(p <= BLOCK) {
mark[v][p] = true;
}
}
}
}
cout << -1;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
if(fopen("hbmt.inp", "r")) {
freopen("hbmt.inp", "r", stdin);
freopen("hbmt.out", "w", stdout);
}
// t_test
hbmt();
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |