답안 #807202

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
807202 2023-08-04T14:48:04 Z rnl42 Passport (JOI23_passport) C++14
0 / 100
438 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]);
            }
        }
    }
    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]);
            }
            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]);
            }
            int val = (l == 0 && r == N-1 ? 0 : 1e18);
            for (int i = l; i <= r; i++) {
                val = min(val, 1+dp[min(L[i], l)][max(r, R[i])]);
            }
            dbg(l, r, val, dp[l][r]);
            assert(val == dp[l][r]);
        }
    }
    for (int i = 0; i < Q; i++) {
        cout << (dp[X[i]][X[i]] >= 1e18 ? -1 : dp[X[i]][X[i]]) << '\n';
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 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 438 ms 1048576 KB Execution killed with signal 9
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 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 0 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 1 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 1 ms 212 KB Output is correct
11 Correct 8 ms 3540 KB Output is correct
12 Runtime error 8 ms 7460 KB Execution killed with signal 6
13 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 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 0 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 1 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 1 ms 212 KB Output is correct
11 Correct 8 ms 3540 KB Output is correct
12 Runtime error 8 ms 7460 KB Execution killed with signal 6
13 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 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 0 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 1 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 1 ms 212 KB Output is correct
11 Correct 8 ms 3540 KB Output is correct
12 Runtime error 8 ms 7460 KB Execution killed with signal 6
13 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 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 438 ms 1048576 KB Execution killed with signal 9
5 Halted 0 ms 0 KB -