Submission #386702

# Submission time Handle Problem Language Result Execution time Memory
386702 2021-04-07T08:27:58 Z wenqi Jakarta Skyscrapers (APIO15_skyscraper) C++14
0 / 100
180 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 2069
#define O 30069
#define MAX 969000000
#define OM (O / MID + 69)

int N, M;

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

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

int smallc[O][MID + 69];
int bigc[O][2 * OM + 69];

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

int dp_small(int pos, int step) {
    int& ans = smallc[pos][step];
    if(ans != -2) return ans;
    ans = MAX;
    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 - step >= 0) {
        ans = min(ans, 1 + dp_small(pos - step, step));
    }
    if(pos + step < N) {
        ans = min(ans, 1 + dp_small(pos + step, step));
    }
    return ans;
}

int dp_big(int i, int offset) {
    int& 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, int, -2);
    fillc(bigc, int, -2);
    int ans;
    if(P[0] < MID) {
        ans = dp_small(B[0], P[0]);
    }else{
        ans = dp_big(0, OM);
    }
    cout << (ans >= MAX ? -1 : ans) << '\n';
    return 0;
}

# Verdict Execution time Memory Grader output
1 Runtime error 168 ms 262148 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 159 ms 262148 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 180 ms 262148 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 163 ms 262148 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 166 ms 262148 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -