A. SwapSortios
http://codeforces.com/contest/489/problem/Aspa
給一個無序數列每次能夠任意交換其中的兩個數,求最小的交換次數使之變成有序數列,並輸出方案code
這個題搞了一個小時,糾結於怎樣才能作到最優,不過到後來仔細一想:blog
和排序後的相比 未排序不一樣的一定要swap最後索性就一個個的swap沒想到A了,至今心有餘悸 - - 排序
上代碼:ci
1 #include <iostream> 2 #include <algorithm> 3 #include <map> 4 #include <vector> 5 6 7 using namespace std; 8 pair <int,int> aaa; 9 vector <pair <int,int> > x; 10 11 int main() 12 { 13 int n; 14 int a[3010],b[3010]; 15 while(cin>>n) 16 { 17 for(int i=0; i<n ;i++) 18 { 19 cin>>a[i]; 20 b[i]=a[i]; 21 } 22 23 int h=0; 24 int t=n-1; 25 x.clear(); 26 while(h!=t) 27 { 28 if(a[h]==*min_element(a+h,a+t+1)) {h++;continue;} 29 aaa.first=h; 30 aaa.second=min_element(a+h,a+t+1)-a; 31 x.push_back(aaa); 32 int minn=min_element(a+h,a+t+1)-a; 33 int temp=a[h]; 34 a[h]=a[minn]; 35 a[minn]=temp; 36 h++; 37 } 38 cout<<x.size()<<endl; 39 vector <pair <int,int> >::iterator iter; 40 for(iter=x.begin();iter!=x.end();iter++){ 41 aaa=*iter; 42 cout<<aaa.first<<" "<<aaa.second<<endl; 43 } 44 } 45 return 0; 46 }
Delement
http://codeforces.com/contest/489/problem/Dstring
給一個有向圖若是存在一組邊 (a, b), (b, c), (a, d), (d, c) 就表明有一個 "damn rhombus". 求某有向圖中 "damn rhombus". 的個數it
暴搜,,,對於點i搜出j的集合對於w(i,j)==2而後排列組合,結果是 i點的方案數 最後再把全部節點的方案數 因爲是有向圖沒必要考慮在排列組合會有重複問題io
1 #include <iostream> 2 #include <vector> 3 #include <cstring> 4 typedef long long LL; 5 using namespace std; 6 vector <int> G[3010]; 7 int ct[3010]; 8 LL js(int x) 9 { 10 return x*(x-1)/2; 11 } //因爲是兩條路徑因此排列組合能夠簡寫~~ 12 int main() 13 { 14 int n,m; 15 while(cin>>n>>m) 16 { 17 for(int i=1;i<=n;i++) G[i].clear(); 18 while(m--) 19 { 20 int ta,tb; 21 cin>>ta>>tb; 22 G[ta].push_back(tb); 23 } 24 LL ans=0; 25 for(int i=1;i<=n;i++) 26 { 27 memset(ct,0,sizeof(ct)); 28 for(int j=0;j<G[i].size();j++) 29 for(int k=0;k<G[G[i][j]].size();k++) 30 ct[G[G[i][j]][k]]++; 31 ct[i]=0; //防止環的發生 32 for(int i=1;i<=n;i++) 33 ans+=js(ct[i]); 34 } 35 cout<<ans<<endl; 36 } 37 return 0; 38 }