Codeforces Round #277.5 (Div. 2)部分题解
1 second
256 megabytes
standard input
standard output
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.
The first line of the input contains integer n (1 ≤ n ≤ 3000)
— the number of array elements. The second line contains elements of array: a0, a1, ..., an - 1 ( - 109 ≤ ai ≤ 109),
where ai is
the i-th element of the array. The elements are numerated from 0 to n - 1 from
left to right. Some integers may appear in the array more than once.
In the first line print k (0 ≤ k ≤ n)
— the number of swaps. Next k lines must contain the descriptions of the kswaps,
one per line. Each swap should be printed as a pair of integers i, j (0 ≤ i, j ≤ n - 1),
representing the swap of elements ai and aj.
You can print indices in the pairs in any order. The swaps are performed in the order they appear in the output, from the first to the last. It is allowed to print i = j and
swap the same pair of elements multiple times.
If there are multiple answers, print any of them. It is guaranteed that at least one answer exists.
5
5 2 5 1 4
2
0 3
4 2
6
10 20 20 40 60 60
0
2
101 100
1
0 1
排序之后贪心一下
<span style="font-size:18px;">/*************************************************************************
> File Name: cf.cpp
> Author: acvcla
> QQ:
> Mail: acvcla@gmail.com
> Created Time: 2014年11月17日 星期一 23时34分13秒
************************************************************************/
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<cstring>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<cstdlib>
#include<ctime>
#include<set>
#include<math.h>
using namespace std;
typedef long long LL;
const int maxn = 3e3 + 10;
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define pb push_back
int A[maxn],B[maxn];
std::vector<int>x,y;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
int ans=0;
while(cin>>n){
x.clear();y.clear();
for(int i=0;i<n;i++){
cin>>A[i];
B[i]=A[i];
}
sort(B,B+n);
for(int i=0;i<n;i++){
if(A[i]==B[i])continue;
int M=0;
for(int j=i+1;j<n;j++)if(A[j]==B[i]){
M=j;
if(B[j]==A[i]){
swap(A[i],A[j]);
x.pb(i);y.pb(j);
break;
}
}
if(A[i]==B[i])continue;
swap(A[i],A[M]);
x.pb(i);y.pb(M);
}
cout<<x.size()<<endl;
for(int i=0;i<x.size();i++){
cout<<x[i]<<' '<<y[i]<<endl;
}
}
return 0;
}</span>
1 second
256 megabytes
standard input
standard output
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.
The first line contains an integer n (1 ≤ n ≤ 100)
— the number of boys. The second line contains sequencea1, a2, ..., an (1 ≤ ai ≤ 100),
where ai is
the i-th boy's dancing skill.
Similarly, the third line contains an integer m (1 ≤ m ≤ 100)
— the number of girls. The fourth line contains sequence b1, b2, ..., bm (1 ≤ bj ≤ 100),
where bj is
the j-th girl's dancing skill.
Print a single number — the required maximum possible number of pairs.
4
1 4 6 2
5
5 1 5 7 9
3
4
1 2 3 4
4
10 11 12 13
0
5
1 1 1 1 1
3
1 2 3
2
直接贪心就好
<span style="font-size:18px;">/*************************************************************************
> File Name: cf.cpp
> Author: acvcla
> QQ:
> Mail: acvcla@gmail.com
> Created Time: 2014年11月17日 星期一 23时34分13秒
************************************************************************/
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<cstring>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<cstdlib>
#include<ctime>
#include<set>
#include<math.h>
using namespace std;
typedef long long LL;
const int maxn = 1e3 + 10;
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define pb push_back
int A[maxn],B[maxn];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n,m;
while(cin>>n){
memset(A,0,sizeof A);
memset(B,0,sizeof B);
int x;
rep(i,1,n){
cin>>x;
++A[x];
}
cin>>m;
rep(i,1,m){
cin>>x;
++B[x];
}
int ans=0;
rep(i,1,100){
if(A[i]<=0)continue;
ans+=min(A[i],B[i-1]+B[i]+B[i+1]);
if(A[i]>=B[i-1]+B[i]+B[i+1])B[i-1]=B[i]=B[i+1]=0;
else{
if(B[i-1]>0&&A[i]>0){
int t=B[i-1];
B[i-1]=max(B[i-1]-A[i],0);
A[i]=max(A[i]-t,0);
}
if(B[i]>0&&A[i]>0){
int t=B[i];
B[i]=max(B[i]-A[i],0);
A[i]=max(A[i]-t,0);
}
if(B[i+1]>0&&A[i]>0){
int t=B[i+1];
B[i+1]=max(B[i+1]-A[i],0);
A[i]=max(A[i]-t,0);
}
}
}
cout<<ans<<endl;
}
return 0;
}</span>
1 second
256 megabytes
standard input
standard output
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.
The single line of the input contains a pair of integers m, s (1 ≤ m ≤ 100, 0 ≤ s ≤ 900)
— the length and the sum of the digits of the required numbers.
In the output print the pair of the required non-negative integer numbers — first the minimum possible number, then — the maximum possible number. If no numbers satisfying conditions required exist, print the pair of numbers "-1
-1" (without the quotes).
2 15
69 96
3 0
-1 -1
贪心加细节
<span style="font-size:18px;">/*************************************************************************
> File Name: cf.cpp
> Author: acvcla
> QQ:
> Mail: acvcla@gmail.com
> Created Time: 2014年11月17日 星期一 23时34分13秒
************************************************************************/
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<cstring>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<cstdlib>
#include<ctime>
#include<set>
#include<math.h>
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 10;
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define pb push_back
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int m,s;
char ans1[200],ans2[200];
while(cin>>m>>s){
bool ok=true;
int t1=s/9;
for(int i=0;i<150;i++)ans2[i]=ans1[i]='9';
if(s==0&&m==1){
cout<<"0 0"<<endl;
continue;
}
if(!s&&m>1||s>m*9){
cout<<"-1 -1"<<endl;continue;
}
ans2[m]=ans1[m]=0;
int d=s%9;
if(d==0){
if(t1==m){
cout<<ans1<<' '<<ans2<<endl;
continue;
}
ans1[0]='1';
ans1[m-t1]='8';
for(int i=m-t1-1;i>0;i--)ans1[i]='0';
for(int i=t1;i<m;i++)ans2[i]='0';
}else{
if(t1==m-1){
ans1[0]='0'+d;
ans2[m-1]='0'+d;
}else{
ans1[0]='1';
ans1[m-t1-1]='0'+d-1;
for(int i=m-t1-2;i>0;i--)ans1[i]='0';
ans2[t1]='0'+d;
for(int i=t1+1;i<m;i++)ans2[i]='0';
}
}
cout<<ans1<<' '<<ans2<<endl;
}
return 0;
}</span>
1 second
256 megabytes
standard input
standard output
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 and d,
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.
The first line of the input contains a pair of integers n, m (1 ≤ n ≤ 3000, 0 ≤ m ≤ 30000)
— the number of intersections and roads, respectively. Next m lines list the roads, one per line. Each of the roads is given by a pair of
integers ai, bi (1 ≤ ai, bi ≤ n;ai ≠ bi)
— the number of the intersection it goes out from and the number of the intersection it leads to. Between a pair of intersections there is at most one road in each of the two directions.
It is not guaranteed that you can get from any intersection to any other one.
Print the required number of "damn rhombi".
5 4
1 2
2 3
1 4
4 3
1
4 12
1 2
1 3
1 4
2 1
2 3
2 4
3 1
3 2
3 4
4 1
4 2
4 3
12
暴力就好。假设u,v之间长度为2的路径有x条,且x>1,那么显然以u。v为起点和终点的四边形有c(x,2)个
<span style="font-size:18px;">/*************************************************************************
> File Name: cf.cpp
> Author: acvcla
> QQ:
> Mail: acvcla@gmail.com
> Created Time: 2014年11月17日 星期一 23时34分13秒
************************************************************************/
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<cstring>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<cstdlib>
#include<ctime>
#include<set>
#include<math.h>
using namespace std;
typedef long long LL;
const int maxn = 3e3 + 10;
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define pb push_back
int n,m;
std::vector<int> G[maxn];
int d[maxn][maxn];
void dfs(int u,int v,int dist){
if(dist>2)return;
if(dist==2){
++d[u][v];return ;
}
for(int i=0;i<G[v].size();i++){
dfs(u,G[v][i],dist+1);
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
while(cin>>n>>m){
for(int i=1;i<=n;i++)G[i].clear();
memset(d,0,sizeof d);
int u,v;
for(int i=1;i<=m;i++){
cin>>u>>v;
G[u].pb(v);
}
LL ans=0;
for(int i=1;i<=n;i++)dfs(i,i,0);
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(d[i][j]<2||j==i)continue;
//cout<<i<<' '<<j<<' '<<d[i][j]<<endl;
LL t=d[i][j];
ans+=(t*(t-1))/2;
}
}
cout<<ans<<endl;
}
return 0;
}</span>
1 second
256 megabytes
standard input
standard output
An n × n square matrix is special, if:
- it is binary, that is, each cell contains either a 0, or a 1;
- the number of ones in each row and column equals 2.
You are given n and the first m rows
of the matrix. Print the number of special n × n matrices, such that the first m rows
coincide with the given ones.
As the required value can be rather large, print the remainder after dividing the value by the given numbermod.
The first line of the input contains three integers n, m, mod (2 ≤ n ≤ 500, 0 ≤ m ≤ n, 2 ≤ mod ≤ 109).
Thenm lines follow, each of them contains n characters
— the first rows of the required special matrices. Each of these lines contains exactly two characters '1', the rest characters are '0'.
Each column of the given m × ntable contains at most two numbers one.
Print the remainder after dividing the required value by number mod.
3 1 1000
011
2
4 4 100500
0110
1010
0101
1001
1
For the first test the required matrices are:
011
101
110 011
110
101
In the second test the required matrix is already fully given, so the answer is 1.
按行dp,因为每行的和必须为2,所以能够在新建行的时候在列和为1或0的位置上选出两个来加入。
直到终于列和所有为2.
详细看代码
<span style="font-size:18px;">/*************************************************************************
> File Name: cf.cpp
> Author: acvcla
> QQ:
> Mail: acvcla@gmail.com
> Created Time: 2014年11月17日 星期一 23时34分13秒
************************************************************************/
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<cstring>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<cstdlib>
#include<ctime>
#include<set>
#include<math.h>
using namespace std;
typedef long long LL;
const int maxn = 500+10;
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define pb push_back
LL d[maxn][maxn],n,m,mod;
bool vis[maxn][maxn];
int col[maxn];
char s[maxn];
LL cn2(LL n){
return n*(n-1)/2;
}
LL dfs(LL x,LL y){//当前还有x列为1,y列为0。到达目标状态的方案种数,之所以是每次选2个是由于每行的和都必须为2
if(x==0&&y==0)return 1;
if(vis[x][y])return d[x][y];
d[x][y]=0;
vis[x][y]=1;
if(x>=2)d[x][y]+=cn2(x)*dfs(x-2,y)%mod;//选出为1的两列让其加上1,此时和为1的为x-2,和为0的为y
if(y>=2)d[x][y]+=cn2(y)*dfs(x+2,y-2)%mod;//选出为0的两列让其加上1,此时和为1的为x+2,和为0的为y-2
if(x>=1&&y>=1)d[x][y]+=x*y*dfs(x,y-1)%mod;//各选一列加上1,此时和为1的为x,和为0的为y-1
return d[x][y]%=mod;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
while(cin>>n>>m>>mod){
memset(vis,0,sizeof vis);
memset(col,0,sizeof col);
rep(i,1,m){
cin>>s;
for(int j=0;j<n;j++){
col[j+1]+=(s[j]=='1');
}
}
LL x=0,y=0;
for(int i=1;i<=n;i++){
if(col[i]==1)x++;
if(col[i]==0)y++;
if(col[i]>2){
cout<<0<<endl;
return 0;
}
}
cout<<dfs(x,y)<<endl;
}
return 0;
}</span>
Codeforces Round #277.5 (Div. 2)部分题解的更多相关文章
- 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) ABCDF
http://codeforces.com/contest/489 Problems # Name A SwapSort standard input/output 1 s, 256 ...
- Codeforces Round #277.5 (Div. 2)
题目链接:http://codeforces.com/contest/489 A:SwapSort In this problem your goal is to sort an array cons ...
- 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)-D
题意:求该死的菱形数目.直接枚举两端的点.平均意义每一个点连接20条边,用邻接表暴力计算中间节点数目,那么中间节点任选两个与两端可组成的菱形数目有r*(r-1)/2. 代码: #include< ...
随机推荐
- centos7 編譯 chmsee
安装libchm及相关的devel包,安装 xulrunner 及 devel 包!否则后面make的时候会出错! 到解压出来的chmsee/src目录下,找到与你系统对应的Makefile文件,我选 ...
- vue的数组如何存储数据
vue 和 angular 还有有些区别的, 比如,vue中的数组数据改变后,view并没有发生改变,angular就不会这样. 所以VUE 在数组扩展方法中提供以了一个新的API arr.$set( ...
- gulp入门与一些基本设置
这里是gulp入门的一些操作,实现了编译sass文件.压缩.合并.添加版本号等基本功能. 友情提示,如果npm出现无法下载可以安装 cnpm.在安装完Nodejs 后 npm install cnpm ...
- macOS Sierra 10.12.4 (16E195) - Clover [ 20170403 ]
原文:https://user.qzone.qq.com/753313822/blog/1424460141?_t_=0.48652242555134495 建议使用 1920 * 1080 屏幕分辨 ...
- sqlserver中sp_executesql使用实例(获取动态sql输出结果)
语法 sp_executesql [ @stmt = ] stmt [ {, [@params=] N'@parameter_name data_type [ [ OUT [ PUT ][,. ...
- C# 回调与 javascritp 回调 比较
C#: using System; using System.Collections.Generic; using System.Text; namespace Delegate { //定义委托,它 ...
- 【C#】Config配置文件的读写,及无法写入/保存配置文件的问题
目的: 一些数据为了在项目打包好后也能方便的修改和调用,通常会把这些数据放到配置文件中,避免硬编码,修改配置文件内容更方便,而不用修改源代码. 使用: 在解决方案资源管理器中找到App.config文 ...
- 添加信任站点和允许ActiveX批处理
有两种写法 1.如果是用IP reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMa ...
- else好像必须做点什么,可以省点资源不做什么吗,else下不能用pass
portfolio = [ {'name': 'IBM', 'shares': 100, 'price': 91.1}, {'name': 'AAPL', 'shares': 50, 'price': ...
- 快速上手最棒的网格框架ag-Grid
由于对aggrid由衷的感谢, 又忍不住写了一篇软文来推广它(其实主要是为了弥补我把enterprise版扣下来后内心的愧疚...) ag-Grid是速度最快,功能最丰富的JavaScript dat ...