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 second
memory limit per test      256 megabytes
input      standard input
output      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.

Input

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.

Output

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.

Sample test(s)
Input
5
5 2 5 1 4
Output
2
0 3
4 2
Input
6
10 20 20 40 60 60
Output
0
Input
2
101 100
Output
1
0 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
 
B. BerSU Ball
time limit per test  1 second
memory limit per test  256 megabytes
input  standard input
output  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.

Input

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.

Output

Print a single number — the required maximum possible number of pairs.

Sample test(s)
Input
4
1 4 6 2
5
5 1 5 7 9
Output
3
Input
4
1 2 3 4
4
10 11 12 13
Output
0
Input
5
1 1 1 1 1
3
1 2 3
Output
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
C. Given Length and Sum of Digits...
time limit per test  1 second
memory limit per test  256 megabytes
input  standard input
output  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.

Input

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.

Output

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).

Sample test(s)
Input
2 15
Output
69 96
Input
3 0
Output
-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
D. Unbearable Controversy of Being
time limit per test  1 second
memory limit per test  256 megabytes
input  standard input
output  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.

Input

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.

Output

Print the required number of "damn rhombi".

Sample test(s)
Input
5 4
1 2
2 3
1 4
4 3
Output
1
Input
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
Output
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
E. Hiking
time limit per test  1 second
memory limit per test  256 megabytes
input  standard input
output  standard output

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.

Input

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.

Output

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.

Sample test(s)
Input
5 9
10 10
20 10
30 1
31 5
40 10
Output
1 2 4 5 
Note

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
F. Special Matrices
time limit per test  1 second
memory limit per test  256 megabytes
input  standard input
output  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 number mod.

Input

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.

Output

Print the remainder after dividing the required value by number mod.

Sample test(s)
Input
3 1 1000
011
Output
2
Input
4 4 100500
0110
1010
0101
1001
Output
1
Note

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;

注意好边界即可

 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