标签:uva331
最小交换数等于逆序数,然后直接枚举即可
// // main.cpp // uva331 // // Created by Fangpin on 15/3/11. // Copyright (c) 2015年 FangPin. All rights reserved. // #include <iostream> #include <vector> #include <cstdio> using namespace std; vector<int> a,b; int ans,n; bool judge(){ for(int i=0;i<a.size()-1;++i){ if(b[i+1]<b[i]) return false; } return true; } void dfs(int d,int cnt){ if(d==cnt){ if(judge()) ++ans; return ; } for(int i=0;i<n-1;++i){ swap(b[i],b[i+1]); dfs(d+1,cnt); swap(b[i],b[i+1]); } } int main(int argc, const char * argv[]) { // insert code here... // std::cout << "Hello, World!\n"; int ca=0; while(scanf("%d",&n),n){ a.clear();b.clear(); for(int i=0;i<n;++i){ int tem; scanf("%d",&tem); a.push_back(tem); } int cnt=0; for(int i=0;i<n;++i){ for(int j=i+1;j<n;++j){ if(a[i]>a[j]) ++cnt; } } ans=0; b=a; if(cnt) dfs(0,cnt); printf("There are %d swap maps for input data set %d.\n",ans,++ca); } return 0; }
标签:uva331
原文地址:http://blog.csdn.net/fangpinlei/article/details/44200433