Codeforces Round #277.5 (Div. 2) A,B,C,D,E,F题解
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 k swaps, 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
问如何在n次交换之内将原数组排序成一个递增数组。
分析:每次交换都使得一位换到其对应的位子即可。n^2
#include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype>
using namespace std;
#define XINF INT_MAX
#define INF 0x3FFFFFFF
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
typedef long long ll;
typedef pair<int,int> PII;
typedef vector<PII> VII;
typedef vector<int> VI;
int a[],id[]; vector<pair<int,int> >v;
int main()
{
ios::sync_with_stdio(false);
int n;
while(cin>>n)
{
v.clear();
for(int i=;i<n;i++)cin>>a[i];
int minn=;
for(int i=;i<n-;i++)
{
minn=i;
for(int j=i+;j<n;j++)
{
if(a[j]<a[minn])
{
minn=j;
}
}
if(i!=minn)
{
swap(a[i],a[minn]);
v.PB(MP(i,minn));
}
}
cout<<v.size()<<endl;
for(int i=;i<v.size();i++)
{
cout<<v[i].first<<" "<<v[i].second<<endl;
} }
return ;
}
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 sequence a1, 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
题意:男女生结对,要求相互结对的男女的skill值相差不得超过1,问,最多能组几队。
方法一:贪心,男女生分别排序一下,而后每次去一个男生,找到满足要求的skill值最小的女生,组队,若无符合要求的,则该男生不组队。
方法二:二分图最大匹配,skill值差一的男女生间加一条边,而后求最大匹配
#include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype>
using namespace std;
#define XINF INT_MAX
#define INF 0x3FFFFFFF
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
typedef long long ll;
typedef pair<int,int> PII;
typedef vector<PII> VII;
typedef vector<int> VI;
int V;
const int MAX_V=;
vector<int> G[MAX_V];
int match[MAX_V];
bool used[MAX_V]; void add_edge(int u,int v)
{
G[u].PB(v);
G[v].PB(u);
} bool dfs(int v)//增广路
{
used[v]=;
for(int i=;i<G[v].size();i++)
{
int u=G[v][i],w=match[u];
if(w<||!used[w]&&dfs(w))
{
match[u]=v;
match[v]=u;
return ;
}
}
return false ;
} int hungary()
{
int res=;
CLR(match,-);
for(int v=;v<V;v++)
{
if(match[v]<)
{
CLR(used,);
if(dfs(v))
{
res++;
}
}
}
return res;
} int a[],b[];
int main()
{
ios::sync_with_stdio(false);
int n,m;
while(cin>>n)
{
for(int i=;i<n;i++)cin>>a[i];
cin>>m;
for(int i=;i<m;i++)cin>>b[i];
V=n+m;
for(int i=;i<V;i++)G[i].clear();
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
if(abs(a[i]-b[j])<=)
{
add_edge(i,n+j);
}
}
}
cout<<hungary()<<endl;
}
return ;
}
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
题意:找出n位,且各个数位上的数的和为m的最小的数和最大的数。
分析:贪心,当m<1&&n!=1或者m>9*n时,无解,输出-1,否则,最小的数为从后往前贪,尽可能取大的,同时要保证最高位上至少能取得1;最大的数为从前向后贪,尽可能取大的,直到和已经达到m;
#include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype>
using namespace std;
#define XINF INT_MAX
#define INF 0x3FFFFFFF
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
typedef long long ll;
typedef pair<int,int> PII;
typedef vector<PII> VII;
typedef vector<int> VI; int a[];
int main()
{
ios::sync_with_stdio(false);
int n,m;
while(cin>>n>>m)
{
int temp=m;
if(m>*n||(m<&&n!=))cout<<-<<" "<<-<<endl;
else
{
for(int i=n;i>;i--)
{
if(m->)
{
a[i]=;
m-=;
}
else if(i!=)
{
a[i]=m-;
m=;
}
else
{
a[i]=m;
}
}
for(int i=;i<=n;i++)
{
cout<<a[i];
}
cout<<" ";
m=temp;
for(int i=;i<=n;i++)
{
if(m>)a[i]=;
else a[i]=m;
m-=a[i];
}
for(int i=;i<=n;i++)
{
cout<<a[i];
}
cout<<endl;
}
}
return ;
}
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
题意:求满足要求的菱形的数目。
枚举菱形的起点和终点两个结点,每次找出中间结点的数目cnt,则这两个结点为起点和终点的方案数C(cnt,2)。不断累加即可。
由于边数不超过30000
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int maxn=;
vector<int>g[maxn];
vector<int>rg[maxn];
int deg[maxn];
int main()
{
ios::sync_with_stdio(false);
int n,m;
while(cin>>n>>m)
{
int u,v;
for(int i=;i<m;i++)
{
cin>>u>>v;
u--,v--;
g[u].push_back(v);
rg[v].push_back(u);
}
long long ans=;
for(int i=;i<n;i++)
{
memset(deg,,sizeof(deg));
for(int j=;j<g[i].size();j++)deg[g[i][j]]++; for(int j=;j<n;j++)
{
long long cnt=;
if(i==j)continue;
for(int k=;k<rg[j].size();k++)
{
if(deg[rg[j][k]])cnt++;
}
if(cnt>)ans+=(cnt-)*cnt/;
} }
cout<<ans<<endl; }
return ;
}
A traveler is planning a water hike along the river. He noted the suitable rest points for the night and wrote out their distances from the starting point. Each of these locations is further characterized by its picturesqueness, so for the i-th rest point the distance from the start equals xi, and its picturesqueness equals bi. The traveler will move down the river in one direction, we can assume that he will start from point 0 on the coordinate axis and rest points are points with coordinates xi.
Every day the traveler wants to cover the distance l. In practice, it turns out that this is not always possible, because he needs to end each day at one of the resting points. In addition, the traveler is choosing between two desires: cover distance l every day and visit the most picturesque places.
Let's assume that if the traveler covers distance rj in a day, then he feels frustration , and his total frustration over the hike is calculated as the total frustration on all days.
Help him plan the route so as to minimize the relative total frustration: the total frustration divided by the total picturesqueness of all the rest points he used.
The traveler's path must end in the farthest rest point.
The first line of the input contains integers n, l (1 ≤ n ≤ 1000, 1 ≤ l ≤ 105) — the number of rest points and the optimal length of one day path.
Then n lines follow, each line describes one rest point as a pair of integers xi, bi (1 ≤ xi, bi ≤ 106). No two rest points have the same xi, the lines are given in the order of strictly increasing xi.
Print the traveler's path as a sequence of the numbers of the resting points he used in the order he used them. Number the points from 1 to n in the order of increasing xi. The last printed number must be equal to n.
5 9
10 10
20 10
30 1
31 5
40 10
1 2 4 5
In the sample test the minimum value of relative total frustration approximately equals 0.097549. This value can be calculated as
函数为单调函数,则可用二分。
不断二分答案然后判断此答案下的最佳方案所产生的误差即可
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
using namespace std;
int n,l;
const int maxn=;
const double eps=1e-;
int d[maxn],p[maxn];
double dp[maxn];
int pre[maxn];
double check(double mid)
{
memset(pre,,sizeof());
for(int i=;i<=n;i++)
{
dp[i]=1e10;
for(int j=;j<i;j++)
{
double temp=dp[j]+sqrt(abs(.+d[i]-d[j]-l))-mid*p[i];
if(temp<dp[i])
{
dp[i]=temp;
pre[i]=j;
}
}
}
return dp[n];
}
void dfs(int x)
{
if(pre[x])dfs(pre[x]);
cout<<x<<" ";
} int main()
{
ios::sync_with_stdio(false);
//freopen("a.in","r",stdin);
cin>>n>>l;
for(int i=;i<=n;i++)cin>>d[i]>>p[i];
double l=,r=1e10;
while(r-l>eps)
{
double mid=(l+r)/2.0;
if(check(mid)>eps)l=mid;
else r=mid;
}
dfs(n);
cout<<endl;
return ;
}
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 number mod.
The first line of the input contains three integers n, m, mod (2 ≤ n ≤ 500, 0 ≤ m ≤ n, 2 ≤ mod ≤ 109). Then m 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 × n table 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.
题意,对于一个n*n的矩阵,已经给出m行,问填满整个矩阵共有多少方案,其中,矩阵有以下要求:所填元素非0即1,每行每列有且必须有两个1
分析:dp。
dp[i][j]表示填满前(i/2+j)行有i列1个1,j列2个1的矩阵的方案数
递推方程为dp[i][j]=dp[i+2][j-2]*(i+2)*(i+1)/2+dp[i][j-1]*i*(n-i-j+1)+dp[i-2][j]*(n-i-j+2)*(n-i-j+1)/2;
注意好边界即可
#include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype>
using namespace std;
#define XINF INT_MAX
#define INF 0x3FFFFFFF
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
typedef long long ll;
typedef pair<int,int> PII;
typedef vector<PII> VII;
typedef vector<int> VI;
ll dp[][];
int deg[];
int x,y;
ll n,m,MOD;
ll dfs(ll a,ll b)
{
if(a<||b<y)return ;
if(dp[a][b]>=)return dp[a][b];
if(b==y&&a<x)return ;
dp[a][b]=;
dp[a][b]+=dfs(a+,b-)*(a+)*(a+)/;
dp[a][b]%=MOD;
dp[a][b]+=dfs(a,b-)*a*(n-a-b+);
dp[a][b]%=MOD;
dp[a][b]+=dfs(a-,b)*(n-a-b+)*(n-a-b+)/;
dp[a][b]%=MOD;
return dp[a][b];
}
int main()
{
ios::sync_with_stdio(false);
while(cin>>n>>m>>MOD)
{
int a=,b;
char ch;
for(int i=;i<m;i++)
{
b=;
for(int j=;j<n;j++)
{
cin>>ch;
if(ch=='')
deg[j]++,b++;
}
if(b!=)a=;
}
if(a)
{
cout<<<<endl;
continue;
}
CLR(dp,-);
a=,b=;
for(int i=;i<n;i++)
{
if(deg[i]==)a++;
else if(deg[i]==)b++;
}
dp[a][b]=;
x=a,y=b;
cout<<dfs(,n)<<endl;
}
return ;
}
Codeforces Round #277.5 (Div. 2) A,B,C,D,E,F题解的更多相关文章
- 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< ...
- 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 ...
随机推荐
- hdu1406
一道很水很水的题!!!!!!!!!! #include<iostream> using namespace std; int main(){ int num1,num2,i,k,j,sum ...
- Nutch关于robot.txt的处理
在nutch中,默认情况下尊重robot.txt的配置,同时不提供配置项以忽略robot.txt. 以下是其中一个解释.即作为apache的一个开源项目,必须遵循某些规定,同时由于开放了源代码,可以简 ...
- 翻译Oracle文档--SYSDBA和SYSOPER系统权限
SYSDBA和SYSOPER是管理权限,被用户来执行高级管理操作例如:创建 开启/关闭 备份/恢复 数据库.SYSDBA系统权限是针对想给予完全授权的数据库管理员SYSOPER系统权限允许一个用户执行 ...
- 64位Ubuntu14.04搭建ADT开发环境
本来放假是要出去玩的,结果出游计划拖到了周一,今天先来无事看看ubuntu最近发展得怎么样了,顺便把ADT也搭建好,方便以后研究android框架.想想在windows下,ADT直接down下来解压就 ...
- 九章算法系列(#3 Binary Tree & Divide Conquer)-课堂笔记
前言 第一天的算法都还没有缓过来,直接就进入了第二天的算法学习.前一天一直在整理Binary Search的笔记,也没有提前预习一下,好在Binary Tree算是自己最熟的地方了吧(LeetCode ...
- shell中的for、while、until
for var in list do commands done 在每个迭代中,变量var会包含列表中的当前值,第一个迭代会使用列表中的第一个值,第二个迭代使用第二个值. 在do和done中,$var ...
- 开心菜鸟系列学习笔记-------javascript(3)
一.原型链: 1)Object 是一个属性的集合,并且都拥有一个单独的原型对象.,这个原型对象object可以是一个object或者null值 2)不过一般来说,我们会使用__内部属性名__下 ...
- LightOj 1230 Placing Lampposts(树形DP)
题意:给定一个森林.每个节点上安装一个灯可以覆盖与该节点相连的所有边.选择最少的节点数num覆盖所有的边.在num最小的前提下,合理放置num个灯使得被两个灯覆盖的边最多? 思路:F[i][0]代表没 ...
- 画图工具Graphviz安装配置
Graphviz (英文:Graph Visualization Software的缩写)是一个由AT&T实验室启动的开源工具包,用于绘制DOT语言脚本描述的图形.它也提供了供其它软件使用的库 ...
- MessagerService总结
一.整体工程图 二.messenger_service_binding.xml <?xml version="1.0" encoding="utf-8"? ...