A. SwapSort
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Input

The first line of the input contains integer n (1 ≤ n ≤ 3000)
— the number of array elements. The second line contains elements of array: a0, a1, ..., an - 1 ( - 109 ≤ ai ≤ 109),
where ai is
the i-th element of the array. The elements are numerated from 0 to n - 1 from
left to right. Some integers may appear in the array more than once.

Output

In the first line print k (0 ≤ k ≤ n)
— the number of swaps. Next k lines must contain the descriptions of the kswaps,
one per line. Each swap should be printed as a pair of integers ij (0 ≤ i, j ≤ n - 1),
representing the swap of elements ai and aj.
You can print indices in the pairs in any order. The swaps are performed in the order they appear in the output, from the first to the last. It is allowed to print i = j and
swap the same pair of elements multiple times.

If there are multiple answers, print any of them. It is guaranteed that at least one answer exists.

Sample test(s)
input
  1. 5
  2. 5 2 5 1 4
output
  1. 2
  2. 0 3
  3. 4 2
input
  1. 6
  2. 10 20 20 40 60 60
output
  1. 0
input
  1. 2
  2. 101 100
output
  1. 1
  2. 0 1

排序之后贪心一下

  1. <span style="font-size:18px;">/*************************************************************************
  2. > File Name: cf.cpp
  3. > Author: acvcla
  4. > QQ:
  5. > Mail: acvcla@gmail.com
  6. > Created Time: 2014年11月17日 星期一 23时34分13秒
  7. ************************************************************************/
  8. #include<iostream>
  9. #include<algorithm>
  10. #include<cstdio>
  11. #include<vector>
  12. #include<cstring>
  13. #include<map>
  14. #include<queue>
  15. #include<stack>
  16. #include<string>
  17. #include<cstdlib>
  18. #include<ctime>
  19. #include<set>
  20. #include<math.h>
  21. using namespace std;
  22. typedef long long LL;
  23. const int maxn = 3e3 + 10;
  24. #define rep(i,a,b) for(int i=(a);i<=(b);i++)
  25. #define pb push_back
  26. int A[maxn],B[maxn];
  27. std::vector<int>x,y;
  28. int main(){
  29. ios_base::sync_with_stdio(false);
  30. cin.tie(0);
  31. int n;
  32. int ans=0;
  33. while(cin>>n){
  34. x.clear();y.clear();
  35. for(int i=0;i<n;i++){
  36. cin>>A[i];
  37. B[i]=A[i];
  38. }
  39. sort(B,B+n);
  40. for(int i=0;i<n;i++){
  41. if(A[i]==B[i])continue;
  42. int M=0;
  43. for(int j=i+1;j<n;j++)if(A[j]==B[i]){
  44. M=j;
  45. if(B[j]==A[i]){
  46. swap(A[i],A[j]);
  47. x.pb(i);y.pb(j);
  48. break;
  49. }
  50. }
  51. if(A[i]==B[i])continue;
  52. swap(A[i],A[M]);
  53. x.pb(i);y.pb(M);
  54. }
  55. cout<<x.size()<<endl;
  56. for(int i=0;i<x.size();i++){
  57. cout<<x[i]<<' '<<y[i]<<endl;
  58. }
  59. }
  60. return 0;
  61. }</span>
B. BerSU Ball
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Input

The first line contains an integer n (1 ≤ n ≤ 100)
— the number of boys. The second line contains sequencea1, a2, ..., an (1 ≤ ai ≤ 100),
where ai is
the i-th boy's dancing skill.

Similarly, the third line contains an integer m (1 ≤ m ≤ 100)
— the number of girls. The fourth line contains sequence b1, b2, ..., bm (1 ≤ bj ≤ 100),
where bj is
the j-th girl's dancing skill.

Output

Print a single number — the required maximum possible number of pairs.

Sample test(s)
input
  1. 4
  2. 1 4 6 2
  3. 5
  4. 5 1 5 7 9
output
  1. 3
input
  1. 4
  2. 1 2 3 4
  3. 4
  4. 10 11 12 13
output
  1. 0
input
  1. 5
  2. 1 1 1 1 1
  3. 3
  4. 1 2 3
output
  1. 2

直接贪心就好

  1. <span style="font-size:18px;">/*************************************************************************
  2. > File Name: cf.cpp
  3. > Author: acvcla
  4. > QQ:
  5. > Mail: acvcla@gmail.com
  6. > Created Time: 2014年11月17日 星期一 23时34分13秒
  7. ************************************************************************/
  8. #include<iostream>
  9. #include<algorithm>
  10. #include<cstdio>
  11. #include<vector>
  12. #include<cstring>
  13. #include<map>
  14. #include<queue>
  15. #include<stack>
  16. #include<string>
  17. #include<cstdlib>
  18. #include<ctime>
  19. #include<set>
  20. #include<math.h>
  21. using namespace std;
  22. typedef long long LL;
  23. const int maxn = 1e3 + 10;
  24. #define rep(i,a,b) for(int i=(a);i<=(b);i++)
  25. #define pb push_back
  26. int A[maxn],B[maxn];
  27. int main(){
  28. ios_base::sync_with_stdio(false);
  29. cin.tie(0);
  30. int n,m;
  31. while(cin>>n){
  32. memset(A,0,sizeof A);
  33. memset(B,0,sizeof B);
  34. int x;
  35. rep(i,1,n){
  36. cin>>x;
  37. ++A[x];
  38. }
  39. cin>>m;
  40. rep(i,1,m){
  41. cin>>x;
  42. ++B[x];
  43. }
  44. int ans=0;
  45. rep(i,1,100){
  46. if(A[i]<=0)continue;
  47. ans+=min(A[i],B[i-1]+B[i]+B[i+1]);
  48. if(A[i]>=B[i-1]+B[i]+B[i+1])B[i-1]=B[i]=B[i+1]=0;
  49. else{
  50. if(B[i-1]>0&&A[i]>0){
  51. int t=B[i-1];
  52. B[i-1]=max(B[i-1]-A[i],0);
  53. A[i]=max(A[i]-t,0);
  54. }
  55. if(B[i]>0&&A[i]>0){
  56. int t=B[i];
  57. B[i]=max(B[i]-A[i],0);
  58. A[i]=max(A[i]-t,0);
  59. }
  60. if(B[i+1]>0&&A[i]>0){
  61. int t=B[i+1];
  62. B[i+1]=max(B[i+1]-A[i],0);
  63. A[i]=max(A[i]-t,0);
  64. }
  65. }
  66. }
  67. cout<<ans<<endl;
  68. }
  69. return 0;
  70. }</span>
C. Given Length and Sum of Digits...
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Input

The single line of the input contains a pair of integers ms (1 ≤ m ≤ 100, 0 ≤ s ≤ 900)
— the length and the sum of the digits of the required numbers.

Output

In the output print the pair of the required non-negative integer numbers — first the minimum possible number, then — the maximum possible number. If no numbers satisfying conditions required exist, print the pair of numbers "-1
-1" (without the quotes).

Sample test(s)
input
  1. 2 15
output
  1. 69 96
input
  1. 3 0
output
  1. -1 -1

贪心加细节

  1. <span style="font-size:18px;">/*************************************************************************
  2. > File Name: cf.cpp
  3. > Author: acvcla
  4. > QQ:
  5. > Mail: acvcla@gmail.com
  6. > Created Time: 2014年11月17日 星期一 23时34分13秒
  7. ************************************************************************/
  8. #include<iostream>
  9. #include<algorithm>
  10. #include<cstdio>
  11. #include<vector>
  12. #include<cstring>
  13. #include<map>
  14. #include<queue>
  15. #include<stack>
  16. #include<string>
  17. #include<cstdlib>
  18. #include<ctime>
  19. #include<set>
  20. #include<math.h>
  21. using namespace std;
  22. typedef long long LL;
  23. const int maxn = 1e5 + 10;
  24. #define rep(i,a,b) for(int i=(a);i<=(b);i++)
  25. #define pb push_back
  26. int main(){
  27. ios_base::sync_with_stdio(false);
  28. cin.tie(0);
  29. int m,s;
  30. char ans1[200],ans2[200];
  31. while(cin>>m>>s){
  32. bool ok=true;
  33. int t1=s/9;
  34. for(int i=0;i<150;i++)ans2[i]=ans1[i]='9';
  35. if(s==0&&m==1){
  36. cout<<"0 0"<<endl;
  37. continue;
  38. }
  39. if(!s&&m>1||s>m*9){
  40. cout<<"-1 -1"<<endl;continue;
  41. }
  42. ans2[m]=ans1[m]=0;
  43. int d=s%9;
  44. if(d==0){
  45. if(t1==m){
  46. cout<<ans1<<' '<<ans2<<endl;
  47. continue;
  48. }
  49. ans1[0]='1';
  50. ans1[m-t1]='8';
  51. for(int i=m-t1-1;i>0;i--)ans1[i]='0';
  52. for(int i=t1;i<m;i++)ans2[i]='0';
  53. }else{
  54. if(t1==m-1){
  55. ans1[0]='0'+d;
  56. ans2[m-1]='0'+d;
  57. }else{
  58. ans1[0]='1';
  59. ans1[m-t1-1]='0'+d-1;
  60. for(int i=m-t1-2;i>0;i--)ans1[i]='0';
  61. ans2[t1]='0'+d;
  62. for(int i=t1+1;i<m;i++)ans2[i]='0';
  63. }
  64. }
  65. cout<<ans1<<' '<<ans2<<endl;
  66. }
  67. return 0;
  68. }</span>
D. Unbearable Controversy of Being
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 abc and d,
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.

Input

The first line of the input contains a pair of integers nm (1 ≤ n ≤ 3000, 0 ≤ m ≤ 30000)
— the number of intersections and roads, respectively. Next m lines list the roads, one per line. Each of the roads is given by a pair of
integers ai, bi (1 ≤ ai, bi ≤ n;ai ≠ bi)
— the number of the intersection it goes out from and the number of the intersection it leads to. Between a pair of intersections there is at most one road in each of the two directions.

It is not guaranteed that you can get from any intersection to any other one.

Output

Print the required number of "damn rhombi".

Sample test(s)
input
  1. 5 4
  2. 1 2
  3. 2 3
  4. 1 4
  5. 4 3
output
  1. 1
input
  1. 4 12
  2. 1 2
  3. 1 3
  4. 1 4
  5. 2 1
  6. 2 3
  7. 2 4
  8. 3 1
  9. 3 2
  10. 3 4
  11. 4 1
  12. 4 2
  13. 4 3
output
  1. 12

暴力就好。假设u,v之间长度为2的路径有x条,且x>1,那么显然以u。v为起点和终点的四边形有c(x,2)个

  1. <span style="font-size:18px;">/*************************************************************************
  2. > File Name: cf.cpp
  3. > Author: acvcla
  4. > QQ:
  5. > Mail: acvcla@gmail.com
  6. > Created Time: 2014年11月17日 星期一 23时34分13秒
  7. ************************************************************************/
  8. #include<iostream>
  9. #include<algorithm>
  10. #include<cstdio>
  11. #include<vector>
  12. #include<cstring>
  13. #include<map>
  14. #include<queue>
  15. #include<stack>
  16. #include<string>
  17. #include<cstdlib>
  18. #include<ctime>
  19. #include<set>
  20. #include<math.h>
  21. using namespace std;
  22. typedef long long LL;
  23. const int maxn = 3e3 + 10;
  24. #define rep(i,a,b) for(int i=(a);i<=(b);i++)
  25. #define pb push_back
  26. int n,m;
  27. std::vector<int> G[maxn];
  28. int d[maxn][maxn];
  29. void dfs(int u,int v,int dist){
  30. if(dist>2)return;
  31. if(dist==2){
  32. ++d[u][v];return ;
  33. }
  34. for(int i=0;i<G[v].size();i++){
  35. dfs(u,G[v][i],dist+1);
  36. }
  37. }
  38. int main(){
  39. ios_base::sync_with_stdio(false);
  40. cin.tie(0);
  41. while(cin>>n>>m){
  42. for(int i=1;i<=n;i++)G[i].clear();
  43. memset(d,0,sizeof d);
  44. int u,v;
  45. for(int i=1;i<=m;i++){
  46. cin>>u>>v;
  47. G[u].pb(v);
  48. }
  49. LL ans=0;
  50. for(int i=1;i<=n;i++)dfs(i,i,0);
  51. for(int i=1;i<=n;i++){
  52. for(int j=1;j<=n;j++){
  53. if(d[i][j]<2||j==i)continue;
  54. //cout<<i<<' '<<j<<' '<<d[i][j]<<endl;
  55. LL t=d[i][j];
  56. ans+=(t*(t-1))/2;
  57. }
  58. }
  59. cout<<ans<<endl;
  60. }
  61. return 0;
  62. }</span>
F. Special Matrices
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

An n × n square matrix is special, if:

  • it is binary, that is, each cell contains either a 0, or a 1;
  • the number of ones in each row and column equals 2.

You are given n and the first m rows
of the matrix. Print the number of special n × n matrices, such that the first m rows
coincide with the given ones.

As the required value can be rather large, print the remainder after dividing the value by the given numbermod.

Input

The first line of the input contains three integers nmmod (2 ≤ n ≤ 500, 0 ≤ m ≤ n, 2 ≤ mod ≤ 109).
Thenm lines follow, each of them contains n characters
— the first rows of the required special matrices. Each of these lines contains exactly two characters '1', the rest characters are '0'.
Each column of the given m × ntable contains at most two numbers one.

Output

Print the remainder after dividing the required value by number mod.

Sample test(s)
input
  1. 3 1 1000
  2. 011
output
  1. 2
input
  1. 4 4 100500
  2. 0110
  3. 1010
  4. 0101
  5. 1001
output
  1. 1
Note

For the first test the required matrices are:

  1. 011
  2. 101
  3. 110
  4.  
  5. 011
  6. 110
  7. 101

In the second test the required matrix is already fully given, so the answer is 1.


按行dp,因为每行的和必须为2,所以能够在新建行的时候在列和为1或0的位置上选出两个来加入。

直到终于列和所有为2.

详细看代码

  1. <span style="font-size:18px;">/*************************************************************************
  2. > File Name: cf.cpp
  3. > Author: acvcla
  4. > QQ:
  5. > Mail: acvcla@gmail.com
  6. > Created Time: 2014年11月17日 星期一 23时34分13秒
  7. ************************************************************************/
  8. #include<iostream>
  9. #include<algorithm>
  10. #include<cstdio>
  11. #include<vector>
  12. #include<cstring>
  13. #include<map>
  14. #include<queue>
  15. #include<stack>
  16. #include<string>
  17. #include<cstdlib>
  18. #include<ctime>
  19. #include<set>
  20. #include<math.h>
  21. using namespace std;
  22. typedef long long LL;
  23. const int maxn = 500+10;
  24. #define rep(i,a,b) for(int i=(a);i<=(b);i++)
  25. #define pb push_back
  26. LL d[maxn][maxn],n,m,mod;
  27. bool vis[maxn][maxn];
  28. int col[maxn];
  29. char s[maxn];
  30. LL cn2(LL n){
  31. return n*(n-1)/2;
  32. }
  33. LL dfs(LL x,LL y){//当前还有x列为1,y列为0。到达目标状态的方案种数,之所以是每次选2个是由于每行的和都必须为2
  34. if(x==0&&y==0)return 1;
  35. if(vis[x][y])return d[x][y];
  36. d[x][y]=0;
  37. vis[x][y]=1;
  38. if(x>=2)d[x][y]+=cn2(x)*dfs(x-2,y)%mod;//选出为1的两列让其加上1,此时和为1的为x-2,和为0的为y
  39. if(y>=2)d[x][y]+=cn2(y)*dfs(x+2,y-2)%mod;//选出为0的两列让其加上1,此时和为1的为x+2,和为0的为y-2
  40. if(x>=1&&y>=1)d[x][y]+=x*y*dfs(x,y-1)%mod;//各选一列加上1,此时和为1的为x,和为0的为y-1
  41. return d[x][y]%=mod;
  42. }
  43. int main(){
  44. ios_base::sync_with_stdio(false);
  45. cin.tie(0);
  46. while(cin>>n>>m>>mod){
  47. memset(vis,0,sizeof vis);
  48. memset(col,0,sizeof col);
  49. rep(i,1,m){
  50. cin>>s;
  51. for(int j=0;j<n;j++){
  52. col[j+1]+=(s[j]=='1');
  53. }
  54. }
  55. LL x=0,y=0;
  56. for(int i=1;i<=n;i++){
  57. if(col[i]==1)x++;
  58. if(col[i]==0)y++;
  59. if(col[i]>2){
  60. cout<<0<<endl;
  61. return 0;
  62. }
  63. }
  64. cout<<dfs(x,y)<<endl;
  65. }
  66. return 0;
  67. }</span>

Codeforces Round #277.5 (Div. 2)部分题解的更多相关文章

  1. Codeforces Round #277.5 (Div. 2) A,B,C,D,E,F题解

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud A. SwapSort time limit per test    1 seco ...

  2. Codeforces Round #277.5 (Div. 2) ABCDF

    http://codeforces.com/contest/489 Problems     # Name     A SwapSort standard input/output 1 s, 256 ...

  3. Codeforces Round #277.5 (Div. 2)

    题目链接:http://codeforces.com/contest/489 A:SwapSort In this problem your goal is to sort an array cons ...

  4. Codeforces Round #277.5 (Div. 2) --E. Hiking (01分数规划)

    http://codeforces.com/contest/489/problem/E E. Hiking time limit per test 1 second memory limit per ...

  5. Codeforces Round #277.5 (Div. 2)-D. Unbearable Controversy of Being

    http://codeforces.com/problemset/problem/489/D D. Unbearable Controversy of Being time limit per tes ...

  6. Codeforces Round #277.5 (Div. 2)-C. Given Length and Sum of Digits...

    http://codeforces.com/problemset/problem/489/C C. Given Length and Sum of Digits... time limit per t ...

  7. Codeforces Round #277.5 (Div. 2)-B. BerSU Ball

    http://codeforces.com/problemset/problem/489/B B. BerSU Ball time limit per test 1 second memory lim ...

  8. Codeforces Round #277.5 (Div. 2)-A. SwapSort

    http://codeforces.com/problemset/problem/489/A A. SwapSort time limit per test 1 second memory limit ...

  9. Codeforces Round #277.5 (Div. 2)-D

    题意:求该死的菱形数目.直接枚举两端的点.平均意义每一个点连接20条边,用邻接表暴力计算中间节点数目,那么中间节点任选两个与两端可组成的菱形数目有r*(r-1)/2. 代码: #include< ...

随机推荐

  1. 关于MySQL字符集问题:Specified key was too long; max key length is 767 bytes

    [文章来源]http://blog.csdn.net/cindy9902/article/details/6215769 MySQL: ERROR 1071 (42000): Specified ke ...

  2. Mac下Python安装目录

    /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages

  3. 一款纯css3实现的图片3D翻转幻灯片

    之前介绍了好多款网页幻灯片,今天要给大家再带来一款纯css3实现的图片3D翻转幻灯片.这款幻灯片图片轮播采用了3D翻转的形式,效果非常不错.一起看下效果图: 在线预览   源码下载 实现的代码. ht ...

  4. VBA 删除页

    怎么让word自动删除第3.6.9.12等3的倍数页‘ Sub kk1206190933() Dim wNum As Integer Dim wPag As Integer With Selectio ...

  5. kubernetes 数据持久化

    pod本身是无状态,所以很多有状态的应用,就需要将数据进行持久化. 1:将数据挂在到宿主机.但是pod重启之后有可能到另外一个节点,这样数据虽然不会丢但是还是有可能会找不到 apiVersion: v ...

  6. pthread_self()究竟根据什么来得到线程的标识符????

    #include<stdlib.h> #include<pthread.h> #include<stdio.h> #include<sched.h> # ...

  7. java clone对象

    本文转载至 http://blog.csdn.net/shootyou/article/details/3945221 现在Clone已经不是一个新鲜词语了,伴随着“多莉”的产生这个词语确实很“火”过 ...

  8. java资料——顺序存储结构和链式存储结构(转)

    顺序存储结构 主要优点 节省存储空间,随机存取表中元素 缺    点 插入和删除操作需要移动元素 在计算机中用一组地址连续的存储单元依次存储线性表的各个数据元素,称作线性表的顺序存储结构. 顺序存储结 ...

  9. nfs服务器与客户端配置

    服务器端(PC)配置 ubuntu提供两种NFS服务器:一种以内核模块形式提供,nfs-kernel-server:一种以用户空间程序形式提供,nfs-user-server;两种择一即可.1. 安装 ...

  10. 使用Ant编译Hadoop工程报错

    在win7用Ant编译hadoop工程的时候,遇到了一个报错,如下: org.eclipse.core.runtime.CoreException: D:\workspace\hadoop-1.1.2 ...