#include <bits/stdc++.h>
using namespace std;
template<class T>
int len(T &a){
return a.size();
}
using ll = long long;
using pii = pair<int,int>;
#define all(a) (a).begin(), (a).end()
#define ff first
#define ss second
string fileio = "";
mt19937 mt(time(nullptr));
const int mod = 1e9 + 7;
const int inf = 1e9;
const ll infl = 1e18;
const int maxn = 1e5 + 1;
struct Node{
int l, r, i;
};
struct Sparsemin{
vector<vector<Node>> t;
int n, logn;
void init(int _n){
n = _n;
logn = 32 - __builtin_clz(n);
t.assign(logn, vector<Node> (n));
}
Node merge(Node a, Node b){
if(a.l < b.l) return a;
if(b.l < a.l) return b;
if(a.r > b.r) return a;
if(b.r > a.r) return b;
if(a.i < b.i) return a;
return b;
}
void build(vector<int> &l, vector<int> &r){
init(len(l));
for(int i = 0; i < n; i ++){
t[0][i] = {l[i], r[i], i};
}
for(int j = 1; j < logn; j ++){
for(int i = 0; i + (1 << j) - 1 < n; i ++){
t[j][i] = merge(t[j - 1][i], t[j - 1][i + (1 << (j - 1))]);
}
}
}
int get(int l, int r){
int k = 31 - __builtin_clz(r - l + 1);
return merge(t[k][l], t[k][l + (1 << k)]).i;
}
};
struct Sparsemax{
vector<vector<Node>> t;
int n, logn;
void init(int _n){
n = _n;
logn = 32 - __builtin_clz(n);
t.assign(logn, vector<Node> (n));
}
Node merge(Node a, Node b){
if(a.r > b.r) return a;
if(b.r > a.r) return b;
if(a.l < b.l) return a;
if(b.l < a.l) return b;
if(a.i > b.i) return a;
return b;
}
void build(vector<int> &l, vector<int> &r){
init(len(l));
for(int i = 0; i < n; i ++){
t[0][i] = {l[i], r[i], i};
}
for(int j = 1; j < logn; j ++){
for(int i = 0; i + (1 << j) - 1 < n; i ++){
t[j][i] = merge(t[j - 1][i], t[j - 1][i + (1 << (j - 1))]);
}
}
}
int get(int l, int r){
int k = 31 - __builtin_clz(r - l + 1);
cout << l << ' ' << r << ' ' << k << endl;
return merge(t[k][l], t[k][l + (1 << k)]).i;
}
};
void solve(){
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] --;
}
vector<int> rig(n, -1);
auto dp = [&](auto &dp, int i)->int{
if(r[i] == n - 1) return 1;
//cout << i << endl;
if(~rig[i]) return rig[i];
int res = inf;
for(int j = r[i]; j >= l[i]; j --){
if(j != i) res = min(res, dp(dp, j) + 1);
}
return rig[i] = res;
};
cout << dp(dp, 0);
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
auto start = chrono::high_resolution_clock::now();
#ifndef LOCAL
if(fileio.size()){
freopen((fileio + ".in").c_str(), "r", stdin);
freopen((fileio + ".out").c_str(), "w", stdout);
}
#endif
int testcases = 1;
#ifdef Tests
cin >> testcases;
#endif
while(testcases --){
solve();
cout << '\n';
#ifdef LOCAL
cout << "_________________________" << endl;
#endif
}
#ifdef LOCAL
auto duration = chrono::duration_cast<chrono::microseconds>(chrono::high_resolution_clock::now() - start);
cout << "time: " << (double)duration.count()/1000.0 << " milliseconds" << endl;
#endif
return 0;
}
Compilation message
passport.cpp: In function 'int main()':
passport.cpp:146:10: warning: variable 'start' set but not used [-Wunused-but-set-variable]
146 | auto start = chrono::high_resolution_clock::now();
| ^~~~~
passport.cpp:149:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
149 | freopen((fileio + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
passport.cpp:150:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
150 | freopen((fileio + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
456 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
456 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |