题目链接:http://codeforces.com/contest/489
A:SwapSort
In this problem your goal is to sort an array consisting of n integers in at most n swaps. For the given array find the sequence of swaps that makes the array sorted in the non-descending order. Swaps are performed consecutively, one after another.
Note that in this problem you do not have to minimize the number of swaps — your task is to find any sequence that is no longer than n.
题意:给出包含n个数的数组(一个数可以出现多次),每次可以交换任意两个数,最多交换n次后,要求数组变成非降序数列。求出这样的一个交换操作(不要求求出最少交换次数)
解法:首先我们需要知道,一个数列最终要变为非降序,要么原数列中最小的数一定要在排完序的数列中的首位置。剩下的就迎刃而解了。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cstdlib> 5 #include<cmath> 6 #include<algorithm> 7 #define inf 0x7fffffff 8 using namespace std; 9 const int maxn=3000+10; 10 int an[maxn]; 11 int cn[maxn][2]; 12 int cnt; 13 int main() 14 { 15 int n; 16 while (scanf("%d",&n)!=EOF) 17 { 18 cnt=0; 19 for (int i=0 ;i<n ;i++) scanf("%d",&an[i]); 20 for (int i=0 ;i<n ;i++) 21 { 22 int minnum=an[i],k=i; 23 for (int j=i+1 ;j<n ;j++) 24 { 25 if (an[j]<minnum) 26 { 27 minnum=an[j] ;k=j ; 28 } 29 } 30 if (k==i) continue; 31 cn[cnt][0]=i; 32 cn[cnt][1]=k; 33 swap(an[i],an[k]); 34 cnt++; 35 } 36 printf("%d\n",cnt); 37 for (int i=0 ;i<cnt ;i++) printf("%d %d\n",cn[i][0],cn[i][1]); 38 } 39 return 0; 40 }
B:BerSU Ball
The Berland State University is hosting a ballroom dance in celebration of its 100500-th anniversary! n boys and m girls are already busy rehearsing waltz, minuet, polonaise and quadrille moves.
We know that several boy&girl pairs are going to be invited to the ball. However, the partners' dancing skill in each pair must differ by at most one.
For each boy, we know his dancing skills. Similarly, for each girl we know her dancing skills. Write a code that can determine the largest possible number of pairs that can be formed from n boys and m girls.
题意:n个男孩,每个男孩给一个值,a1,a2,,,,an;m个女孩,b1,b2,,,bm。如果abs(ai-bj)<=1,说明男孩i 和 女孩j 配对成功,配对成功的这两个人就不能再和其他人配对了,即每个人只有一次配对成功的机会。问最大成功配对数。
解法:咋一看想到了二分最大匹配,再一想想,好像贪心可以搞。对两个数组非降序排序,如果 ai > bj + 1,那么ai 后面的肯定也和bj 配对不成功;如果ai 和 bj 配对成功,ai+1 和 bj 也能配对成功,那么这时候我们把bj 和 ai 组在一起,让ai+1有机会和 bj+1 配对。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cstdlib> 5 #include<cmath> 6 #include<algorithm> 7 #define inf 0x7fffffff 8 using namespace std; 9 const int maxn=111; 10 int an[maxn],bn[maxn]; 11 int n,m; 12 int main() 13 { 14 while (scanf("%d",&n)!=EOF) 15 { 16 for (int i=0 ;i<n ;i++) scanf("%d",&an[i]); 17 scanf("%d",&m); 18 for (int i=0 ;i<m ;i++) scanf("%d",&bn[i]); 19 int cnt=0; 20 sort(an,an+n); 21 sort(bn,bn+m); 22 int j=0; 23 for (int i=0 ;i<n ;i++) 24 { 25 while (j<m && an[i]-bn[j]>1) j++; 26 if (j==m) break; 27 if (abs(an[i]-bn[j])==1 || an[i]==bn[j]) {cnt++;j++; } 28 } 29 printf("%d\n",cnt); 30 } 31 return 0; 32 }
C:Given Length and Sum of Digits...
You have a positive integer m and a non-negative integer s. Your task is to find the smallest and the largest of the numbers that have length m and sum of digits s. The required numbers should be non-negative integers written in the decimal base without leading zeroes.
题意:给出n 和 m ,构造出两个不含前导零的n位数,位数上值的和为m,一个是最大,另一个最小。
解法:简单构造
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cstdlib> 5 #include<cmath> 6 #include<algorithm> 7 #define inf 0x7fffffff 8 using namespace std; 9 const int maxn=104; 10 char str[maxn],str2[maxn]; 11 int m,s; 12 int main() 13 { 14 while (scanf("%d%d",&m,&s)!=EOF) 15 { 16 if (m==1 && s==0) {printf("0 0\n");continue; } 17 if (s==0) {printf("-1 -1\n");continue; } 18 memset(str,0,sizeof(str)); 19 memset(str2,0,sizeof(str2)); 20 int flag=0; 21 int cnt=0; 22 int ss=s; 23 str[m-1]='1';ss--; 24 for (int i=0 ;i<m-1 ;i++) str[i]='0'; 25 //if (ss<0) {printf("-1 -1\n");continue; } 26 while (ss>9) 27 { 28 str[cnt++] = '9'; 29 ss -= 9 ; 30 } 31 if (ss>0) {str[cnt]=str[cnt]-'0'+ ss+'0';cnt++;} 32 33 int cnt2=0; 34 while (s>9) 35 { 36 str2[cnt2++]='9'; 37 s -= 9; 38 } 39 if (s>0) str2[cnt2++]=s+'0'; 40 while (cnt2<m) str2[cnt2++]='0'; 41 if (cnt2!=m) 42 { 43 printf("-1 -1\n");continue; 44 } 45 str2[cnt2]=0; 46 for (int i=m-1 ;i>=0 ;i--) cout<<str[i]; 47 printf(" %s\n",str2); 48 } 49 return 0; 50 }
D:Unbearable Controversy of Being
Tomash keeps wandering off and getting lost while he is walking along the streets of Berland. It's no surprise! In his home town, for any pair of intersections there is exactly one way to walk from one intersection to the other one. The capital of Berland is very different!
Tomash has noticed that even simple cases of ambiguity confuse him. So, when he sees a group of four distinct intersections a, b, c andd, such that there are two paths from a to c — one through b and the other one through d, he calls the group a "damn rhombus". Note that pairs (a, b), (b, c), (a, d), (d, c) should be directly connected by the roads. Schematically, a damn rhombus is shown on the figure below:
Other roads between any of the intersections don't make the rhombus any more appealing to Tomash, so the four intersections remain a "damn rhombus" for him.
Given that the capital of Berland has n intersections and m roads and all roads are unidirectional and are known in advance, find the number of "damn rhombi" in the city.
When rhombi are compared, the order of intersections b and d doesn't matter.
题意:有向图,有四个点a,b,c,d,如果a->b , b->d ,a->c ,c->d ,这样的一个简单图称为 "damn rhombus".然后给出n个点,m条有向边,求出 "damn rhombus"的个数
解法:暴力枚举,统计出这样一条边 a->b->d 的个数,如果 a 到 d 至少有这样的两条边,那么 a 和 d 就可以满足题中的要求,a为源点,d为汇点。
注意:如果a 到 d 有三条那样的边的话,就可以构成3个 "damn rhombus".
代码:很好懂,按照解法中的思想一步一步来,就没有给出详细解释了。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cstdlib> 5 #include<cmath> 6 #include<algorithm> 7 #include<queue> 8 #include<vector> 9 #define inf 0x7fffffff 10 using namespace std; 11 const int maxn=3000+10; 12 typedef long long ll; 13 vector<int> vec[maxn]; 14 int n,m; 15 int vis[maxn][maxn]; 16 int vis2[maxn][maxn]; 17 ll ans; 18 int main() 19 { 20 while (scanf("%d%d",&n,&m)!=EOF) 21 { 22 for (int i=0 ;i<maxn ;i++) vec[i].clear(); 23 memset(vis,0,sizeof(vis)); 24 memset(vis2,0,sizeof(vis2)); 25 int a,b; 26 for (int i=0 ;i<m ;i++) 27 { 28 scanf("%d%d",&a,&b); 29 vec[a].push_back(b); 30 vis[a][b]=1; 31 } 32 ans=0; 33 for (int i=1 ;i<=n ;i++) 34 { 35 int b=vec[i].size(); 36 for (int j=0 ;j<b ;j++) 37 { 38 int u=vec[i][j]; 39 int num=vec[u].size(); 40 for (int k=0 ;k<num ;k++) 41 { 42 int v=vec[u][k]; 43 if (v==u || v==i) continue; 44 vis2[i][v]++; 45 } 46 } 47 } 48 //cout<<vis2[1][2]<<endl; 49 for (int i=1 ;i<=n ;i++) 50 { 51 for (int j=1 ;j<=n ;j++) 52 if (vis2[i][j]>1) ans += (ll)vis2[i][j]*(vis2[i][j]-1)/2; 53 } 54 printf("%I64d\n",ans); 55 } 56 return 0; 57 }
后续:感谢大牛提出宝贵的意见。。。