Submission #807241

# Submission time Handle Problem Language Result Execution time Memory
807241 2023-08-04T15:11:44 Z rnl42 Passport (JOI23_passport) C++14
48 / 100
421 ms 1048576 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long

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;

signed 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, 1e18));
    dp[0][N-1] = 0;
    vector<vector<int>> optR(N+1, vector<int>(N+1, 1e18));
    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]);
            } else optR[l][r] = optR[l+1][r];
        }
    }
    vector<int> optLR(N, 1e18);
    for (int l = 0; l < N; l++) {
        vector<int> opt(N+1, -1e18);
        vector<int> optLRcumul(N+1, 1e18);
        for (int r = l; r < N; r++) {
            if (L[r] >= l) {
                opt[r+1] = max(opt[r], R[r]);
            } else opt[r+1] = opt[r];
            opt[r+1] = max(opt[r], R[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] >= 0) {
                dp[l][r] = min(dp[l][r], 1+dp[l][opt[r+1]]);
            }
            if (optR[l][r] < 1e18) {
                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]] >= 1e18 ? -1 : dp[X[i]][X[i]]) << '\n';
    }
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Runtime error 421 ms 1048576 KB Execution killed with signal 9
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 3 ms 3540 KB Output is correct
12 Correct 2 ms 3680 KB Output is correct
13 Correct 2 ms 3796 KB Output is correct
14 Correct 2 ms 3540 KB Output is correct
15 Correct 2 ms 3796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 3 ms 3540 KB Output is correct
12 Correct 2 ms 3680 KB Output is correct
13 Correct 2 ms 3796 KB Output is correct
14 Correct 2 ms 3540 KB Output is correct
15 Correct 2 ms 3796 KB Output is correct
16 Correct 151 ms 214500 KB Output is correct
17 Correct 149 ms 242580 KB Output is correct
18 Correct 157 ms 245324 KB Output is correct
19 Correct 144 ms 232416 KB Output is correct
20 Correct 167 ms 245388 KB Output is correct
21 Correct 158 ms 245324 KB Output is correct
22 Correct 153 ms 245352 KB Output is correct
23 Correct 166 ms 245280 KB Output is correct
24 Correct 155 ms 245368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 3 ms 3540 KB Output is correct
12 Correct 2 ms 3680 KB Output is correct
13 Correct 2 ms 3796 KB Output is correct
14 Correct 2 ms 3540 KB Output is correct
15 Correct 2 ms 3796 KB Output is correct
16 Correct 151 ms 214500 KB Output is correct
17 Correct 149 ms 242580 KB Output is correct
18 Correct 157 ms 245324 KB Output is correct
19 Correct 144 ms 232416 KB Output is correct
20 Correct 167 ms 245388 KB Output is correct
21 Correct 158 ms 245324 KB Output is correct
22 Correct 153 ms 245352 KB Output is correct
23 Correct 166 ms 245280 KB Output is correct
24 Correct 155 ms 245368 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 162 ms 229840 KB Output is correct
28 Correct 162 ms 245432 KB Output is correct
29 Correct 167 ms 245424 KB Output is correct
30 Correct 163 ms 245392 KB Output is correct
31 Correct 151 ms 230368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Runtime error 421 ms 1048576 KB Execution killed with signal 9
5 Halted 0 ms 0 KB -