题目链接: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. 通过 XML HTTP 把文本文件载入 HTML 元素

    新建一个.aspx文件 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="01-通 ...

  2. 14)Java中Assert

    J2SE 1.4在语言上提供了一个新特性,就是assertion(断言)功能,它是该版本在Java语言方面最大的革新.在软件开发中,assertion是一种经典的调试.测试方式. jvm 断言默认是关 ...

  3. 1)Java JDK和JRE

    >JRE :  Java Runtime Enviroment   Java的运行环境.面向Java程序的使用者,而不是开发者.如果你仅下载并安装了JRE,那么你的系统只能运行Java程序(不能 ...

  4. Android开发环境下关于如何导出手机通讯录数据库【Written By KillerLegend】

    首先度Linux中的权限(Permissions)进行一些说明: permissions一共有10个符号位,[- --- --- ---],在这里我们从左至右由0开始编号,各个符号位的编号分别为0,1 ...

  5. IOS学习2

    1. #import,#include 和@class的区别 都引用一个类,根本定义区别:#include ,#import会把所有的copy一份到该文件 #import比#include的优势,im ...

  6. MongoDB探索之路(一)——入门

    1.MongoDB和传统关系型数据库的比较  2.面向文档的 NoSQL 数据库主要解决的问题不是高性能的并发读写,而是保证海量数据存储的同时,具有良好的查询性能.  3.MongoDB可以作为日志分 ...

  7. 大话RAC介质恢复---联机日志损坏

    对联机日志的损坏要根据日志状态进行分析,联机日志一般会有Current.Active和Inactive三种状态.Inactive状态不会造成数据丢失.而Active和Current状态的日志一般会造成 ...

  8. oracle 几个时间函数探究

    近来经常用到时间函数,在此写一个笔记,记录自己的所得,希望也对您有所帮助. 1.对于一个时间如 sysdate:2015/1/30 14:16:03如何只得到年月日,同时它的数据类型不变化呢? 最容易 ...

  9. Unity3d Shortcuts

    参考:http://www.ceeger.com/Manual/ 场景视图导航  Click-drag to drag the camera around. 点击拖拽平移场景视图 Hold Alt a ...

  10. android开发系列之多线程

    今天在这篇博客里面,我只想谈谈自己对程序开发里面避无可避的一个问题-多线程的一些看法与思考. 其实说到多线程这个名称相信只要接触过软件这个行业的人都已经耳熟能详了,但是如果被问到到底什么才是多线程呢? ...