把一个序列打乱,问从小到大排序后,需要经过几次交换;
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions:8948 | Accepted: 4747 |
Description
Input
Output
Sample Input
2 3 1 2 3 5 2 3 5 4 1
Sample Output
0 3
代码:
#include<cstdio> #include<cstdlib> #include<iostream> #include<algorithm> #include<string> #include<cstring> #include<cmath> #include<queue> using namespace std; #define sf scanf #define pf printf #define INF 1<<29 #define eps 1e-6 const double PI=acos(-1.0); #define lint __int64 #define LL long long const int tt=10000; #define ULLint unsigned long long //2^64-1>1.8*10^19 #define clr(x) memset(x,0,sizeof(x)) #define Clr(x) memset(x,-1,sizeof(x)) int n; int a[10004]; int b[10004]; int main(){ clr(a); clr(a); int cas; sf("%d",&cas); while(cas--){ sf("%d",&n); for(int i=0;i<n;i++){ sf("%d",&a[i]); b[i]=a[i]; } sort(b,b+n); int sum=0; for(int i=0;i<n;i++){ if(a[i]!=b[i]){ for(int j=i+1;j<n;j++){ if(a[j]==b[i]){ swap(a[i],a[j]); sum++; } } } } pf("%d\n",sum); } return 0; }