Submission #709892

#TimeUsernameProblemLanguageResultExecution timeMemory
709892noeditJakarta Skyscrapers (APIO15_skyscraper)C++17
57 / 100
530 ms262144 KiB
#include <bits/stdc++.h> //#pragma GCC optimize("Ofast") //#pragma GCC optimize("unroll-loops") using namespace std; typedef long long ll; const int INF = 1e9; const int B = 160; const int MAXN = 3e4 + 5; vector<pair<int, int> > d; int dist[MAXN][B]; vector<array<short, 3> > g[MAXN][B]; signed main() { //ifstream cin("input.txt"); //ofstream cout("output.txt"); ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; d.resize(m); vector<int> big; for (int i = 0; i < m; i++) { cin >> d[i].first >> d[i].second; if (d[i].second >= B) { big.push_back(i); } } for (int i = 0; i < n; i++) for (int j = 0; j < B; j++) dist[i][j] = INF; for (int i = 0; i < m; i++) { if (d[i].second >= B) continue; g[d[i].first][0].push_back({d[i].first, d[i].second, 0}); } for (int i = 0; i < n; i++) { for (int j = 1; j < B; j++) { if (i - j >= 0) { g[i][j].push_back({i - j, 0, 1}); g[i][j].push_back({i - j, j, 1}); } if (i + j < n) { g[i][j].push_back({i + j, 0, 1}); g[i][j].push_back({i + j, j, 1}); } } } for (auto& i : big) { for (int j = d[i].first, k = 0; j < n; j += d[i].second, k++) { g[d[i].first][0].push_back({j, 0, k}); } for (int j = d[i].first, k = 0; j >= 0; j -= d[i].second, k++) { g[d[i].first][0].push_back({j, 0, k}); } } set<array<int, 3> > st; st.insert({0, d[0].first, 0}); dist[d[0].first][0] = 0; while (!st.empty()) { auto [dst, pos, pw] = *st.begin(); st.erase(st.begin()); if (dst > dist[pos][pw]) continue; for (auto& [psu, pwu, w] : g[pos][pw]) { if (dist[psu][pwu] > dst + w) { st.erase({dist[psu][pwu], psu, pwu}); dist[psu][pwu] = dst + w; st.insert({dist[psu][pwu], psu, pwu}); } } } int mn = INF; for (int i = 0; i < B; i++) mn = min(mn, dist[d[1].first][i]); if (mn == INF) cout << -1; else cout << mn; return 0; } /* small (i, j) -1> (i - j, 0), (i + j, 0) (i, 0) -0> (i, j) (i, j) -1> (i - j, j), (i + j, j) large (i, 0) -1> (i + j, 0) (i, 0) -1> (i - j, 0) */

Compilation message (stderr)

skyscraper.cpp: In function 'int main()':
skyscraper.cpp:43:42: warning: narrowing conversion of 'd.std::vector<std::pair<int, int> >::operator[](((std::vector<std::pair<int, int> >::size_type)i)).std::pair<int, int>::first' from 'int' to 'short int' [-Wnarrowing]
   43 |         g[d[i].first][0].push_back({d[i].first, d[i].second, 0});
skyscraper.cpp:43:54: warning: narrowing conversion of 'd.std::vector<std::pair<int, int> >::operator[](((std::vector<std::pair<int, int> >::size_type)i)).std::pair<int, int>::second' from 'int' to 'short int' [-Wnarrowing]
   43 |         g[d[i].first][0].push_back({d[i].first, d[i].second, 0});
skyscraper.cpp:51:38: warning: narrowing conversion of '(i - j)' from 'int' to 'short int' [-Wnarrowing]
   51 |                 g[i][j].push_back({i - j, 0, 1});
      |                                    ~~^~~
skyscraper.cpp:52:38: warning: narrowing conversion of '(i - j)' from 'int' to 'short int' [-Wnarrowing]
   52 |                 g[i][j].push_back({i - j, j, 1});
      |                                    ~~^~~
skyscraper.cpp:52:43: warning: narrowing conversion of 'j' from 'int' to 'short int' [-Wnarrowing]
   52 |                 g[i][j].push_back({i - j, j, 1});
      |                                           ^
skyscraper.cpp:56:38: warning: narrowing conversion of '(i + j)' from 'int' to 'short int' [-Wnarrowing]
   56 |                 g[i][j].push_back({i + j, 0, 1});
      |                                    ~~^~~
skyscraper.cpp:57:38: warning: narrowing conversion of '(i + j)' from 'int' to 'short int' [-Wnarrowing]
   57 |                 g[i][j].push_back({i + j, j, 1});
      |                                    ~~^~~
skyscraper.cpp:57:43: warning: narrowing conversion of 'j' from 'int' to 'short int' [-Wnarrowing]
   57 |                 g[i][j].push_back({i + j, j, 1});
      |                                           ^
skyscraper.cpp:65:41: warning: narrowing conversion of 'j' from 'int' to 'short int' [-Wnarrowing]
   65 |             g[d[i].first][0].push_back({j, 0, k});
      |                                         ^
skyscraper.cpp:65:47: warning: narrowing conversion of 'k' from 'int' to 'short int' [-Wnarrowing]
   65 |             g[d[i].first][0].push_back({j, 0, k});
      |                                               ^
skyscraper.cpp:69:41: warning: narrowing conversion of 'j' from 'int' to 'short int' [-Wnarrowing]
   69 |             g[d[i].first][0].push_back({j, 0, k});
      |                                         ^
skyscraper.cpp:69:47: warning: narrowing conversion of 'k' from 'int' to 'short int' [-Wnarrowing]
   69 |             g[d[i].first][0].push_back({j, 0, k});
      |                                               ^
#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...