AGC003 C-BBuBBBlesort! (Thinking, parity)

Title:

Insert picture description here

solution:

将数组离散化,因为每个数互不相同,因此数组会变成排列.
那么最后数值i就要放到下标i的位置.

对于操作2,其实就是相同奇偶性的数的位置可以任意变换.
因此对于操作1,目的是为了改变操作2无法修改的奇偶性,
例如a[2]=3,23的奇偶性不同,需要用操作1修改.

因此记录又多少个位置,a[i]%2!=i%2,设一共有cnt个,那么答案为cnt/2,
因为一次操作1修改了两个数的奇偶性.

code:

#include <bits/stdc++.h>
#define int long long
#define PI pair<int,int>
using namespace std;
const int maxm=2e6+5;
int a[maxm];
int b[maxm];
int n;
void solve(){
    
    
    cin>>n;
    for(int i=1;i<=n;i++){
    
    
        cin>>a[i];
        b[i]=a[i];
    }
    sort(b+1,b+1+n);
    for(int i=1;i<=n;i++){
    
    
        a[i]=lower_bound(b+1,b+1+n,a[i])-b;
    }
    int ans=0;
    for(int i=1;i<=n;i++){
    
    
        if(a[i]%2!=i%2){
    
    
            ans++;
        }
    }
    cout<<ans/2<<endl;
}
signed main(){
    
    
    ios::sync_with_stdio(0);
    solve();
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_44178736/article/details/114854194