Codeforces Round #277.5 (Div. 2)
题目链接: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 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".
代码:很好懂,按照解法中的思想一步一步来,就没有给出详细解释了。
#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)的更多相关文章
- Codeforces Round #277.5 (Div. 2) ABCDF
http://codeforces.com/contest/489 Problems # Name A SwapSort standard input/output 1 s, 256 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #277.5 (Div. 2)-D
题意:求该死的菱形数目.直接枚举两端的点.平均意义每一个点连接20条边,用邻接表暴力计算中间节点数目,那么中间节点任选两个与两端可组成的菱形数目有r*(r-1)/2. 代码: #include< ...
- 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 ...
随机推荐
- PopupWindow的简单使用
测试代码: package com.zzw.testpopuwindows; import android.app.Activity; import android.graphics.Color; i ...
- 用FileInputStream读文件,字节数组接收,不知道文件的大小时怎么办
FileInputStream in = new FileInputStream(文件路径File); byte[] buffer = new byte[in.available()]; in.rea ...
- Orcle 系统表
oracle系统表大全 一.管理员 1.用户: select username from dba_users; 改口令 alter user spgroup identified by spgtest ...
- C 网页压力测试器
引言 <<独白>> 席慕蓉 节选一 把向你借来的笔还给你吧. 一切都发生在回首的刹那. 我的彻悟如果是缘自一种迷乱,那么,我的种种迷乱不也就只是因为一种彻悟? 在一回首间,才忽 ...
- Python 3.5.2建立与DB2的连接
Python是可以连接数据库,并从数据库获取相应的数据库的,但是怎么连接呢? 这是个问题,以下是我使用Python建立数据库连接的步骤(我使用的工具为:PyCharm) 1.首先下载setuptool ...
- 【转载】MongoDB参数
我们可以通过mongod --help查看mongod的所有参数说明,以下是各参数的中文解释. 基本配置–quiet# 安静输出 –port arg# 指定服务端口号,默认端口27017 –bind_ ...
- 调用微信退款接口时出现System.Security.Cryptography.CryptographicException: 出现了内部错误 解决办法
我总结了一下出现证书无法加载的原因有以下三个 1.证书密码不正确,微信证书密码就是商户号 解决办法:请检查证书密码是不是和商户号一致 2.IIS设置错误,未加载用户配置文件 解决办法:找到网站使用的应 ...
- GIS中栅格数据的拼接
Datamanager Tools——Raster——Raster Dataset——Mosaic to New Raster 如果最大值是实际的真值,选择masaic operator要保留Max ...
- hdu 5412 CRB and Queries
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5412 CRB and Queries Description There are $N$ boys i ...
- 开启Objective-C --- OC基础知识
一.Objective-C简述 Objective-C通常写作ObjC和较少用的Objective C或Obj-C,是扩充C的面向对象编程语言.Objective-C主要用于:编写iOS操作 ...