Submission #807135

# Submission time Handle Problem Language Result Execution time Memory
807135 2023-08-04T13:58:11 Z rnl42 Passport (JOI23_passport) C++14
16 / 100
466 ms 1048576 KB
#include <bits/stdc++.h>
using namespace std;

string to_string(string s) { return s; }
template <typename T> string to_string(T v) {
	bool first = true;
	string res = "[";
	for (const auto &x : v) {
		if (!first)
			res += ", ";
		first = false;
		res += to_string(x);
	}
	res += "]";
	return res;
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

void dbg_out() { cout << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
	cout << ' ' << to_string(H);
	dbg_out(T...);
}

#ifdef DEBUG
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

int N, Q;
vector<int> L, R;
vector<int> X;
vector<vector<int>> dp;

int main() {
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> N;
    L.resize(N), R.resize(N);
    vector<vector<vector<int>>> ibyLR(N, vector<vector<int>>(N));
    for (int i = 0; i < N; i++) {
        cin >> L[i] >> R[i], L[i]--, R[i]--;
        ibyLR[L[i]][R[i]].push_back(i);
    }
    cin >> Q;
    X.resize(Q);
    for (int i = 0; i < Q; i++) {
        cin >> X[i], X[i]--;
    }
    dp.assign(N, vector<int>(N, 1e9));
    dp[0][N-1] = 0;
    vector<vector<int>> optR(N+1, vector<int>(N+1, 1e9));
    for (int r = 0; r < N; r++) {
        for (int l = r; l >= 0; l--) {
            if (R[l] <= r) {
                optR[l][r] = min(optR[l+1][r], L[l]);
                dbg(l, r, optR[l][r]);
            }
        }
    }
    vector<int> optLR(N, 1e9);
    for (int l = 0; l < N; l++) {
        vector<int> opt(N+1, -1);
        vector<int> optLRcumul(N+1, 1e9);
        for (int r = l; r < N; r++) {
            if (L[r] >= l) {
                opt[r+1] = max(opt[r], R[r]);
            }
        }
        for (int r = l; r < N; r++) {
            optLRcumul[r+1] = min(optLRcumul[r], optLR[r]);
        }
        for (int r = N-1; r >= l; r--) {
            dp[l][r] = min(dp[l][r], optLRcumul[r+1]);
            if (opt[r+1] != -1) {
                dp[l][r] = min(dp[l][r], 1+dp[l][opt[r+1]]);
            }
            if (optR[l][r] != 1e9) {
                dp[l][r] = min(dp[l][r], 1+dp[optR[l][r]][r]);
            }
            for (int i : ibyLR[l][r]) {
                optLR[i] = min(optLR[i], 1+dp[l][r]);
            }
        }
    }
    for (int i = 0; i < Q; i++) {
        cout << (dp[X[i]][X[i]] >= 1e9 ? -1 : dp[X[i]][X[i]]) << '\n';
    }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Runtime error 466 ms 1048576 KB Execution killed with signal 9
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 2 ms 2900 KB Output is correct
12 Correct 2 ms 3028 KB Output is correct
13 Correct 3 ms 3156 KB Output is correct
14 Correct 2 ms 2900 KB Output is correct
15 Correct 4 ms 3156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 2 ms 2900 KB Output is correct
12 Correct 2 ms 3028 KB Output is correct
13 Correct 3 ms 3156 KB Output is correct
14 Correct 2 ms 2900 KB Output is correct
15 Correct 4 ms 3156 KB Output is correct
16 Correct 108 ms 171688 KB Output is correct
17 Correct 128 ms 194180 KB Output is correct
18 Incorrect 116 ms 196428 KB Output isn't correct
19 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 2 ms 2900 KB Output is correct
12 Correct 2 ms 3028 KB Output is correct
13 Correct 3 ms 3156 KB Output is correct
14 Correct 2 ms 2900 KB Output is correct
15 Correct 4 ms 3156 KB Output is correct
16 Correct 108 ms 171688 KB Output is correct
17 Correct 128 ms 194180 KB Output is correct
18 Incorrect 116 ms 196428 KB Output isn't correct
19 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Runtime error 466 ms 1048576 KB Execution killed with signal 9
5 Halted 0 ms 0 KB -