A - Cubes Sorting
题意
给一个序列,每次可以选择两个相邻的元素进行交换,问交换的次数是否大于
(n-1)*n/2-1.
思路
一个有n个元素的逆序对数量为(n-1)*n/2,也就是说,一个有n个元素的序列,最多交换(n-1)*n/2就可以使序列变得有序,而题目给的交换次数刚好比最大交换次数小1,也就是说明,只要不是完全的从大到小的排序,在(n-1)*n/2-1次数内肯定能排序成从小到大,所有其实就是判断是否有(a[i]<=a[i+1])的情况存在。
代码
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define me memset
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
using namespace std;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
ll a[N];
ll dp[N];
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
for(int i=1 ; i<=n ; i++) cin>>a[i];
bool flag=false;
for(int i=1 ; i<=n-1 ; i++)
{
if(a[i]<=a[i+1]) flag=true;
}
if(flag) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}
总结
这一题不知道n个元素的最大逆序对数量是(n-1)*n/2。