Submission #1253957

#TimeUsernameProblemLanguageResultExecution timeMemory
1253957hoa208Jakarta Skyscrapers (APIO15_skyscraper)C++20
57 / 100
1098 ms52440 KiB
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define FORD(i, b, a) for (int i = (b), _a = (a); i >= _a; i--)
#define pa pair<ll, ll>
#define fi first
#define se second
#define bit(mask, j) ((mask >> j) & 1)
#define t_test int t;cin >> t;while(t--)
const   ll mod = 1e9 + 7;
const   ll INF = 1e18;
inline  void adm(ll &x){if(x>=mod)x%=mod;else if(x<0)x+=mod;}
//--------------------------------------------------------------------

struct state {
    ll w, u, t;
    bool operator < (const state &a) const {
        return w > a.w;
    }
};
priority_queue<state> q;
const ll BLOCK = 180;
const ll N = 3e4 + 10;
vector<ll> g[N];
ll n, m;
ll dp[N][BLOCK + 10];
void hbmt() {
    cin >> n >> m;
    ll doge1 = 0;
    memset(dp, 0x3f, sizeof dp);
    FOR(i, 1, m) {
        ll b, p;
        cin >> b >> p;
        if(i == 1) {
            dp[b][0] = 0;
            q.push({0, b, 0});
        }
        if(i == 2) {
            doge1 = b;
        }
        g[b].push_back(p);
    }
    while(!q.empty()) {
        ll u = q.top().u, du = q.top().w, t = q.top().t;
        q.pop();
        if(du != dp[u][t]) continue;
        if(u == doge1) {
            cout << du;
            exit(0);
        }
        if(t != 0 && u + t < n) {
            if(dp[u + t][t] > dp[u][t] + 1) {
                dp[u + t][t] = dp[u][t] + 1;
                q.push({dp[u + t][t], u + t, t});
            }
        } 
        if(t != 0 && u - t >= 0) {
            if(dp[u - t][t] > dp[u][t] + 1) {
                dp[u - t][t] = dp[u][t] + 1;
                q.push({dp[u - t][t], u - t, t});
            }
        }
        for(auto p : g[u]) {
            if(p > BLOCK) {
                ll c = 0;
                for(int v = u + p; v < n; v += p) {
                    c++;
                    if(dp[v][0] > dp[u][t] + c) {
                        dp[v][0] = dp[u][t] + c;
                        q.push({dp[v][0], v, 0});
                    }
                }
                c = 0;
                for(int v = u - p; v >= 0; v -= p) {
                    c++;
                    if(dp[v][0] > dp[u][t] + c) {
                        dp[v][0] = dp[u][t] + c;
                        q.push({dp[v][0], v, 0});
                    }
                }
            }
            else if(p != 0) {
                if(u + p < n) {
                    if(dp[u + p][p] > dp[u][t] + 1) {
                        dp[u + p][p] = dp[u][t] + 1;
                        q.push({dp[u + p][p], u + p, p});
                    }
                }
                if(u - p >= 0) {
                        if(dp[u - p][p] > dp[u][t] + 1) {
                        dp[u - p][p] = dp[u][t] + 1;
                        q.push({dp[u - p][p], u - p, p});
                    }
                }
            }
        }
    }
    cout << -1;
}

int main() {
    
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    
    if(fopen("hbmt.inp", "r")) {
        freopen("hbmt.inp", "r", stdin);
        freopen("hbmt.out", "w", stdout);
    }
    
    // t_test 
    hbmt();
    return 0;
}

Compilation message (stderr)

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