Codeforces Round #672 (Div. 2)------Cubes Sorting

46 篇文章 0 订阅
26 篇文章 0 订阅

题目

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.

题意

给定你一个数组,请你判定按照冒泡排序的思想,能否交换(n * (n - 1) / 2) - 1以内完成排序

题解

这题是一个思维题
下面介绍思维的思路:
首先冒泡排序的最坏的情况交换次数是n * (n - 1) / 2,所以对比题目的条件,只要数组不是纯单调递减序列即可在要求内完成。
还有一种就是相对暴力的方法,本着锻炼算法的能力写的,冒泡排序的交换次数即求出逆序对的数目,此处运用归并排序求逆序对数目

AC代码(思维)

#include <iostream>
using namespace std;
const int N = 5e4 + 15;
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 = 0;
        for(int i = 1; i < n; i++){
            if(a[i] >= a[i - 1]){
                flag = 1;
                break;
            }
        }
        if(flag)
            cout << "YES" << endl;
        else
            cout << "NO" << endl;
    }
    return 0;
}

AC代码(归并排序求逆序对)

#include<iostream>
#include<string.h>
using namespace std;
#define ll long long
const ll N = 5e4 +15;
ll ans = 0;
void merge(ll sourcerArr[], ll tempArr[], ll s, ll mid, ll e){
    ll i = s, j = mid + 1, k = s;
    while(i != mid + 1 && j != e + 1){
        if(sourcerArr[i] > sourcerArr[j])
            tempArr[k++] = sourcerArr[j++], ans += mid - i + 1;
        else
            tempArr[k++] = sourcerArr[i++];
    }
    while (i != mid + 1)
        tempArr[k++] = sourcerArr[i++];
    while (j != e + 1)
        tempArr[k++] =  sourcerArr[j++];
    for(ll i = s; i <= e; i++)
        sourcerArr[i] = tempArr[i];
}
void mergeSort(ll sourcerArr[], ll tempArr[], ll s, ll e){
    if(s >= e)
        return;
    ll mid = (s + e) / 2;
    mergeSort(sourcerArr, tempArr, s, mid);
    mergeSort(sourcerArr, tempArr, mid +1, e);
    merge(sourcerArr, tempArr, s, mid, e);
}
ll a[N], b[N];
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    std::cout.tie(0);
    ll t; cin >> t;
    while(t--){
        memset(a, 0, sizeof(a));
        memset(b, 0, sizeof(b));
        ll n; cin >> n;
        ans = 0;
        for(ll i =0; i < n; i++)
            cin >> a[i];
        mergeSort(a, b, 0, n - 1);
        ll x = (n * (n - 1) / 2) - 1;
        if(ans <= x)
            cout << "YES" << endl;
        else
            cout << "NO" << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值