题目链接: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. ThinkPHP之中的getField、Find、select、返回数据类型详解(ThinkPHP之中所有数据读取了)

    小李子:用于演示作用的数据库表:customers 官方解读: “ 读取数据集其实就是获取数据表中的多行记录(以及关联数据),使用select方法 ” $customers=D('customers' ...

  2. FileInputStream利用缓冲数组读取数据

    package cd.itcast.fileinputstream; import java.io.File; import java.io.FileInputStream; import java. ...

  3. 射击比赛 (POJ 1719) 题解

    [问题描述] 我们假设射击的目标是一个由R*C(2≤R≤C≤ 1000)个小方格组成的矩形网格.网格中每一列恰有2个白色的小方格和R-2个黑色的小方格.定义网格的行从顶至底编号为1~R,列从左至右编号 ...

  4. USB协议分析

    一.USB设备描述结构 1.逻辑组织结构 在USB设备的逻辑组织中,包含设备.配置.接口和端点4个层次.设备通常有一个或多个配置,配置通常有一个或多个接口,接口有零或多个端点. 每个USB设备都可以包 ...

  5. Perl 随机数和随机密码的产生

    Perl有着强大的随机数产生函数rand(),下面的代码详细介绍其应用 #!/usr/bin/perl #  use strict;   use warnings; # 0~1之间    $rando ...

  6. 博客导出工具(C++实现,支持sina,csdn,自定义列表)

    操作系统:windowAll 编程工具:visual studio 2013 编程语言:VC++ 最近博文更新的较频繁,为了防止账号异常引起csdn博文丢失,所以花了点时间做了个小工具来导出博文,用做 ...

  7. Python 爬虫实例

    下面是我写的一个简单爬虫实例 1.定义函数读取html网页的源代码 2.从源代码通过正则表达式挑选出自己需要获取的内容 3.序列中的htm依次写到d盘 #!/usr/bin/python import ...

  8. db2建立表空间

    --缓冲池 CREATE BUFFERPOOL EMP_BUF_POOL  IMMEDIATE  ALL DBPARTITIONNUMS  SIZE 250  NUMBLOCKPAGES 0  PAG ...

  9. Node.js:util.inherits 面向对象特性【原型】

    /** * Created by Administrator on 2014/9/4. */ var util = require('util'); function Base() { this.na ...

  10. AppCan4.0:开发者要做有价值的APP

    在当今的移动盛世,谈论APP“生存”话题未免太过沉重.但面对百万级移动应用大军所产生的激烈竞争,且保证“立而不倒”,这样的探讨就显得格外重要了. 主打“价值牌”才能“一条龙” 有这样一组数据,在我国, ...