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 ...
随机推荐
- SQL SERVER中查询参数为空(null)时默认查询所有的实现
最近在项目中碰到一个比较有意思的问题,网上查找了一些方法,在这里总结分享一下. 我们经常会碰到这样的场景:需要查询数据,有一些查询条件,但是查询的时候,我们希望在某个条件为空的时候,则不筛选这个条件, ...
- Java实现九九乘法表的输出
九九乘法表一般为三角形,每个数分别和从1到自身的数相乘然后把结果列出来,即要用到两层循环,外层是从1到9for(i=1;i<=9;i++),内层是当前数和从1到自身相乘for(j=1;j< ...
- Java求和
用while结构求0~100的整数数字之和. 代码如下: public class WhileDemo { public static void main(String[] args) { int l ...
- 把Java对象转为xml格式
主要使用到的Java类有:javax.xml.bind.JAXBContext,javax.xml.bind.Marshaller(编排) 代码主要展示如下: public class Student ...
- ViewPager中GridView问题
GridView 嵌套在ViewPager中问题. 1. GridView属性设置无法显示. 正常显示方式 <GridView android:padding="8dip" ...
- java作用域public ,private ,protected 及不写时的区别(转)
在说明这四个关键字之前,我想就class之间的关系做一个简单的定 义,对于继承自己的class,base class可以认为他们都是自己的子 女,而对于和自己一个目录下的classes,认为都是自己的 ...
- xml结构
一.XmlHelper using System; using System.Collections.Generic; using System.Linq; using System.Web; usi ...
- SQL语句基础之 管理数据库,表 和 数据
MySQL中的基本sql语句 MySQL中主要有三个大的对象,第一个是数据库,有了数据库后,我们才能在数据库里面建表,因为Mysql是关系数据库,它的数据都会以记录的形式存到表里,所以第二个是表,然后 ...
- shell字符串的截取
1.变量 var 从 npos ∈ [0, length-1] 位开始,从左->右截取 num 个字符: ${var:npos:num} / ${var:npos} 小结:若 npos < ...
- 常用的PC/SC接口函数
PC/SC规范是一个基于WINDOWS平台的一个标准用户接口(API),提供了一个从个人电脑(Personal Computer)到智能卡(SmartCard)的整合环境,PC/SC规范建立在工业标准 ...