This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define ar array
#define pb push_back
#define ln '\n'
#define int long long
using i64 = long long;
template <class F, class _S>
bool chmin(F &u, const _S &v){
bool flag = false;
if ( u > v ){
u = v; flag |= true;
}
return flag;
}
template <class F, class _S>
bool chmax(F &u, const _S &v){
bool flag = false;
if ( u < v ){
u = v; flag |= true;
}
return flag;
}
const int inf = 1e15 + 1;
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n; cin >> n;
vector <int> L(n), R(n);
for ( int i = 0; i < n; i++ ){
cin >> L[i] >> R[i];
--L[i], --R[i];
}
int q; cin >> q;
vector <int> qu(q), p(q);
for ( auto &x: qu ){
cin >> x;
--x;
}
iota(all(p), 0);
sort(all(p), [&](int &x, int &y){
return R[qu[x]] - L[qu[x]] > R[qu[y]] - L[qu[y]];
});
vector <vector<int>> dp(n, vector <int> (n, -1));
dp[0][n - 1] = 0;
auto dfs = [&](auto dfs, int l, int r) -> int{
auto &ret = dp[l][r];
if ( ret != -1 ){
return ret;
} ret = inf;
for ( int i = l; i <= r; i++ ){
if ( L[i] < l || R[i] > r ){
chmin(ret, dfs(dfs, min(l, L[i]), max(r, R[i])) + 1);
}
}
return ret;
};
vector <int> ans(q);
for ( auto &i: p ){
int s = qu[i];
if ( !s && q == 1 ){
int ans = 0, lst = -1;
bool flag = true;
priority_queue <int> q;
q.push(R[s]);
for ( int i = 1; i < n; i++ ){
if ( lst < i ){
if ( q.empty() || q.top() < i ){
flag = false;
break;
}
lst = q.top();
q.pop(); ++ans;
} q.push(R[i]);
}
cout << (flag ? ans : -1) << ln;
return 0;
}
ans[i] = dfs(dfs, L[s], R[s]);
}
for ( auto &x: ans ){
cout << (x == inf ? -1 : x + 1) << ln;
}
cout << '\n';
}
# | 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... |