【CF1591】【数组数组】【逆序对】#759(div2)D. Yet Another Sorting Problem
题目:Problem - D - Codeforces
题解
此题是给数组排序的题,操作是选取任意三个数,然后交换他们,确保他们的位置会发生改变。
可以交换无限次,最终可以形成一个不下降序列就输出“YES”,否则“NO”。
只需要注意以下两点即可解出此题:
1.如果数组中存在两个相同的元素,那么就一定满足题意,输出“YES”
(因为这样就可以移动任意第三个数且保证另外两个相同的数位置不变)
2.因为不下降序列的逆序对数量为0,且每次进行交换操作一定会改变偶数个逆序对,因此当逆序对数量为偶数时,满足题意
方法:
我是用map容器检验是否有重复元素,用树状数组和离散化来求逆序对。
注意树状数组每次要初始化,且初始化范围为每次的n,不要全部初始化,会超时。
(map也要初始化)
代码
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
typedef long long ll;
const int N = 5e5 + 10;
int n;
int tree[N];
map<int, int> vis;
struct node
{
int val, id;
} a[N];
void add(int i, int v)
{
while (i <= n)
{
tree[i] += v;
i += i & -i;
}
}
ll getsum(int i)
{
ll res = 0;
while (i > 0)
{
res += tree[i];
i -= i & -i;
}
return res;
}
bool cmp(node aa, node bb)
{
return aa.val < bb.val;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T;
cin >> T;
while (T--)
{
vis.clear();
cin >> n;
int f = 0;
for (int i = 1; i <= n; ++i)
{
cin >> a[i].val;
a[i].id = i;
if (vis[a[i].val])
f = 1;
vis[a[i].val] = 1;
}
if (f)
{
cout << "YES" << endl;
continue;
}
sort(a + 1, a + 1 + n, cmp);
ll ans = 0;
for (int i = 1; i <= n; ++i)
{
add(a[i].id, 1);
ans += i - 1 - getsum(a[i].id - 1);
}
if (ans % 2 == 0)
cout << "YES" << endl;
else
cout << "NO" << endl;
for (int i = 1; i <= n; ++i)
{
tree[i] = 0;
}
}
return 0;
}
【CF1591】【数组数组】【逆序对】#759(div2)D. Yet Another Sorting Problem的更多相关文章
- 2021.12.10 P5041 [HAOI2009]求回文串(树状数组求逆序对)
2021.12.10 P5041 [HAOI2009]求回文串(树状数组求逆序对) https://www.luogu.com.cn/problem/P5041 题意: 给一个字符串 \(S\) ,每 ...
- POJ2299Ultra-QuickSort(归并排序 + 树状数组求逆序对)
树状数组求逆序对 转载http://www.cnblogs.com/shenshuyang/archive/2012/07/14/2591859.html 转载: 树状数组,具体的说是 离散化+树 ...
- HDU 1394 树状数组求逆序对
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- HDU 1394 Minimum Inversion Number (树状数组求逆序对)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题目让你求一个数组,这个数组可以不断把最前面的元素移到最后,让你求其中某个数组中的逆序对最小是多 ...
- poj3067 Japan 树状数组求逆序对
题目链接:http://poj.org/problem?id=3067 题目就是让我们求连线后交点的个数 很容易想到将左端点从小到大排序,如果左端点相同则右端点从小到大排序 那么答案即为逆序对的个数 ...
- SGU180(树状数组,逆序对,离散)
Inversions time limit per test: 0.25 sec. memory limit per test: 4096 KB input: standard output: sta ...
- 牛客练习赛38 D 题 出题人的手环 (离散化+树状数组求逆序对+前缀和)
链接:https://ac.nowcoder.com/acm/contest/358/D来源:牛客网 出题人的手环 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K,其他 ...
- [NOIP2013提高&洛谷P1966]火柴排队 题解(树状数组求逆序对)
[NOIP2013提高&洛谷P1966]火柴排队 Description 涵涵有两盒火柴,每盒装有 n 根火柴,每根火柴都有一个高度. 现在将每盒中的火柴各自排成一列, 同一列火柴的高度互不相 ...
- [NOI导刊2010提高&洛谷P1774]最接近神的人 题解(树状数组求逆序对)
[NOI导刊2010提高&洛谷P1774]最接近神的人 Description 破解了符文之语,小FF开启了通往地下的道路.当他走到最底层时,发现正前方有一扇巨石门,门上雕刻着一幅古代人进行某 ...
- codeforces 459D D. Pashmak and Parmida's problem(离散化+线段树或树状数组求逆序对)
题目链接: D. Pashmak and Parmida's problem time limit per test 3 seconds memory limit per test 256 megab ...
随机推荐
- [luogu4484]最长上升子序列
标算是状压dp+打表,前者时间复杂度为$o(n^{2}2^{n})$,并通过打表做到$o(1)$ 参考loj2265中关于杨表的相关知识,不难发现答案即$\frac{\sum_{a\vdash n}a ...
- bean注解
1.beans.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi=&qu ...
- pycurl报错: ImportError: pycurl: libcurl link-time ssl backend (openssl) is different from compile-time ssl backend
报错: ImportError: pycurl: libcurl link-time ssl backend (openssl) is different from compile-time ssl ...
- MacBookpro安装VMware Fusion虚拟机,并安装win7 64位系统
1.准备好安装用的东西(准备好正确的东西,安装路上就成功了一半)(1)VMware Fusion 附带注册机生成注册码,链接: https://pan.baidu.com/s/13Qm9zPOFjFt ...
- 硬盘SSD、HDD和SSHD都是什么意思?哪种类型硬盘最好?
硬盘分类:(1)HHD 机械硬盘(Mechanical hard disk)(2)SSD 固态硬盘(solid state drive/disk)(3)SSHD 混合硬盘,说白了就是HDD+SSD=S ...
- java四则运算规则
java四则运算规则 1.基本规则 运算符:进行特定操作的符号.例如:+ 表达式:用运算符连起来的式子叫做表达式.例如:20 + 5.又例如:a + b 四则运算: 加:+ 减:- 乘:* 除:/ 取 ...
- 日常Java 2021/9/20
Java随机数 运用Java的random函数实现猜数字游戏 随机产生一个1-50之间的数字,然后让玩家猜数,猜大猜小都给出提示,猜对后游戏停止 package pingchangceshi; imp ...
- A Child's History of England.29
You have not forgotten the New Forest which the Conqueror made, and which the miserable people whose ...
- abundant
In ecology [生态学], local abundance is the relative representation of a species in a particular ecosys ...
- Https原理及证书管理
Https原理及证书管理 SSL(Secure Sockets Layer,安全套接层)/TLS(Transport Layer Security,传输层安全)保证了客户端web服务器的连接安全.客户 ...