题目链接: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次后,要求数组变成非降序数列。求出这样的一个交换操作(不要求求出最少交换次数)

解法:首先我们需要知道,一个数列最终要变为非降序,要么原数列中最小的数一定要在排完序的数列中的首位置。剩下的就迎刃而解了。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define inf 0x7fffffff
using namespace std;
const int maxn=+;
int an[maxn];
int cn[maxn][];
int cnt;
int main()
{
int n;
while (scanf("%d",&n)!=EOF)
{
cnt=;
for (int i= ;i<n ;i++) scanf("%d",&an[i]);
for (int i= ;i<n ;i++)
{
int minnum=an[i],k=i;
for (int j=i+ ;j<n ;j++)
{
if (an[j]<minnum)
{
minnum=an[j] ;k=j ;
}
}
if (k==i) continue;
cn[cnt][]=i;
cn[cnt][]=k;
swap(an[i],an[k]);
cnt++;
}
printf("%d\n",cnt);
for (int i= ;i<cnt ;i++) printf("%d %d\n",cn[i][],cn[i][]);
}
return ;
}

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 配对。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define inf 0x7fffffff
using namespace std;
const int maxn=;
int an[maxn],bn[maxn];
int n,m;
int main()
{
while (scanf("%d",&n)!=EOF)
{
for (int i= ;i<n ;i++) scanf("%d",&an[i]);
scanf("%d",&m);
for (int i= ;i<m ;i++) scanf("%d",&bn[i]);
int cnt=;
sort(an,an+n);
sort(bn,bn+m);
int j=;
for (int i= ;i<n ;i++)
{
while (j<m && an[i]-bn[j]>) j++;
if (j==m) break;
if (abs(an[i]-bn[j])== || an[i]==bn[j]) {cnt++;j++; }
}
printf("%d\n",cnt);
}
return ;
}

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,一个是最大,另一个最小。

解法:简单构造

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define inf 0x7fffffff
using namespace std;
const int maxn=;
char str[maxn],str2[maxn];
int m,s;
int main()
{
while (scanf("%d%d",&m,&s)!=EOF)
{
if (m== && s==) {printf("0 0\n");continue; }
if (s==) {printf("-1 -1\n");continue; }
memset(str,,sizeof(str));
memset(str2,,sizeof(str2));
int flag=;
int cnt=;
int ss=s;
str[m-]='';ss--;
for (int i= ;i<m- ;i++) str[i]='';
//if (ss<0) {printf("-1 -1\n");continue; }
while (ss>)
{
str[cnt++] = '';
ss -= ;
}
if (ss>) {str[cnt]=str[cnt]-''+ ss+'';cnt++;} int cnt2=;
while (s>)
{
str2[cnt2++]='';
s -= ;
}
if (s>) str2[cnt2++]=s+'';
while (cnt2<m) str2[cnt2++]='';
if (cnt2!=m)
{
printf("-1 -1\n");continue;
}
str2[cnt2]=;
for (int i=m- ;i>= ;i--) cout<<str[i];
printf(" %s\n",str2);
}
return ;
}

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 abc 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".

代码:很好懂,按照解法中的思想一步一步来,就没有给出详细解释了。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<queue>
#include<vector>
#define inf 0x7fffffff
using namespace std;
const int maxn=+;
typedef long long ll;
vector<int> vec[maxn];
int n,m;
int vis[maxn][maxn];
int vis2[maxn][maxn];
ll ans;
int main()
{
while (scanf("%d%d",&n,&m)!=EOF)
{
for (int i= ;i<maxn ;i++) vec[i].clear();
memset(vis,,sizeof(vis));
memset(vis2,,sizeof(vis2));
int a,b;
for (int i= ;i<m ;i++)
{
scanf("%d%d",&a,&b);
vec[a].push_back(b);
vis[a][b]=;
}
ans=;
for (int i= ;i<=n ;i++)
{
int b=vec[i].size();
for (int j= ;j<b ;j++)
{
int u=vec[i][j];
int num=vec[u].size();
for (int k= ;k<num ;k++)
{
int v=vec[u][k];
if (v==u || v==i) continue;
vis2[i][v]++;
}
}
}
//cout<<vis2[1][2]<<endl;
for (int i= ;i<=n ;i++)
{
for (int j= ;j<=n ;j++)
if (vis2[i][j]>) ans += (ll)vis2[i][j]*(vis2[i][j]-)/;
}
printf("%I64d\n",ans);
}
return ;
}

后续:感谢大牛提出宝贵的意见。。。

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

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

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

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

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

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

  9. Codeforces Round #277.5 (Div. 2)B——BerSU Ball

    B. BerSU Ball time limit per test 1 second memory limit per test 256 megabytes input standard input ...

随机推荐

  1. kettle的hello world

    本篇介绍使用kettle的一个最简单的例子,可以初步了解下转换. 需求是这样的: 存在一个本地csv文件,文件的内容如下 现在需要将csv中的数据保存到本地的文本文件中 1.创建一个转换,并且重命名 ...

  2. tp框架集成支付宝,中转页变成gbk编码

    tp框架中集成支付宝的功能,将支付宝的demo例子存在到下图位置\Extend\Vendor\Alipay 生成支付订单 /** * 支付订单 */ public function pay() { h ...

  3. DXperience重编译汉化的方法

    1. 必须有DXperience相应版本的全部源代码SourceCode.把全部源代码复制到\Program Files\Developer Express .NET vX.X\Sources目录.目 ...

  4. Delphi XE5 for android 调用Java类库必看的文件

    C:\Program Files\Embarcadero\RAD Studio\12.0\source\rtl\android 的目录 Androidapi.AppGlue.pasAndroidapi ...

  5. java 复习

    整型: byte 1 short 2 int 4 long 80b1001 1_233_32 1341414141414Ljava 没有无符号类型浮点型:float 4 double 812.2f 无 ...

  6. Redhat 6环境下安装Oracle 12c的方法

    Step 1: 要在Linux上安装Oracle,需要在安装Oracle之前安装好相应的软件包,在不同操作系统环境下,对软件包的要求各不相同.具体对应的软件包,见官网文档:https://docs.o ...

  7. SQL语句基础之 管理数据库,表 和 数据

    MySQL中的基本sql语句 MySQL中主要有三个大的对象,第一个是数据库,有了数据库后,我们才能在数据库里面建表,因为Mysql是关系数据库,它的数据都会以记录的形式存到表里,所以第二个是表,然后 ...

  8. linux kernel 0.11 bootsect

    bootsect作用 ①将自己移动到0x90000处 ②将setup从磁盘读到0x90200处 ③将system从磁盘读到0x10000处 寄存器 汇编代码中存在:数据段data seg 栈段 sta ...

  9. Hive与HBase区别

    对于刚接触大数据的用户来说,要想区分Hive与HBase是有一定难度的.本文将尝试从其各自的定义.特点.限制.应用场景等角度来进行分析,以作抛砖引玉之用. ====Hive是什么?Apache Hiv ...

  10. [转]VC的DDK编译环境构建

    [转]VC的DDK编译环境构建 http://blog.csdn.net/skdev/article/details/1336935   1 环境状况 Windows XP SP1 NTDDK(win ...