This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Need to git gud and reach 1900+
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
using namespace std;
#define all(s) s.begin(), s.end()
#define ok puts("ok")
#define ll long long
#define pb push_back
#define mk make_pair
#define fr first
#define sc second
#define vi vector < int >
#define pi pair < int, int >
#define prev prev123
#define next next123
#define pow pow123
#define left left123
#define right right123
const int N = 30007;
const int M = 6e6 + 7;
const int INF = 1e9 + 7;
pi a[N];
int d[M];
int m, n;
vector < pi > g[M];
main(){ // If you don't know what to do, check the text box at the bottom
cin >> m >> n;
for (int i = 1; i <= n; i++){
scanf("%d%d", &a[i].fr, &a[i].sc);
a[i].fr++;
}
sort(a + 1, a + n + 1);
int k = unique(a + 1, a + n + 1) - a;
for (int i = 1; i < k; i++){
if (a[i].sc <= 175)
g[a[i].fr].pb(mk(m + (a[i].fr - 1) * 175 + a[i].sc, 0));
else {
int pos = a[i].fr, pw = a[i].sc, c = 0;
while (pos + pw <= m){
pos += pw; c++;
g[a[i].fr].pb(mk(pos, c));
}
pos = a[i].fr, c = 0;
while (pos - pw > 0){
pos -= pw; c++;
g[a[i].fr].pb(mk(pos, c));
}
}
}
for (int i = 1; i <= m; i++){
for (int j = 1; j <= 175; j++){
if (i + j <= m){
g[m + (i - 1) * 175 + j].pb(mk(m + (i + j - 1) * 175 + j, 1));
g[m + (i - 1) * 175 + j].pb(mk(i + j, 1));
}
if (i - j > 0){
g[m + (i - 1) * 175 + j].pb(mk(m + (i - j - 1) * 175 + j, 1));
g[m + (i - 1) * 175 + j].pb(mk(i - j, 1));
}
}
}
priority_queue < pi > pq;
pq.push(mk(0, a[1].fr));
memset(d, 0x3f3f3f3f, sizeof(d));
d[a[1].fr] = 0;
while (!pq.empty()){
int w = -pq.top().fr, v = pq.top().sc;
pq.pop();
if (d[v] < w)
continue;
for (pi to : g[v]){
if (d[to.fr] > d[v] + to.sc){
d[to.fr] = d[v] + to.sc;
pq.push(mk(-d[to.fr], to.fr));
}
}
}
cout << d[a[2].fr];
}
/*
Totally not inspired by BenQ's template
Things you should do:
1. Look for stupid typos in code e.g 1 instead of -1 etc
2. Check if there is no infinite loops
3. "Measure twice, cut once"
4. Stay focused
*/
Compilation message (stderr)
skyscraper.cpp:33:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main(){ // If you don't know what to do, check the text box at the bottom
^
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:36:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &a[i].fr, &a[i].sc);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |