dd

· · 个人记录



#include<bits/stdc++.h>
using namespace std;
const int ma = 1e6;
int a[ma];
bool vis[ma];
int main(){
    int n;
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
    }
    for(int i = 1; i <= n; i++){
        if(a[i] == i){
            vis[i] = 1;
        }
    }
    int cnt = 0;
    for(int i = 1; i<= n; i++){
        if(vis[i]){
            if(vis[i + 1] == 1){
                cnt++;
                vis[i + 1] = 0;
                vis[i] = 0;
            }else{
                if(a[i - 1] + 1 != i){
                    vis[i] = 0;
                    cnt++;
                    int d = a[i];
                    a[i] = a[i - 1];
                    a[i - 1] = d;
                }else if(a[i + 1] - 1 != i){
                    vis[i] = 0;
                    cnt++;
                    int d = a[i];
                    a[i] = a[i + 1];
                    a[i + 1] = d;
                }
            }
        }
    }
    cout << cnt;
    return 0;
}