这道题考了逆元知识,有个很重要的结论:逆序数对数==把这个数列排成递增序列所需要的次数(每次只能换相邻的)
有了这个结论就OK了。
#include <stdio.h>
#include<map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include <iostream>
#define LL long long
#define _LL __int64
using namespace std;
typedef long long ll;
const ll maxn = 100100;
ll cnt;
ll n,a[maxn],t[maxn],x,y;
void merge_sort(ll *a, ll x, ll y, ll *t)//求逆序数对数板子 a为数组,区间[x,y),t用来存原递增序列
{
if(y-x > 1)
{
ll m = x + (y-x)/2;
ll p = x,q = m,i = x;
merge_sort(a,x,m,t);
merge_sort(a,m,y,t);
while(p < m || q < y)
{
if(q >= y || (p < m && a[p] <= a[q]))
t[i++] = a[p++];
else
{
t[i++] = a[q++];
cnt += m-p;
}
}
for(i = x; i < y; i++)
a[i] = t[i];
}
}
int main()
{
while(~scanf("%lld %lld %lld",&n,&x,&y))//这里千万要~ 不然返回-1的时候就不会结束了
{
memset(a,0,sizeof(a));
memset(t,0,sizeof(t));
ll Min=min(x,y);
for(ll i = 0; i < n; i++)
scanf("%lld",&a[i]);
cnt = 0;
merge_sort(a,0,n,t);//求逆序数
printf("%lld\n",cnt*Min);//这里根据题的意思可以知道就是cnt*Min值
}
return 0;
}