제출 #1254166

#제출 시각아이디문제언어결과실행 시간메모리
1254166VKhangJakarta Skyscrapers (APIO15_skyscraper)C++20
100 / 100
153 ms32696 KiB
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5, MOD = 1e9 + 7;
const long long L = 1e18;
#define ll long long
#define fi first
#define se second
#define yes {cout << "YES";return;}
#define no  {cout << "NO"; return;}
#define pb push_back
#define MASK(i) (1ll << (i))
#define BIT(i, s) ((1ll << (i)) & (s))
#define all(a) a.begin(), a.end()
int n, m;
int a[N], b[N];
bool visited[N], Left[N], Right[N];
int dist[N];
vector <int> s[N];
priority_queue <pair <int, int>, vector<pair <int, int>>, greater <pair <int, int>>> p;
void solve()
{
    cin >> n >> m;
    for (int i = 0; i < m; i++){
        cin >> a[i] >> b[i];
        s[a[i]].pb(i);
    }
    for (int i = 0; i < n; i++) sort(all(s[i]));
    memset(dist, 0x3f, sizeof dist);
    for (int x: s[a[0]]){
        dist[x] = 0;
        p.push({0, x});
    }
    while(p.size()){
        int i = p.top().fi;
        int j = p.top().se;
        p.pop();
        if (visited[j]) continue;
        visited[j] = true;
        bool ok = true;
        if (!Left[j]){
            for (int l = a[j] - b[j]; ok && l >= 0; l -= b[j]){
                int pre = 0;
                for (int x: s[l]){
                    if (visited[x]) break;
                    if (b[x] == pre) continue;
                    pre = b[x];
                    int y = (a[j] - l) / b[j];
                    dist[x] = min(dist[x], i + y);
                    if (b[x] == b[j]){
                        if (ok) p.push({dist[x], x});
                        Right[x] = true;
                        ok = false;
                    } else p.push({dist[x], x});
                }
            }
        }
        ok = true;
        if (!Right[j]){
            for (int l = a[j] + b[j]; ok && l < n; l += b[j]){
                int pre = 0;
                for (int x: s[l]){
                    if (visited[x]) break;
                    if (b[x] == pre) continue;
                    pre = b[x];
                    int y = (l - a[j]) / b[j];
                    dist[x] = min(dist[x], i + y);
                    if (b[x] == b[j]){
                        if (ok) p.push({dist[x], x});
                        Left[x] = true;
                        ok = false;
                    } else p.push({dist[x], x});
                }
            }
        }
    }
    cout << (dist[1] == dist[m + 1] ? -1 : dist[1]);
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    if (fopen("VKhang.inp","r")){
        freopen("VKhang.inp","r",stdin);
        freopen("VKhang.out","w",stdout);
    }
    solve();
    return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

skyscraper.cpp: In function 'int main()':
skyscraper.cpp:82:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   82 |         freopen("VKhang.inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:83:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   83 |         freopen("VKhang.out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#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...