https://www.bnuoj.com/v3/problem_show.php?pid=4096
For a sequence A, we define its number of reverse order to meet A[I]>A[J] and i<j order number of <i,j>, such an ordered pair is called reverse.
For example a[0]=1,a[1]=2,a[2]=4,a[3]=5,a[4]=3, there are <2,4> and <3,4> in reverse order, and its reverse number is 2.
Now, give you a sequence of n, which requires just a K-exchange operation, and each interchange can only be performed between adjacent two numbers, and the resulting sequence has the smallest and most likely number of reverse order.
Input
The input data has multiple groups, each set of data consists of two rows, the first row has two integers N (1<=n<=1,000) and K (0<=k<=1,000,000,000), respectively, the length of the sequence and the number of interchange operations that need to be performed.
The second line has n integers, which in turn give all the numbers in the sequence, all within the int range.
The input ends with EOF.
Output
For each set of data, output a single row, consisting of two integers, separated by a space, the first is the smallest inverse value to be performed after the exact k operation, and the second is the maximum inverse value.
Sample Input
5 11 2 3 4 55 11 2 3 5 4
Sample Output
1 10 2
Source2009 Beijing Normal University freshman program design Competition Authorhuang Kun @ BNU (modified from HUST Campus) The key is to think of 1, there is a repeat number 2, reverse the number of the largest is not necessarily n * (n-1)/2 , because there are repeated numbers, 2, 2, 2 such data, is 0 my idea is to first find out the number of reverse order in the beginning, and then compare with K, because it can move K times, according to the optimal scheme, each can increase/subtract 1 reverse order number. So judging the current number of reverse order to now + K and MX relationship can be
#include <cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<algorithm>#defineIOS Ios::sync_with_stdio (False)using namespacestd;#defineINF (0X3F3F3F3F)typedefLong Long intLL; #include<iostream>#include<sstream>#include<vector>#include<Set>#include<map>#include<queue>#include<string>intN, K;Const intMAXN = ++ -;intA[MAXN];intB[maxn];map<int,int>Book ;intCalcinta[]) { intAns =0; for(inti =1; I <= N; ++i) { for(intj = i +1; J <= N; ++j) {if(A[i] > A[j]) + +ans; } } returnans;}BOOLcmpintAintb) {returnA >b;}voidWork () {intBug =0; Book.clear (); for(inti =1; I <= N; ++i) {scanf ("%d", &A[i]); B[i]=A[i]; if(Book[a[i]]) bug =1; Book[a[i]]++; } intnow =Calc (a); Sort (a+1, A +1+N, CMP); intMX =Calc (a); intAnsmi =0; intAnsmx =0; intMxcut =0, Miadd =0; if(n = =1) {printf ("0 0\n"); return; } if(now + k <=mx) {ansmx= Now +K; } Else{ansmx=MX; intleft = Now + K-MX; if(left%2==1&&!bug) Mxcut =1; } if(Now-k >=0) {Ansmi= Now-K; } Else{Ansmi=0; intleft = k-Now ; if(left%2==1&&!bug) Miadd =1; } printf ("%d%d\n", Ansmi + miadd, Ansmx-mxcut);}intMain () {#ifdef local freopen ("Data.txt","R", stdin);#endif while(SCANF ("%d%d", &n, &k)! =EOF) work (); return 0;}
View Code
BNU 4096 Reverse-order thinking problem