题目: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;
}

最新文章

  1. hosts文件的一个小发现
  2. C#并行编程-线程同步原语
  3. 17. 星际争霸之php设计模式--职责链模式
  4. Ubuntu遇到Please ensure that adb is correctly located at &#39;...adb.exe&#39; and can be executed 问题解决方法
  5. ubuntu不能访问windows中的文件
  6. 每日一九度之 题目1030:毕业bg
  7. QQ群里收集的外企iOS开发的笔试题
  8. State of Hyperparameter Selection
  9. angularjs 利用filter进行表单查询及分页查询
  10. QT creator中使用opencv
  11. Android应用程序组件Content Provider在应用程序之间共享数据的原理分析
  12. PHP常用内置函数
  13. Xamarin 手动安装步骤+破解
  14. Visual Studio 单元测试之二---顺序单元测试
  15. CentOS6.5+mysql5.5源码安装
  16. Javascript 判断变量类型的陷阱 与 正确的处理方式
  17. (二)部署solr7.1.0到tomcat
  18. RequireJS中的require返回模块
  19. Python基础-week02
  20. ThinkPHP5+Apicloud+vue商城APP实战

热门文章

  1. python实现其它形态学操作
  2. c语言循环位移(数字,字符串)
  3. html+css第九篇
  4. freeswitch APR库哈希表
  5. Codeforces 985G - Team Players(三元环)
  6. Matlab矢量图图例函数quiverkey
  7. 基因组Denovo组装原理、软件、策略及实施
  8. 学习java第十九天
  9. Hadoop入门 集群常用知识与常用脚本总结
  10. nextcloud搭建私有云盘