이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 > 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... |