首页 > 其他分享 >Codeforces Round #672 (Div. 2) Problem A

Codeforces Round #672 (Div. 2) Problem A

时间:2022-11-18 11:03:03浏览次数:44  
标签:cubes int Codeforces long mid num test Div Problem


今日份的题目。(指9月24日,因为比赛从晚上10点半持续到12点半)
问题A是水题,题面如下(大半夜了,就不翻译了,赶着睡觉)(其他题目明天再发)
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

3
5
5 3 2 1 4
6
2 2 2 2 2 2
2
2 1

Output

YES
YES
NO

实际上就是求逆序对。
主要运用的数学定理是,任意一个排列经过和逆序对数等同次数的相邻元素交换,可以得到有序的排列。
所以就是求逆序对的数量,再和n(n-1)/2-1做比较即可。
同时注意一下取值范围。
代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 500005
using namespace std;
int T;
int n;
long long aim;
int num[maxn];
int r_num[maxn],l_num[maxn];
long long merge(int l,int r){
if(l==r){
return 0;
}
int mid=(l+r)>>1;
long long ans=0;
ans=merge(l,mid)+merge(mid+1,r);
int lind=0,rind=0;
for(int i=l;i<=mid;i++){
l_num[i-l]=num[i];
}
for(int i=mid+1;i<=r;i++){
r_num[i-mid-1]=num[i];
}
for(int i=l;i<=r;i++){
if(lind>(mid-l)){
num[i]=r_num[rind++];
}else if(rind>(r-mid-1)){
num[i]=l_num[lind++];
ans+=rind;
}else if(l_num[lind]<=r_num[rind]){
num[i]=l_num[lind++];
ans+=rind;
}else{
num[i]=r_num[rind++];
}
}
return ans;
}
int main(){
scanf("%d",&T);
while(T--){
scanf("%d",&n);
aim=1LL*n*(n-1)/2-1;
for(int i=0;i<n;i++){
scanf("%d",&num[i]);
}
long long step=merge(0,n-1);
// printf("%d ",step);
if(step<=aim){
printf("YES\n");
}else{
printf("NO\n");
}
}
return 0;
}


标签:cubes,int,Codeforces,long,mid,num,test,Div,Problem
From: https://blog.51cto.com/u_9368800/5867931

相关文章

  • codeforces 2000左右dp题目练习
    栾巨的dp题单,刷完他要v我500可以提升dp水平。1.YaroslavandTwoStrings白给题,令\(f_{i,0/1,0/1}\)表示前\(i\)位,有无小于,有无大于,根据每位的字符转移即可。2.To......
  • SVG Line Between Divs (multi-point)
     <!doctypehtml><html><head><metacharset="utf-8"><title>SVGLineBetweenDivs(multi-point)</title><style>html,body{margin:0;padding:0;}......
  • Codeforces Round #828 (Div. 3) A~F
    A签到点击查看代码#include<bits/stdc++.h>#definelllonglongusingnamespacestd;constintN=2e5+10;intn;map<int,char>m;inta[N];chars[N];i......
  • Codeforces 1646 D. Weight the Tree
    题意给你n个节点的树,让你给每个节点进行赋值,并且赋的值需要为正整数;同时当一个节点的值等于所有邻居节点的值的和时,这个点为好点;求出一组赋值情况,满足树的好点个数最大......
  • WEB安全DIV+CSS基础
    0x001DIV+CSS介绍层叠样式表(英文全称:CascadingStyleSheets)是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标准通用标记语言的一个子集)等文件样式的计算机......
  • CodeForces - 372C Watching Fireworks is Fun
    题意:有n个点,其中m个要放烟花。每个放烟花的点有属性b[i],放的时间t[i]和位置a[i]。假设放烟花的时候你在位置x,那么可以获得快乐b[i]-|x-a[i]|。你走来走去地看烟花,起始位置......
  • Codeforces Round #833 (Div. 2)-B
    B题目链接:https://codeforces.com/contest/1748/problem/B Whatisthemaximumpossiblelengthadiversestring?100!10个数字每个出现10次暴力n*102下见代码#......
  • B. Diverse Substrings
    题目链接:Problem-B-Codeforces输入71727741010501100639999652345618789987887987998798输出1210121015106题目大意就是给出T个用例给出一个长度为n,只包含'0'......
  • Codeforces Gym 100958 E Cellular Automaton (Makoto rng_58 Soejima contest) 题解
    题目链接其实"序列中1的数量有限"是一个非常重要的条件。这意味着我们可以找到序列中的第一个1和最后一个1。考虑这样一件事情:初始时我们把一个长度为\(2^{2w+1}\)的"滑......
  • vue3点击其他元素隐藏固定DIV
    vue3点击其他元素隐藏固定DIV显示的内容<divv-if="hSearch"ref="iscity"><div><inputclass="h-9w-full"placeholder="内容搜索..."/></div></div>元......