Submission #516212

# Submission time Handle Problem Language Result Execution time Memory
516212 2022-01-20T16:15:28 Z Dirii Jakarta Skyscrapers (APIO15_skyscraper) C++14
100 / 100
749 ms 46396 KB
// #pragma GCC target("avx2")
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define ll long long
#define cll const ll
#define lp(a, b, c) for(ll a = b; a <= c; ++a)
#define lpd(a, b, c) for(ll a = b; a >= c; --a)
#define vec(a) vector<a>
#define pp(a, b) pair<a, b>
#define EACHCASE lpd(cs, read(), 1)
#define Fname "f"
using namespace std;

template <typename T> inline void Read(T &x){
    x = 0; char c;
    ll dau = 1;
    while(!isdigit(c = getchar())) if(c == '-') dau = -1;
    do{
        x = x * 10 + c - '0';
    } while (isdigit(c = getchar()));
    x *= dau;
}

ll read(){
    ll tmp;
    cin >> tmp;
    return tmp;
}

void giuncute(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
}

void OF(){
    if(fopen(Fname".inp", "r")){
        freopen(Fname".inp", "r", stdin);
        freopen(Fname".out", "w", stdout);
    } else if(fopen(Fname".in", "r")){
        freopen(Fname".in", "r", stdin);
        freopen(Fname".out", "w", stdout);
    }
}

cll mxn = 3e4 + 5, _sqrt = 173;
ll n, m, dp[mxn][_sqrt + 4], initu, initp, finalu, finalp;
vec(ll) jump[mxn];
priority_queue<pp(ll, pp(ll, ll))> q;
bool visited[mxn] = {0};

void prc(ll u, ll cu, ll step){
    if(u + step < n && dp[u + step][step] > cu + 1){
        dp[u + step][step] = cu + 1;
        q.push({-dp[u + step][step], {u + step, step}});
    } 
    if(u - step >= 0 && dp[u - step][step] > cu + 1){
        dp[u - step][step] = cu + 1;
        q.push({-dp[u - step][step], {u - step, step}});
    } 
}

int main(){
    giuncute();
    #ifndef ONLINE_JUDGE
    OF();
    #endif
    cin >> n >> m;
    lp(i, 1, m){
        static ll u, p;
        cin >> u >> p;
        if(i == 1) initu = u, initp = p;
        if(i == 2) finalu = u, finalp = p;
        jump[u].push_back(p);
    }
    lp(i, 0, n) lp(j, 0, _sqrt) dp[i][j] = 1e16;
    dp[initu][0] = 0;
    q.push({0, {initu, 0}});
    ll ans = 1e16;
    while(q.size()){
        ll cu = -q.top().first, u = q.top().second.first, step = q.top().second.second;
        q.pop();
        if(cu != dp[u][step]) continue;
        if(u == finalu) ans = min(ans, cu);

        // maintain last step
        prc(u, cu, step);

        if(visited[u]) continue;
        visited[u] = 1;
        // change step
        for(ll i : jump[u]){
            if(i <= _sqrt)
                prc(u, cu, i);
            else{
                for(ll v = u + i, cnt = 1; v < n; v += i, ++cnt)
                    if(dp[v][0] > cu + cnt){
                        dp[v][0] = cu + cnt;
                        q.push({-dp[v][0], {v, 0}});
                    }
                for(ll v = u - i, cnt = 1; v >= 0; v -= i, ++cnt)
                    if(dp[v][0] > cu + cnt){
                        dp[v][0] = cu + cnt;
                        q.push({-dp[v][0], {v, 0}});
                    }
            }
        }
    }
    if(ans > 1e15) ans = -1;
    cout << ans;
}

Compilation message

skyscraper.cpp: In function 'void OF()':
skyscraper.cpp:38:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   38 |         freopen(Fname".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:39:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   39 |         freopen(Fname".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:41:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   41 |         freopen(Fname".in", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:42:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   42 |         freopen(Fname".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 972 KB Output is correct
2 Correct 1 ms 972 KB Output is correct
3 Correct 1 ms 972 KB Output is correct
4 Correct 1 ms 972 KB Output is correct
5 Correct 1 ms 972 KB Output is correct
6 Correct 1 ms 972 KB Output is correct
7 Correct 1 ms 972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 972 KB Output is correct
2 Correct 1 ms 972 KB Output is correct
3 Correct 1 ms 972 KB Output is correct
4 Correct 1 ms 972 KB Output is correct
5 Correct 1 ms 972 KB Output is correct
6 Correct 1 ms 972 KB Output is correct
7 Correct 1 ms 972 KB Output is correct
8 Correct 1 ms 972 KB Output is correct
9 Correct 1 ms 1100 KB Output is correct
10 Correct 1 ms 1100 KB Output is correct
11 Correct 2 ms 1228 KB Output is correct
12 Correct 1 ms 1100 KB Output is correct
13 Correct 1 ms 1100 KB Output is correct
14 Correct 2 ms 1356 KB Output is correct
15 Correct 2 ms 1356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 972 KB Output is correct
2 Correct 1 ms 972 KB Output is correct
3 Correct 1 ms 972 KB Output is correct
4 Correct 1 ms 972 KB Output is correct
5 Correct 1 ms 972 KB Output is correct
6 Correct 1 ms 972 KB Output is correct
7 Correct 1 ms 972 KB Output is correct
8 Correct 1 ms 972 KB Output is correct
9 Correct 1 ms 1100 KB Output is correct
10 Correct 2 ms 1100 KB Output is correct
11 Correct 2 ms 1228 KB Output is correct
12 Correct 1 ms 1100 KB Output is correct
13 Correct 1 ms 1100 KB Output is correct
14 Correct 2 ms 1356 KB Output is correct
15 Correct 2 ms 1356 KB Output is correct
16 Correct 1 ms 1228 KB Output is correct
17 Correct 3 ms 2124 KB Output is correct
18 Correct 2 ms 3404 KB Output is correct
19 Correct 2 ms 3788 KB Output is correct
20 Correct 2 ms 3788 KB Output is correct
21 Correct 1 ms 1484 KB Output is correct
22 Correct 1 ms 3404 KB Output is correct
23 Correct 2 ms 3276 KB Output is correct
24 Correct 3 ms 3660 KB Output is correct
25 Correct 3 ms 3788 KB Output is correct
26 Correct 3 ms 3788 KB Output is correct
27 Correct 3 ms 3788 KB Output is correct
28 Correct 4 ms 3788 KB Output is correct
29 Correct 12 ms 3788 KB Output is correct
30 Correct 3 ms 3788 KB Output is correct
31 Correct 5 ms 3860 KB Output is correct
32 Correct 4 ms 3788 KB Output is correct
33 Correct 16 ms 3964 KB Output is correct
34 Correct 15 ms 3968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 972 KB Output is correct
2 Correct 1 ms 972 KB Output is correct
3 Correct 1 ms 972 KB Output is correct
4 Correct 1 ms 1100 KB Output is correct
5 Correct 1 ms 972 KB Output is correct
6 Correct 1 ms 972 KB Output is correct
7 Correct 1 ms 972 KB Output is correct
8 Correct 1 ms 972 KB Output is correct
9 Correct 1 ms 1100 KB Output is correct
10 Correct 1 ms 1100 KB Output is correct
11 Correct 2 ms 1228 KB Output is correct
12 Correct 1 ms 1100 KB Output is correct
13 Correct 2 ms 1100 KB Output is correct
14 Correct 2 ms 1356 KB Output is correct
15 Correct 2 ms 1356 KB Output is correct
16 Correct 1 ms 1228 KB Output is correct
17 Correct 3 ms 2124 KB Output is correct
18 Correct 2 ms 3404 KB Output is correct
19 Correct 2 ms 3788 KB Output is correct
20 Correct 2 ms 3788 KB Output is correct
21 Correct 1 ms 1484 KB Output is correct
22 Correct 2 ms 3404 KB Output is correct
23 Correct 2 ms 3276 KB Output is correct
24 Correct 3 ms 3660 KB Output is correct
25 Correct 2 ms 3788 KB Output is correct
26 Correct 2 ms 3788 KB Output is correct
27 Correct 2 ms 3788 KB Output is correct
28 Correct 3 ms 3788 KB Output is correct
29 Correct 8 ms 3788 KB Output is correct
30 Correct 4 ms 3788 KB Output is correct
31 Correct 5 ms 3788 KB Output is correct
32 Correct 4 ms 3844 KB Output is correct
33 Correct 17 ms 4028 KB Output is correct
34 Correct 18 ms 3916 KB Output is correct
35 Correct 16 ms 3636 KB Output is correct
36 Correct 3 ms 2636 KB Output is correct
37 Correct 20 ms 4496 KB Output is correct
38 Correct 19 ms 4492 KB Output is correct
39 Correct 19 ms 4496 KB Output is correct
40 Correct 26 ms 4492 KB Output is correct
41 Correct 22 ms 4492 KB Output is correct
42 Correct 6 ms 4172 KB Output is correct
43 Correct 6 ms 4172 KB Output is correct
44 Correct 6 ms 4084 KB Output is correct
45 Correct 56 ms 5064 KB Output is correct
46 Correct 53 ms 5068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 972 KB Output is correct
2 Correct 1 ms 972 KB Output is correct
3 Correct 1 ms 972 KB Output is correct
4 Correct 1 ms 972 KB Output is correct
5 Correct 1 ms 972 KB Output is correct
6 Correct 1 ms 972 KB Output is correct
7 Correct 1 ms 972 KB Output is correct
8 Correct 1 ms 972 KB Output is correct
9 Correct 1 ms 1100 KB Output is correct
10 Correct 1 ms 1100 KB Output is correct
11 Correct 2 ms 1228 KB Output is correct
12 Correct 1 ms 1100 KB Output is correct
13 Correct 1 ms 1100 KB Output is correct
14 Correct 2 ms 1356 KB Output is correct
15 Correct 2 ms 1372 KB Output is correct
16 Correct 1 ms 1228 KB Output is correct
17 Correct 3 ms 2124 KB Output is correct
18 Correct 3 ms 3404 KB Output is correct
19 Correct 2 ms 3788 KB Output is correct
20 Correct 2 ms 3788 KB Output is correct
21 Correct 1 ms 1484 KB Output is correct
22 Correct 2 ms 3404 KB Output is correct
23 Correct 2 ms 3276 KB Output is correct
24 Correct 3 ms 3660 KB Output is correct
25 Correct 3 ms 3788 KB Output is correct
26 Correct 2 ms 3788 KB Output is correct
27 Correct 3 ms 3792 KB Output is correct
28 Correct 5 ms 3788 KB Output is correct
29 Correct 8 ms 3788 KB Output is correct
30 Correct 3 ms 3788 KB Output is correct
31 Correct 5 ms 3788 KB Output is correct
32 Correct 4 ms 3788 KB Output is correct
33 Correct 14 ms 3916 KB Output is correct
34 Correct 15 ms 3976 KB Output is correct
35 Correct 13 ms 3660 KB Output is correct
36 Correct 3 ms 2636 KB Output is correct
37 Correct 22 ms 4496 KB Output is correct
38 Correct 20 ms 4492 KB Output is correct
39 Correct 19 ms 4500 KB Output is correct
40 Correct 19 ms 4492 KB Output is correct
41 Correct 19 ms 4516 KB Output is correct
42 Correct 6 ms 4172 KB Output is correct
43 Correct 5 ms 4172 KB Output is correct
44 Correct 6 ms 4084 KB Output is correct
45 Correct 59 ms 5080 KB Output is correct
46 Correct 56 ms 5068 KB Output is correct
47 Correct 82 ms 19404 KB Output is correct
48 Correct 19 ms 33612 KB Output is correct
49 Correct 20 ms 36684 KB Output is correct
50 Correct 20 ms 40012 KB Output is correct
51 Correct 60 ms 44100 KB Output is correct
52 Correct 85 ms 44092 KB Output is correct
53 Correct 37 ms 43216 KB Output is correct
54 Correct 20 ms 42536 KB Output is correct
55 Correct 18 ms 42504 KB Output is correct
56 Correct 24 ms 43452 KB Output is correct
57 Correct 80 ms 42720 KB Output is correct
58 Correct 23 ms 42960 KB Output is correct
59 Correct 27 ms 42944 KB Output is correct
60 Correct 37 ms 42960 KB Output is correct
61 Correct 31 ms 42912 KB Output is correct
62 Correct 51 ms 43468 KB Output is correct
63 Correct 50 ms 44668 KB Output is correct
64 Correct 55 ms 44616 KB Output is correct
65 Correct 265 ms 46252 KB Output is correct
66 Correct 747 ms 46392 KB Output is correct
67 Correct 749 ms 46396 KB Output is correct