博客
关于我
Codeforces Round #672 (Div. 2) 1420A 【思维】 题解
阅读量:329 次
发布时间:2019-03-04

本文共 2657 字,大约阅读时间需要 8 分钟。

前言

欢迎大家访问林深时不见鹿的博客,本博客致力于最通俗易懂的题解,如有不足, 还请多多指教。

目录

CodeForces - 1420A

For god’s sake, you’re boxes with legs! It is literally your only purpose! Walking onto buttons! How can you not do the one thing you were designed for?
Oh, that’s funny, is it? Oh it’s funny? Because we’ve been at this for twelve hours and you haven’t solved it either, so I don’t know why you’re laughing. You’ve got one hour! Solve it!

Wheatley decided to try to make a test chamber. He made a nice test chamber, but there was only one detail absent — cubes.

For completing the chamber Wheatley needs n cubes. i-th cube has a volume ai.

Wheatley has to place cubes in such a way that they would be sorted in a non-decreasing order by their volume. Formally, for each i>1, ai−1≤ai must hold.

To achieve his goal, Wheatley can exchange two neighbouring cubes. It means that for any i>1 you can exchange cubes on positions i−1 and i.

But there is a problem: Wheatley is very impatient. If Wheatley needs more than n⋅(n−1)2−1 exchange operations, he won’t do this boring work.

Wheatly wants to know: can cubes be sorted under this conditions?

Input

Each test contains multiple test cases.

The first line contains one positive integer t (1≤t≤1000), denoting the number of test cases. Description of the test cases follows.

The first line of each test case contains one positive integer n (2≤n≤5⋅104) — number of cubes.

The second line contains n positive integers ai (1≤ai≤109) — volumes of cubes.

It is guaranteed that the sum of n over all test cases does not exceed 105.

Output

For each test case, print a word in a single line: “YES” (without quotation marks) if the cubes can be sorted and “NO” (without quotation marks) otherwise.

Example

Input

355 3 2 1 462 2 2 2 2 222 1

Output

YESYESNO

Note

In the first test case it is possible to sort all the cubes in 7 exchanges.

In the second test case the cubes are already sorted.

In the third test case we can make 0 exchanges, but the cubes are not sorted yet, so the answer is “NO”.

1.题意

给你一个数组,你可以每次交换相邻两个数,使得该数组单调不下降,如果你交换的次数超过 n*(n-1)/2 -1 输出YES,否则输出 NO。

2.思路

这道题就是求解冒泡排序交换的次数,但你如果直接用冒泡排序去求解的话,一个样例的时间复杂度是O(n),1000个样例肯定会超时的。当一个数组严格单调下降时,即冒泡排序最坏的情况下交换次数为 n-1+n-2+n-3+…+ 2+1 即n*(n-1)/2 ,我们可以发现和题目要求的次数n*(n-1)/2 -1正好差一。因此只要数组不是严格单调下降数组都可以满足要求。

3.代码

#include
#include
#include
using namespace std;const int N = 5e4 + 10;int a[N];int main(){ int T; cin >> T; while (T--) { int n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; int flag = 1; for (int i = 0; i < (n-1); i++) { if (a[i] <= a[i + 1]) { flag = 0; break; } } if(flag==0) puts("YES"); else puts("NO"); } return 0;}

转载地址:http://txrh.baihongyu.com/

你可能感兴趣的文章
Remove function
查看>>
在没实践机会的前提下,如何跨越级别
查看>>
从面试官角度告诉大家如何准备项目方面的描述
查看>>
架构师入门:搭建基本的Eureka架构(从项目里抽取)
查看>>
Java核心技术及面试指南 流程控制方面的面试题答案
查看>>
MongoDB 快速扫盲贴
查看>>
修复搜狗、360等浏览器不识别SameSite=None 引起的单点登录故障
查看>>
明天要早起,今天不博了。
查看>>
2017/08/21 工作日志
查看>>
EXTJS4.2——10.Tab+Iframe
查看>>
EXTJS4.2——3.1 添加文本框
查看>>
WEB基础——AJAX
查看>>
one + two = 3
查看>>
Kali Day01 --- arpspoof命令进行断网攻击(ARP欺骗)
查看>>
echart关系图平分节点删除时自动平衡问题
查看>>
【Coursera】Internet History 读书笔记
查看>>
《ODAY安全:软件漏洞分析技术》学习心得-----shellcode的一点小小的思考
查看>>
PHP serialize && unserialize Security Risk Research
查看>>
Deformity ASP/ASPX Webshell、Webshell Hidden Learning
查看>>
Decision tree(决策树)算法初探
查看>>