BBuBBBlesort!(思维)

问题 C: BBuBBBlesort!

时间限制: 2 Sec   内存限制: 256 MB
提交: 176   解决: 58
[ 提交][ 状态][ 讨论版][命题人: admin]

题目描述

Snuke got an integer sequence of length N from his mother, as a birthday present. The i-th (1≦i≦N) element of the sequence is ai. The elements are pairwise distinct. He is sorting this sequence in increasing order. With supernatural power, he can perform the following two operations on the sequence in any order:

Operation 1: choose 2 consecutive elements, then reverse the order of those elements.
Operation 2: choose 3 consecutive elements, then reverse the order of those elements.
Snuke likes Operation 2, but not Operation 1. Find the minimum number of Operation 1 that he has to perform in order to sort the sequence in increasing order.

Constraints
1≦N≦105
0≦Ai≦109
If i≠j, then Ai≠Aj.
All input values are integers.

输入

The input is given from Standard Input in the following format:

N
A1
:
AN

输出

Print the minimum number of times Operation 1 that Snuke has to perform.

样例输入

4

2

4

3

1

样例输出

1

提示

The given sequence can be sorted as follows:

First, reverse the order of the last three elements. The sequence is now: 2,1,3,4.
Then, reverse the order of the first two elements. The sequence is now: 1,2,3,4.
In this sequence of operations, Operation 1 is performed once. It is not possible to sort the sequence with less number of Operation 1, thus the answer is 1.

题意:有两种操作,第一种是将相邻的两个元素反转,第二种是将相邻的三个元素反转,尽可能多的使用第二种操作将序列排序,输出最小的第一种操作的使用次数。

题解:对于第二种操作,只能使与自身正确位置相差为偶数的元素排序,若相差为偶数,则只能使用第一种操作排序,可以将元素排序然后离散化,遍历每个元素记录与自身正确位置的差值为奇数的元素个数,答案即为个数/2。

#include<stdio.h>
#include <algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<algorithm>
#define INF 0x3f3f3f3f
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define FAST_IO ios::sync_with_stdio(false)
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MAX=1e5+10;
const int mod=1e9+7;
typedef long long ll;
using namespace std;

inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline ll qpow(ll a,ll b){ll r=1,t=a; while(b){if(b&1)r=(r*t)%mod;b>>=1;t=(t*t)%mod;}return r;}
inline ll inv1(ll b){return qpow(b,mod-2);}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll r=exgcd(b,a%b,y,x);y-=(a/b)*x;return r;}
inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;}

int a[100005],b[100005];
map<int,int>lisan;

int main()
{
    int n;
    cin>>n;

    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        b[i]=a[i];
    }
    sort(b+1,b+n+1);

    for(int i=1;i<=n;i++)
        lisan[b[i]]=i;

    int ans=0;
    for(int i=1;i<=n;i++)
    {
        int temp=abs(i-lisan[a[i]]);
        if(temp%2)
            ans++;
    }

    printf("%d\n",ans/2);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值