// happy
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
#define int long long
//#define ll long long
#define fi first
#define se second
#define pb push_back
using namespace std;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tp;
const int N = 1e5 + 5;
const int oo = 1e15;
const int mod = 1e9 + 7;
const int base = 29; // >= so pt pb max
struct e{
int b, p;
} T[N];
bool comp(e g1, e g2){
return g1.b < g2.b;
}
int n, m, b[N], p[N], dp[N];
vector<int> v[N];
void dij(){
for(int i = 1; i <= m; i ++) dp[i] = oo;
priority_queue<pii, vector<pii>, greater<pii>> pq;
dp[1] = 0;
pq.push({0, 1});
while(!pq.empty()){
int w = pq.top().fi;
int i = pq.top().se;
pq.pop();
if(w > dp[i]) continue;
// cout << w << " " << i << " g\n";
if(i == 2){
cout << dp[i];
return;
}
if(!p[i]) continue;
for(int j = 1; i + j * p[i] <= m || i - j * p[i] >= 1; j ++){
int tmp = i + j * p[i];
if(tmp <= m && dp[tmp] > dp[i] + j){
pq.push({dp[tmp] = dp[i] + j, tmp});
}
tmp = i - j * p[i];
if(tmp >= 1 && dp[tmp] > dp[i] + j) pq.push({dp[tmp] = dp[i] + j, tmp});
}
}
cout << -1;
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define task "v"
if(fopen(task ".inp","r")){
freopen(task ".inp","r",stdin);
freopen(task ".out","w",stdout);
}
cin >> m >> n;
for(int i = 1; i <= n; i ++){
cin >> T[i].b >> T[i].p;
T[i].b++;
p[T[i].b] = T[i].p;
// cout << T[i].b << " " << T[i].p << "h\n";
}
sort(T + 1, T + n + 1, comp);
dij();
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:70:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
70 | freopen(task ".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:71:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
71 | freopen(task ".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4700 KB |
Output is correct |
2 |
Incorrect |
1 ms |
4704 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4700 KB |
Output is correct |
2 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4700 KB |
Output is correct |
2 |
Incorrect |
1 ms |
4704 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Incorrect |
1 ms |
4696 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Incorrect |
1 ms |
4696 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |