Submission #977914

#TimeUsernameProblemLanguageResultExecution timeMemory
977914SmuggingSpunJakarta Skyscrapers (APIO15_skyscraper)C++14
57 / 100
156 ms33192 KiB
#include<bits/stdc++.h> #define taskname "skyscraper" using namespace std; typedef long long ll; template<class T>bool minimize(T& a, T b){ if(a > b){ a = b; return true; } return false; } const int lim = 3e4 + 5; int n, m, b[lim], p[lim]; namespace sub1{ void solve(){ if(m == 2){ return void(cout << (abs(b[0] - b[1]) % p[0] == 0 ? abs(b[0] - b[1]) / p[0] : -1)); } int ans = (abs(b[0] - b[1]) % p[0] == 0 ? abs(b[0] - b[1]) / p[0] : INT_MAX); if(abs(b[0] - b[2]) % p[0] == 0 && abs(b[2] - b[1]) % p[2] == 0){ minimize(ans, abs(b[0] - b[2]) / p[0] + abs(b[2] - b[1]) / p[2]); } cout << (ans == INT_MAX ? -1 : ans); } } namespace sub23{ const int LIM = 2e3 + 5; int d[LIM]; void solve(){ priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>pq; memset(d, 0x0f, sizeof(d)); pq.emplace(d[0] = 0, 0); while(!pq.empty()){ auto [w, u] = pq.top(); pq.pop(); if(w == d[u]){ for(int v = 0; v < m; v++){ if(abs(b[u] - b[v]) % p[u] == 0 && minimize(d[v], w + abs(b[u] - b[v]) / p[u])){ pq.emplace(d[v], v); } } } } cout << (d[1] == d[LIM - 1] ? -1 : d[1]); } } namespace sub4{ const int LIM = 2e3 + 5; int d[LIM]; vector<int>pos[LIM]; void solve(){ priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>pq; memset(d, 0x0f, sizeof(d)); pq.emplace(d[b[0]] = 0, b[0]); for(int i = 0; i < m; i++){ pos[b[i]].emplace_back(i); } while(!pq.empty()){ auto [w, u] = pq.top(); pq.pop(); if(u == b[1]){ return void(cout << w); } if(w == d[u]){ for(int& index : pos[u]){ for(int v = u - p[index], cnt = 1; v > -1; v -= p[index], cnt++){ if(minimize(d[v], w + cnt)){ pq.emplace(d[v], v); } } for(int v = u + p[index], cnt = 1; v < n; v += p[index], cnt++){ if(minimize(d[v], w + cnt)){ pq.emplace(d[v], v); } } } } } cout << -1; } } namespace sub5{ const int SIZE = 200; vector<int>pos[lim]; vector<vector<int>>e[SIZE]; int d[lim]; void solve(){ for(int i = 1; i < SIZE; i++){ e[i].resize(i); } for(int i = 0; i < m; i++){ pos[b[i]].emplace_back(p[i]); for(int j = 1; j < SIZE; j++){ e[j][b[i] % j].emplace_back(b[i]); } } for(int i = 0; i < n; i++){ sort(pos[i].begin(), pos[i].end()); pos[i].resize(unique(pos[i].begin(), pos[i].end()) - pos[i].begin()); } priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>pq; memset(d, 0x0f, sizeof(d)); pq.emplace(d[b[0]] = 0, 0); while(!pq.empty()){ auto [w, u] = pq.top(); pq.pop(); if(u == b[1]){ return void(cout << w); } if(w == d[u]){ for(int& index : pos[u]){ if(p[index] < SIZE){ for(int& v : e[p[index]][u % p[index]]){ if(minimize(d[v], w + abs(v - u) / p[index])){ pq.emplace(d[v], v); } } } else{ for(int v = u - p[index], cnt = 1; v > -1; v -= p[index], cnt++){ if(minimize(d[v], w + cnt)){ pq.emplace(d[v], v); } } for(int v = u + p[index], cnt = 1; v < n; v += p[index], cnt++){ if(minimize(d[v], w + cnt)){ pq.emplace(d[v], v); } } } } } } cout << -1; } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); if(fopen(taskname".inp", "r")){ freopen(taskname".inp", "r", stdin); } cin >> n >> m; for(int i = 0; i < m; i++){ cin >> b[i] >> p[i]; } if(n <= 10 && m <= 3){ sub1::solve(); } else if(n <= 2000 && m <= 2000){ sub23::solve(); } else if(n <= 2000){ sub4::solve(); } else{ sub5::solve(); } }

Compilation message (stderr)

skyscraper.cpp: In function 'void sub23::solve()':
skyscraper.cpp:34:9: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   34 |    auto [w, u] = pq.top();
      |         ^
skyscraper.cpp: In function 'void sub4::solve()':
skyscraper.cpp:59:9: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   59 |    auto [w, u] = pq.top();
      |         ^
skyscraper.cpp: In function 'void sub5::solve()':
skyscraper.cpp:105:9: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  105 |    auto [w, u] = pq.top();
      |         ^
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:140:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  140 |   freopen(taskname".inp", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...