#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define pb push_back
#define sz(a) (ll) a.size()
#define all(x) (x).begin(), (x).end()
#define rep(i, a, b) for(ll i=a; i<b; i++)
#define rrep(i, a, b) for(ll i=a; i>=b; i--)
#define vl vector<ll>
#define vpll vector<pair<ll, ll>>
#define vvl vector<vector<ll>>
#define pll pair<ll, ll>
ll n;
vl l, r, dpl, dpr, best;
void f() {
cin >> n;
l.resize(n);
r.resize(n);
rep(i, 0, n) cin >> l[i] >> r[i];
dpl.assign(n, 0);
dpr.assign(n, 0);
best.assign(n, 0);
dpl[0]=1;
best[0]=1;
rep(i, 1, n){
dpl[i]=1;
rep(j, max(0LL, i-2), i-l[i]) if(j+r[j]<i) dpl[i]=max(dpl[i], dpl[j]+1);
if(i-3>=0) dpl[i]=max(dpl[i], best[i-3]+1);
best[i]=max(best[i-1], dpl[i]);
}
dpr[n-1]=1;
best.assign(n, 0);
best[n-1]=1;
rrep(i, n-2, 0){
dpr[i]=1;
rep(j, i+r[i]+1, min(n-1, i+2)+1) if(j-l[j]>i) dpr[i]=max(dpr[i], dpr[j]+1);
if(i+3<n) dpr[i]=max(dpr[i], best[i+3]+1);
best[i]=max(best[i+1], dpr[i]);
}
ll ans=max(dpl[n-1], dpr[0]);
rep(i, 1, n-1) ans=max(ans, dpl[i]+dpr[i]-1);
cout << ans;
}
int main() {
int tc = 1;
// cin >> tc;
for (int i = 1; i <= tc; i++) {
// cout << '#' << i << endl;
f();
if (i != tc) cout << endl;
}
}
# | 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... |