답안 #386781

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
386781 2021-04-07T09:46:22 Z wenqi Jakarta Skyscrapers (APIO15_skyscraper) C++14
0 / 100
146 ms 262148 KB
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

#define all(x) begin(x), end(x)
#define fillc(p, t, v) fill_n((t*) p, sizeof(p) / sizeof(t), v)
#define pb push_back

#define MID 169
#define O 30069
#define MAX 96900000000ll
#define K 9999999999699ll
#define OM (O / MID + 69)

int N, M;

int B[O];
int P[O];
int TP;

vector<int> SMALL[O];
vector<int> BIG[O];

ll smallc[O][1069];
bool seen1[O][MID + 69];
ll bigc[O][1069];

ll dp_small(int, int);
ll dp_big(int, int);

ll dp_small(int pos, int step) {
    ll& ans = smallc[pos][step];
    if(ans != -2) return ans;
    ans = K;
    ll a = ans;
    if(pos == TP) return ans = 0;
    for(int j : SMALL[pos]) {
        a = min(a, dp_small(B[j], P[j]));
    }
    for(int j : BIG[pos]) {
        a = min(a, dp_big(j, OM));
    }
    if(pos - step >= 0) {
        a = min(a, 1 + dp_small(pos - step, step));
    }
    if(pos + step < N) {
        a = min(a, 1 + dp_small(pos + step, step));
    }
    if(a == K) {
        ans = -2;
        return K;
    }else{
        ans = min(ans, MAX);
        return ans = a;
    }
}

ll dp_big(int i, int offset) {
    ll& ans = bigc[i][offset];
    if(ans != -2) return ans;
    ans = MAX;
    int pos = B[i] + (offset - OM) * P[i];
    if(pos == TP) return ans = 0;
    for(int j : SMALL[pos]) {
        ans = min(ans, dp_small(B[j], P[j]));
    }
    for(int j : BIG[pos]) {
        ans = min(ans, dp_big(j, OM));
    }
    if(pos - P[i] >= 0) {
        ans = min(ans, 1 + dp_big(i, offset - 1));
    }
    if(pos + P[i] < N) {
        ans = min(ans, 1 + dp_big(i, offset + 1));
    }
    return ans;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> N >> M;
    for(int i = 0; i < M; i++) {
        cin >> B[i] >> P[i];
        if(P[i] < MID)
            SMALL[B[i]].pb(i);
        else
            BIG[B[i]].pb(i);
        if(i == 1) TP = B[i];
    }
    fillc(smallc, ll, MAX);
    fillc(bigc, ll, MAX);
    queue<pair<int, pair<int, int>>> q;
    if(P[0] < MID) {
        smallc[B[0]][P[0]] = 0;
        q.push({0, {B[0], P[0]}});
    }else{
        bigc[0][OM] = 0;
        q.push({1, {0, OM}});
    }
    while(not q.empty()) {
        auto p = q.front();
        q.pop();

        int pos, step;
        int a, b;

        int t = p.first;
        a = p.second.first;
        b = p.second.second;

        ll dis;

        if (t == 0) {
            dis = smallc[a][b];
            pos = a;
            step = b;
        }else{
            dis = bigc[a][b];
            pos = B[a] + (b - OM) * P[a];
            step = P[a];
        }

        for(int j : SMALL[pos]) {
            ll& c = smallc[B[j]][P[j]];
            if(dis < c) {
                c = dis;
                q.push({0, {B[j], P[j]}});
            }
        }

        for(int j : BIG[pos]) {
            ll& c = bigc[j][OM];
            if(dis < c) {
                c = dis;
                q.push({1, {j, OM}});
            }
        }

        if(t == 0) {
        if(pos - step >= 0) {
            ll& c = smallc[pos - step][step];
            if(dis + 1 < c) {
                c = dis + 1;
                q.push({t, {pos - step, step}});
            }
        }
        if(pos + step < N) {
            ll& c = smallc[pos + step][step];
            if(dis + 1 < c) {
                c = dis + 1;
                q.push({t, {pos + step, step}});
            }
        }
        }else{
            if(pos - step >= 0) {
            ll& c = bigc[a][b - 1];
            if(dis + 1 < c) {
                c = dis + 1;
                q.push({t, {a, b - 1}});
            }
        }
        if(pos + step < N) {
            ll& c = bigc[a][b + 1];
            if(dis + 1 < c) {
                c = dis + 1;
                q.push({t, {a, b + 1}});
            }
        }
        }
    }
    ll ans = MAX;
    for(auto k : smallc[TP])
        ans = min(ans, k);
    cout << (ans >= MAX ? -1 : ans) << '\n';
    return 0;
}

# 결과 실행 시간 메모리 Grader output
1 Runtime error 145 ms 262148 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 137 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 139 ms 262148 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 146 ms 262148 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 144 ms 262148 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -