付忠庆的练习小笔记-Codeforces #277.5 Div2 A&D

A. SwapSort

http://codeforces.com/contest/489/problem/A

给一个无序数列每次可以任意交换其中的两个数,求最小的交换次数使之变成有序数列,并输出方案

这个题搞了一个小时,纠结于怎样才能做到最优,不过到后来仔细一想:

和排序后的相比 未排序不同的必定要swap最后索性就一个个的swap没想到A了,至今心有余悸 - - 

上代码:

 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 }

D

http://codeforces.com/contest/489/problem/D

给一个有向图如果存在一组边 (a, b), (b, c), (a, d), (d, c)  就代表有一个 "damn rhombus".   求某有向图中 "damn rhombus". 的个数

 

暴搜,,,对于点i搜出j的集合对于w(i,j)==2然后排列组合,结果是 i点的方案数  最后再把所有节点的方案数  由于是有向图不必考虑在排列组合会有重复问题

 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 }

 

posted @ 2014-11-19 16:56  付忠庆  阅读(138)  评论(0编辑  收藏  举报