#include <bits/stdc++.h>
using namespace std;
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("Ofast,unroll-loops,fast-math,O3")
#define pb push_back
#define ll long long
#define f first
#define s second
//#define sz(v) int(v.size())
#define all(v) v.begin(),v.end()
int mod = 1e9 + 7;
const int N = 2e5,k = 317 + 1;
const int inf = 2e9;
void solve(){
int n;
cin >> n;
vector <pair<int,int>> v;
int cnt = 0,cnt1 = 0;
for(int i = 1; i <= n; i++){
int x,y;
cin >> x >> y;
if(x == i) cnt1++;
if(y == 1) cnt++;
v.pb({x,y});
}
if(cnt == n){
cout << n;
return;
}
if(cnt1 == n){
int ans = 0;
for(int i = 0; i < v.size(); i++){
if(v[i].s == 1){
ans++;
if(i - 1 >= 0 && v[i - 1].s == 0){
v[i - 1].s = -1;
}
if(i + 1 < n && v[i + 1].s == 0){
v[i + 1].s = -1;
}
v[i].s = -1;
}
}
for(int i = 0; i < v.size(); i++) if(v[i].s != -1) ans++;
cout << ans << '\n';
return;
}
if(n <= 20){
int ans = inf;
for(int mask = 0; mask < (1 << n); mask++){
vector <int> v1;
for(int i = 0; i < n; i++){
if((mask >> i & 1) == 1){
v1.pb(i);
}
}
bool f1 = 1;
for(int i = 0; i < n; i++){
int x,y;
bool f = 0;
for(auto to : v1){
x = v[to].f,y = v[to].s;
if(abs(x - v[i].f) <= y - v[i].s){
f = 1;
break;
}
}
if(f == 0){
f1 = 0;
}
}
if(f1 == 1){
ans = min(ans,__builtin_popcount(mask));
}
}
cout << ans;
}
}
signed main(){
//freopen("time.in", "r", stdin);
//freopen("time.out", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int t = 1;
while(t--){
solve();
}
}
| # | 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... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |