D. Yet Another Sorting Problem (偶排列)

奇偶排列:
一个排列中,如果交换两个数,那么它排列的奇偶性一定发生变化。
参考:百度-奇排列-性质1

(1)考虑数组中的数两两不相同:比较当前数组与不下降数组之间排列奇偶性。如果它们排列的奇偶性相同,可以变化,否则不行。具体请自行证明。

(2)数组中有相同的数:数组逆序对奇偶性可以随意转换,就一定能转换为不下降子序列。

逆序对树:acwing板子

// Problem: D. Yet Another Sorting Problem
// Contest: Codeforces - Codeforces Round #759 (Div. 2, based on Technocup 2022 Elimination Round 3)
// URL: https://codeforces.com/contest/1591/problem/D
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h> 
using namespace std;
#define ff first
#define ss second
#define lowbit(x) (x&-x)
#define pf(a) printf("%d\n",a)
#define mem(x,y) memset(x,y,sizeof(x))
#define dbg(x) cout << #x << " = " << x << endl
#define rep(i,l,r) for(int i = l; i <= r; i++)
#define fep(i,a,b) for(int i=b; i>=a; --i)
#define PII pair<int,int>
typedef long long ll;

const int N = 1e6+10;

int n,m;
int a[N], b[N];
ll cnt;

void merge_sort(int a[], int l, int r)
{
	if(l>=r) return;
	int mid = (l+r)>>1;
	merge_sort(a,l,mid);
	merge_sort(a,mid+1,r);
	int i = l, j = mid + 1;
	for(int k = l; k <= r; k++) {
		if(j>r || (i<=mid && a[i]<=a[j])) b[k] = a[i++];
		else cnt += mid - i + 1, b[k] = a[j++];
	}
	for(int k = l; k <= r; k++) a[k] = b[k];
}

void solve()
{
	cnt=0;
	scanf("%d",&n);
	rep(i,1,n) {
		scanf("%d",&a[i]);
		b[i] = a[i];
	}
	sort(b+1,b+1+n);
	rep(i,1,n){
		if(b[i-1]==b[i]) {
			puts("YES");
			return;
		} 
	}
	merge_sort(a,1,n);
	puts(cnt&1?"NO":"YES");
}

int main()
{
	int t;
	scanf("%d", &t);
	while(t--)
		solve();
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值