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;
const int maxn = 2505;
int n, q;
int l[maxn];
int r[maxn];
int dp[maxn][maxn];
int f(int from, int to)
{
if (from == 1 && to == n)
{
return 0;
}
if (dp[from][to] != 0)
{
return dp[from][to];
}
int result = maxn;
for (int pos = from; pos <= to; ++pos)
{
if (l[pos] < from || r[pos] > to)
{
result = min(result, 1 + f(min(from, l[pos]), max(to, r[pos])));
}
}
return dp[from][to] = result;
}
void fastIO()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int main()
{
fastIO();
cin >> n;
for (int i = 1; i <= n; ++i)
{
cin >> l[i] >> r[i];
}
cin >> q;
for (int i = 1; i <= q; ++i)
{
int start;
cin >> start;
int ans = f(start, start);
if (ans == 0 || ans > n)
{
cout << -1 << "\n";
}
else
{
cout << ans << "\n";
}
}
return 0;
}
# | 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... |