| # | Time | Username | Problem | Language | Result | Execution time | Memory |
|---|---|---|---|---|---|---|---|
| 1308127 | thuhienne | Passport (JOI23_passport) | C++20 | 21 ms | 4416 KiB |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define thuhien ""
#define re exit(0);
const int inf = 1e9;
int n;
int l[200009],r[200009];
int minl[2009][2009],maxr[2009][2009],dp[2009][2009];
pair <int,int> uni(pair <int,int> a,pair <int,int> b) {
return {min(a.first,b.first),max(a.second,b.second)};
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(nullptr);
if (fopen(thuhien".inp","r")) {
freopen(thuhien".inp","r",stdin);
freopen(thuhien".out","w",stdout);
}
cin >> n;
for (int i = 1;i <= n;i++) {
cin >> l[i] >> r[i];
}
for (int i = n;i >= 1;i--) {
minl[i][i] = maxr[i][i] = i;
for (int j = i + 1;j <= n;j++) {
int a = minl[i][j - 1];
if (l[a] == l[j]) {
minl[i][j] = r[a] > r[j] ? a : j;
} else {
minl[i][j] = l[a] < l[j] ? a : j;
}
a = maxr[i][j - 1];
if (r[a] == r[j]) {
maxr[i][j] = l[a] < l[j] ? a : j;
} else {
maxr[i][j] = r[a] > r[j] ? a : j;
}
}
}
for (int len = n - 1;len >= 1;len--) {
for (int i = 1;i + len - 1 <= n;i++) {
int left = i,right = i + len - 1;
dp[left][right] = inf;
int pos = minl[left][right];
pair <int,int> temp = uni({left,right},{l[pos],r[pos]});
dp[left][right] = min(dp[left][right],dp[temp.first][temp.second] + 1);
pos = maxr[left][right];
temp = uni({left,right},{l[pos],r[pos]});
dp[left][right] = min(dp[left][right],dp[temp.first][temp.second] + 1);
}
}
int q;cin >> q;while (q--) {
int a;cin >> a;
cout << (dp[a][a] > n ? -1 : dp[a][a]) << '\n';
}
}
Compilation message (stderr)
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
