답안 #807139

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
807139 2023-08-04T14:01:47 Z rnl42 Passport (JOI23_passport) C++14
0 / 100
1 ms 468 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, -1);
        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]);
            }
        }
        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] >= 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';
    }
}
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 468 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 340 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 340 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 340 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 468 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -