#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)
{
cout << -1 << "\n";
}
else
{
cout << ans << "\n";
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
468 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
468 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |