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 ...
随机推荐
- 初识CLR
眨眼间我已经实习了半年时间并且转正了,身份也正式从一个学生转变为一个职场人,这个博客自从开始实习以来就一直没有更新过= =没错,就是我懒癌晚期,不过不行!一切都要开始走向正轨,此博会继续见证我的成长, ...
- 记录android开发博客
1.一国外android开发博客,值得关注 https://blog.stylingandroid.com/page/2/ 2.一个app设计博客,很赞 http://androidniceties. ...
- 帝国cms实现自动生成缩略图和自动分页功能
无论你手工发布,还是采集而来,免不了要进行手工操作弄缩略图,不然标题图片没有,挺烦人的 只需一次设定,就可以在文章编辑框里自动勾选上分页和生成缩略图,免除你次次进行操作的麻烦,好了,废话不多说,上菜“ ...
- Django数据迁移
http://www.ziqiangxuetang.com/django/django-data-migration.html
- 栈应用之中缀表达式计算 MFC实现(计算器核心)
大家好,我是小鸭酱,博客地址为:http://www.cnblogs.com/xiaoyajiang 支持小数.阶乘.乘方.加减乘除.括号优先级运算,美化输出结果(显示结果末尾没有多余的0) void ...
- tableViewCell 的删除按钮
- (UITableViewCellEditingStyle)tableView:(UITableView*)tableView editingStyleForRowAtIndexPath:(NSIn ...
- 【转】sqlmap用户手册
http://192.168.136.131/sqlmap/mysql/get_int.php?id=1 当给sqlmap这么一个url的时候,它会: 1.判断可注入的参数2.判断可以用那种SQL注入 ...
- 手把手教你清除WIN7的C盘垃圾
WIN7系统用着用着C盘会变得越来越大,可用空间变得越来越小,磁盘清理,和安全卫士怎么清也清不出这些系统深度的垃圾.我们可以手动删除,释放C盘空间. 这样一清理下来,结果我的C盘就释放了近10个GB的 ...
- SGU176 Flow construction
http://acm.sgu.ru/problem.php?contest=0&problem=176 有源汇上下界最小流,可以选择跟有源汇上下界最大流一样的建图方式,然后用二分去二分t-&g ...
- qt捕获全局windows消息(使用QAbstractNativeEventFilter,然后注册这个类)
qt 如何捕获全屏的鼠标事件,这个帖子上面主要讲述了下嵌入式qt怎么抓取系统级消息,不过从这篇文章中我也看到了希望,有个回复说winEventFilter支持这种方式,然后我就顺着这个线索找到了na ...