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.
55 2 5 1 4
20 34 2
610 20 20 40 60 60
0
2101 100
10 1
问如何在n次交换之内将原数组排序成一个递增数组。
分析:每次交换都使得一位换到其对应的位子即可。n^2
1 #include <iostream> 2 #include <sstream> 3 #include <ios> 4 #include <iomanip> 5 #include <functional> 6 #include <algorithm> 7 #include <vector> 8 #include <string> 9 #include <list> 10 #include <queue> 11 #include <deque> 12 #include <stack> 13 #include <set> 14 #include <map> 15 #include <cstdio> 16 #include <cstdlib> 17 #include <cmath> 18 #include <cstring> 19 #include <climits> 20 #include <cctype> 21 using namespace std; 22 #define XINF INT_MAX 23 #define INF 0x3FFFFFFF 24 #define MP(X,Y) make_pair(X,Y) 25 #define PB(X) push_back(X) 26 #define REP(X,N) for(int X=0;X<N;X++) 27 #define REP2(X,L,R) for(int X=L;X<=R;X++) 28 #define DEP(X,R,L) for(int X=R;X>=L;X--) 29 #define CLR(A,X) memset(A,X,sizeof(A)) 30 #define IT iterator 31 typedef long long ll; 32 typedef pair<int,int> PII; 33 typedef vector<PII> VII; 34 typedef vector<int> VI; 35 int a[3010],id[3010]; 36 37 vector<pair<int,int> >v; 38 int main() 39 { 40 ios::sync_with_stdio(false); 41 int n; 42 while(cin>>n) 43 { 44 v.clear(); 45 for(int i=0;i<n;i++)cin>>a[i]; 46 int minn=0; 47 for(int i=0;i<n-1;i++) 48 { 49 minn=i; 50 for(int j=i+1;j<n;j++) 51 { 52 if(a[j]<a[minn]) 53 { 54 minn=j; 55 } 56 } 57 if(i!=minn) 58 { 59 swap(a[i],a[minn]); 60 v.PB(MP(i,minn)); 61 } 62 } 63 cout<<v.size()<<endl; 64 for(int i=0;i<v.size();i++) 65 { 66 cout<<v[i].first<<" "<<v[i].second<<endl; 67 } 68 69 } 70 return 0; 71 }
View Code
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.
41 4 6 255 1 5 7 9
3
41 2 3 4410 11 12 13
0
51 1 1 1 131 2 3
2
题意:男女生结对,要求相互结对的男女的skill值相差不得超过1,问,最多能组几队。
方法一:贪心,男女生分别排序一下,而后每次去一个男生,找到满足要求的skill值最小的女生,组队,若无符合要求的,则该男生不组队。
方法二:二分图最大匹配,skill值差一的男女生间加一条边,而后求最大匹配
1 #include <iostream> 2 #include <sstream> 3 #include <ios> 4 #include <iomanip> 5 #include <functional> 6 #include <algorithm> 7 #include <vector> 8 #include <string> 9 #include <list> 10 #include <queue> 11 #include <deque> 12 #include <stack> 13 #include <set> 14 #include <map> 15 #include <cstdio> 16 #include <cstdlib> 17 #include <cmath> 18 #include <cstring> 19 #include <climits> 20 #include <cctype> 21 using namespace std; 22 #define XINF INT_MAX 23 #define INF 0x3FFFFFFF 24 #define MP(X,Y) make_pair(X,Y) 25 #define PB(X) push_back(X) 26 #define REP(X,N) for(int X=0;X<N;X++) 27 #define REP2(X,L,R) for(int X=L;X<=R;X++) 28 #define DEP(X,R,L) for(int X=R;X>=L;X--) 29 #define CLR(A,X) memset(A,X,sizeof(A)) 30 #define IT iterator 31 typedef long long ll; 32 typedef pair<int,int> PII; 33 typedef vector<PII> VII; 34 typedef vector<int> VI; 35 int V; 36 const int MAX_V=310; 37 vector<int> G[MAX_V]; 38 int match[MAX_V]; 39 bool used[MAX_V]; 40 41 void add_edge(int u,int v) 42 { 43 G[u].PB(v); 44 G[v].PB(u); 45 } 46 47 bool dfs(int v)//增广路 48 { 49 used[v]=1; 50 for(int i=0;i<G[v].size();i++) 51 { 52 int u=G[v][i],w=match[u]; 53 if(w<0||!used[w]&&dfs(w)) 54 { 55 match[u]=v; 56 match[v]=u; 57 return 1; 58 } 59 } 60 return false ; 61 } 62 63 int hungary() 64 { 65 int res=0; 66 CLR(match,-1); 67 for(int v=0;v<V;v++) 68 { 69 if(match[v]<0) 70 { 71 CLR(used,0); 72 if(dfs(v)) 73 { 74 res++; 75 } 76 } 77 } 78 return res; 79 } 80 81 82 int a[110],b[110]; 83 int main() 84 { 85 ios::sync_with_stdio(false); 86 int n,m; 87 while(cin>>n) 88 { 89 for(int i=0;i<n;i++)cin>>a[i]; 90 cin>>m; 91 for(int i=0;i<m;i++)cin>>b[i]; 92 V=n+m; 93 for(int i=0;i<V;i++)G[i].clear(); 94 for(int i=0;i<n;i++) 95 { 96 for(int j=0;j<m;j++) 97 { 98 if(abs(a[i]-b[j])<=1) 99 { 100 add_edge(i,n+j); 101 } 102 } 103 } 104 cout<<hungary()<<endl; 105 } 106 return 0; 107 }
View Code
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;
1 #include <iostream> 2 #include <sstream> 3 #include <ios> 4 #include <iomanip> 5 #include <functional> 6 #include <algorithm> 7 #include <vector> 8 #include <string> 9 #include <list> 10 #include <queue> 11 #include <deque> 12 #include <stack> 13 #include <set> 14 #include <map> 15 #include <cstdio> 16 #include <cstdlib> 17 #include <cmath> 18 #include <cstring> 19 #include <climits> 20 #include <cctype> 21 using namespace std; 22 #define XINF INT_MAX 23 #define INF 0x3FFFFFFF 24 #define MP(X,Y) make_pair(X,Y) 25 #define PB(X) push_back(X) 26 #define REP(X,N) for(int X=0;X<N;X++) 27 #define REP2(X,L,R) for(int X=L;X<=R;X++) 28 #define DEP(X,R,L) for(int X=R;X>=L;X--) 29 #define CLR(A,X) memset(A,X,sizeof(A)) 30 #define IT iterator 31 typedef long long ll; 32 typedef pair<int,int> PII; 33 typedef vector<PII> VII; 34 typedef vector<int> VI; 35 36 int a[110]; 37 int main() 38 { 39 ios::sync_with_stdio(false); 40 int n,m; 41 while(cin>>n>>m) 42 { 43 int temp=m; 44 if(m>9*n||(m<1&&n!=1))cout<<-1<<" "<<-1<<endl; 45 else 46 { 47 for(int i=n;i>0;i--) 48 { 49 if(m-9>0) 50 { 51 a[i]=9; 52 m-=9; 53 } 54 else if(i!=1) 55 { 56 a[i]=m-1; 57 m=1; 58 } 59 else 60 { 61 a[i]=m; 62 } 63 } 64 for(int i=1;i<=n;i++) 65 { 66 cout<<a[i]; 67 } 68 cout<<" "; 69 m=temp; 70 for(int i=1;i<=n;i++) 71 { 72 if(m>9)a[i]=9; 73 else a[i]=m; 74 m-=a[i]; 75 } 76 for(int i=1;i<=n;i++) 77 { 78 cout<<a[i]; 79 } 80 cout<<endl; 81 } 82 } 83 return 0; 84 }
View Code
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 41 22 31 44 3
1
4 121 21 31 42 12 32 43 13 23 44 14 24 3
12
题意:求满足要求的菱形的数目。
枚举菱形的起点和终点两个结点,每次找出中间结点的数目cnt,则这两个结点为起点和终点的方案数C(cnt,2)。不断累加即可。
由于边数不超过30000
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector> 5 using namespace std; 6 const int maxn=3010; 7 vector<int>g[maxn]; 8 vector<int>rg[maxn]; 9 int deg[maxn]; 10 int main() 11 { 12 ios::sync_with_stdio(false); 13 int n,m; 14 while(cin>>n>>m) 15 { 16 int u,v; 17 for(int i=0;i<m;i++) 18 { 19 cin>>u>>v; 20 u--,v--; 21 g[u].push_back(v); 22 rg[v].push_back(u); 23 } 24 long long ans=0; 25 for(int i=0;i<n;i++) 26 { 27 memset(deg,0,sizeof(deg)); 28 for(int j=0;j<g[i].size();j++)deg[g[i][j]]++; 29 30 for(int j=0;j<n;j++) 31 { 32 long long cnt=0; 33 if(i==j)continue; 34 for(int k=0;k<rg[j].size();k++) 35 { 36 if(deg[rg[j][k]])cnt++; 37 } 38 if(cnt>1)ans+=(cnt-1)*cnt/2; 39 } 40 41 } 42 cout<<ans<<endl; 43 44 } 45 return 0; 46 }
View Code
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 910 1020 1030 131 540 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
函数为单调函数,则可用二分。
不断二分答案然后判断此答案下的最佳方案所产生的误差即可
1 #include <iostream> 2 #include <cstring> 3 #include <cmath> 4 #include <cstdio> 5 using namespace std; 6 int n,l; 7 const int maxn=1010; 8 const double eps=1e-12; 9 int d[maxn],p[maxn]; 10 double dp[maxn]; 11 int pre[maxn]; 12 double check(double mid) 13 { 14 memset(pre,0,sizeof(0)); 15 for(int i=1;i<=n;i++) 16 { 17 dp[i]=1e10; 18 for(int j=0;j<i;j++) 19 { 20 double temp=dp[j]+sqrt(abs(.0+d[i]-d[j]-l))-mid*p[i]; 21 if(temp<dp[i]) 22 { 23 dp[i]=temp; 24 pre[i]=j; 25 } 26 } 27 } 28 return dp[n]; 29 } 30 void dfs(int x) 31 { 32 if(pre[x])dfs(pre[x]); 33 cout<<x<<" "; 34 } 35 36 int main() 37 { 38 ios::sync_with_stdio(false); 39 //freopen("a.in","r",stdin); 40 cin>>n>>l; 41 for(int i=1;i<=n;i++)cin>>d[i]>>p[i]; 42 double l=0,r=1e10; 43 while(r-l>eps) 44 { 45 double mid=(l+r)/2.0; 46 if(check(mid)>eps)l=mid; 47 else r=mid; 48 } 49 dfs(n); 50 cout<<endl; 51 return 0; 52 }
View Code
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 1000011
2
4 4 1005000110101001011001
1
For the first test the required matrices are:
011101110 011110101
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;
注意好边界即可
1 #include <iostream> 2 #include <sstream> 3 #include <ios> 4 #include <iomanip> 5 #include <functional> 6 #include <algorithm> 7 #include <vector> 8 #include <string> 9 #include <list> 10 #include <queue> 11 #include <deque> 12 #include <stack> 13 #include <set> 14 #include <map> 15 #include <cstdio> 16 #include <cstdlib> 17 #include <cmath> 18 #include <cstring> 19 #include <climits> 20 #include <cctype> 21 using namespace std; 22 #define XINF INT_MAX 23 #define INF 0x3FFFFFFF 24 #define MP(X,Y) make_pair(X,Y) 25 #define PB(X) push_back(X) 26 #define REP(X,N) for(int X=0;X<N;X++) 27 #define REP2(X,L,R) for(int X=L;X<=R;X++) 28 #define DEP(X,R,L) for(int X=R;X>=L;X--) 29 #define CLR(A,X) memset(A,X,sizeof(A)) 30 #define IT iterator 31 typedef long long ll; 32 typedef pair<int,int> PII; 33 typedef vector<PII> VII; 34 typedef vector<int> VI; 35 ll dp[510][510]; 36 int deg[510]; 37 int x,y; 38 ll n,m,MOD; 39 ll dfs(ll a,ll b) 40 { 41 if(a<0||b<y)return 0; 42 if(dp[a][b]>=0)return dp[a][b]; 43 if(b==y&&a<x)return 0; 44 dp[a][b]=0; 45 dp[a][b]+=dfs(a+2,b-2)*(a+2)*(a+1)/2; 46 dp[a][b]%=MOD; 47 dp[a][b]+=dfs(a,b-1)*a*(n-a-b+1); 48 dp[a][b]%=MOD; 49 dp[a][b]+=dfs(a-2,b)*(n-a-b+2)*(n-a-b+1)/2; 50 dp[a][b]%=MOD; 51 return dp[a][b]; 52 } 53 int main() 54 { 55 ios::sync_with_stdio(false); 56 while(cin>>n>>m>>MOD) 57 { 58 int a=0,b; 59 char ch; 60 for(int i=0;i<m;i++) 61 { 62 b=0; 63 for(int j=0;j<n;j++) 64 { 65 cin>>ch; 66 if(ch=='1') 67 deg[j]++,b++; 68 } 69 if(b!=2)a=1; 70 } 71 if(a) 72 { 73 cout<<0<<endl; 74 continue; 75 } 76 CLR(dp,-1); 77 a=0,b=0; 78 for(int i=0;i<n;i++) 79 { 80 if(deg[i]==1)a++; 81 else if(deg[i]==2)b++; 82 } 83 dp[a][b]=1; 84 x=a,y=b; 85 cout<<dfs(0,n)<<endl; 86 } 87 return 0; 88 }
View Code
转载于:https://www.cnblogs.com/fraud/p/4109303.html
Codeforces Round #277.5 (Div. 2) A,B,C,D,E,F题解相关推荐
- 贪心+构造 Codeforces Round #277 (Div. 2) C. Palindrome Transformation
题目传送门 1 /* 2 贪心+构造:因为是对称的,可以全都左一半考虑,过程很简单,但是能想到就很难了 3 */ 4 /**************************************** ...
- Codeforces Round #277 (Div. 2) 题解
Codeforces Round #277 (Div. 2) A. Calculating Function time limit per test 1 second memory limit per ...
- Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)切题报告(A-B题)
这是我第二次参加CF比赛,也是第一次写博客,写得不好,望各位大佬海涵. A. Single Wildcard Pattern Matching 原题链接 这道题,这是难到让人无法直视,太水了. 题目大 ...
- [区间记忆化dp入门][Bribe the Prisoners SPOJ - GCJ1C09C][Codeforces Round #505D (rated, Div. 1 + Div. 2, ba]
Bribe the Prisoners SPOJ - GCJ1C09C 作为这类题代表,f[i][j]代表第i点到第j点单独处理的最值 这题关键:释放某个囚犯后,就把囚犯分成两段,两段互相独立 这类d ...
- Codeforces Round #626(Div.2) 解题报告
Codeforces Round #626 (Div. 2, based on Moscow Open Olympiad in Informatics) [前言] 最近沉迷只狼,好久没有打CF了,水平 ...
- Codeforces Round #277 (Div. 2) A. Calculating Function 水题
A. Calculating Function Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/4 ...
- Codeforces Round #277(Div 2) A、B、C、D、E题解
转载请注明出处: http://www.cnblogs.com/fraud/ --by fraud A. Calculating Function 水题,判个奇偶即可 1 #inc ...
- Codeforces Round #277 (Div. 2) C. Palindrome Transformations 贪心
思路:只需处理字符串的一半,即:根据右半段来修改左半段.因为同时修改对称的两个字符,所需的操作数与修改一个字符是相等的.为了使操作数最小,应该按顺序来修改. 由于对称性,可将pos > mid的 ...
- Codeforces Round #304 C(Div. 2)(模拟)
题目链接: http://codeforces.com/problemset/problem/546/C 题意: 总共有n张牌,1手中有k1张分别为:x1, x2, x3, ..xk1,2手中有k2张 ...
最新文章
- arm7汇编中怎么使用c中的变量_MDK-ARM armasm汇编器——内建变量和常量.pdf
- aix oracle 10.2.0.1 升级 10.2.0.4,安装oracle 10.2.0.4 on aix 6.1的不同之处
- 电脑经典的小技巧48条
- python epoll多路复用技术_python:多路复用+零拷贝
- codevs 1085 数字游戏 dp或者暴搜
- 为什么 select count(*) from t,在 InnoDB 引擎中比 MyISAM 慢?
- SQL反模式笔记7——多列属性
- 四大数据库的比较(SQL Server、Oracle、Sybase和DB2)
- 工控设备 如何将数据发送到串口_嵌入式无风扇工控机在水质监测系统中的应用...
- Vue 页面如何利用生命周期函数监听用户预览时长
- [css] 说说你对字母“X“在CSS中有什么作用?
- dos命令测试网络连通情况
- 巴铁 无人驾驶_巴铁骗局再现 深圳无人巴士谎言又来忽悠人
- ElasticDL:首个基于 TensorFlow 实现弹性深度学习的开源系统
- 数据结构复习之【线性表】
- 当微信遇上 10 万战绩的「跳一跳」外挂,程序员还能“逍遥”多久?
- hex2bin附源代码
- 2020 APP批量头像,高质量严选真实用户头像
- Winform实现微信功能
- android检测蜂窝网络,Android 判断网络状态及开启网路