#include<bits/stdc++.h>
#define taskname "C"
typedef long long ll;
using namespace std;
template<class T>bool minimize(T& a, T b){
if(a > b){
a = b;
return true;
}
return false;
}
template<class T>void maximize(T& a, T b){
if(a < b){
a = b;
}
}
const int INF = 1e9;
int n, q;
namespace sub2{
void solve(){
vector<int>L(n + 1), R(n + 1);
for(int i = 1; i <= n; i++){
cin >> L[i] >> R[i];
}
vector<vector<int>>dp(n + 1, vector<int>(n + 1, INF));
dp[1][n] = 0;
for(int len = n - 2; len > -1; len--){
for(int l = 1; true; l++){
int r = l + len;
if(r > n){
break;
}
for(int i = l; i <= r; i++){
minimize(dp[l][r], dp[min(L[i], l)][max(R[i], r)] + 1);
}
}
}
cin >> q;
for(int _ = 0; _ < q; _++){
int x;
cin >> x;
cout << (dp[x][x] == INF ? -1 : dp[x][x]) << "\n";
}
}
}
namespace sub3{
struct Data{
int x, y;
bool w;
Data(){}
Data(int x, int y, bool w){
this->x = x;
this->y = y;
this->w = w;
}
};
void solve(){
vector<int>L(n + 1), R(n + 1);
for(int i = 1; i <= n; i++){
cin >> L[i] >> R[i];
}
vector<vector<vector<Data>>>g(n + 1, vector<vector<Data>>(n + 1));
for(int l = 1; l <= n; l++){
int bl = L[l], br = R[l];
g[bl][br].push_back(Data(l, l, true));
for(int r = l + 1; r <= n; r++){
minimize(bl, L[r]);
maximize(br, R[r]);
g[bl][r].push_back(Data(l, r, true));
g[l][br].push_back(Data(l, r, true));
g[l + 1][r].push_back(Data(l, r, false));
g[l][r - 1].push_back(Data(l, r, false));
}
}
vector<vector<int>>f(n + 1, vector<int>(n + 1, INF));
deque<pair<int, int>>d;
d.push_back(make_pair(1, n));
f[1][n] = 0;
while(!d.empty()){
int x, y;
tie(x, y) = d.front();
d.pop_front();
for(Data& v : g[x][y]){
if(minimize(f[v.x][v.y], f[x][y] + int(v.w))){
if(v.w){
d.push_back(make_pair(v.x, v.y));
}
else{
d.push_front(make_pair(v.x, v.y));
}
}
}
}
cin >> q;
for(int _ = 0; _ < q; _++){
int x;
cin >> x;
cout << (f[x][x] == INF ? -1 : f[x][x]) << "\n";
}
}
}
namespace sub145{
void solve(){
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if(fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
}
cin >> n;
if(n <= 300){
sub2::solve();
}
else if(n <= 2500){
sub3::solve();
}
else{
sub145::solve();
}
}
컴파일 시 표준 에러 (stderr) 메시지
passport.cpp: In function 'int main()':
passport.cpp:110:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
110 | freopen(taskname".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~| # | 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... |