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 }