Codeforces Round #277.5 (Div. 2) partial question
A. SwapSorttime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output
In this problem your goal is to sort an array consistingNIntegers in at mostNSwaps. For the given array find the sequence of swaps that makes the array sorted in the non-descending order. Swaps are saved med 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 longerN.
Input
The first line of the input contains integerN(1? ≤?N? ≤? 3000)-the number of array elements. The second line contains elements of array:A0 ,?A1 ,?...,?AN? -? 1 (? -? 109? ≤?AI? ≤? 109), whereAIIsI-Th element of the array. The elements are numerated from 0N? -? 1 from left to right. Some integers may appear in the array more than once.
Output
In the first line printK(0? ≤?K? ≤?N)-The number of swaps. NextKLines must contain the descriptions ofKSwaps, one per line. Each swap shocould be printed as a pair of integersI,J(0? ≤?I,?J? ≤?N? -? 1), representing the swap of elementsAIAndAJ. You can print indices in the pairs in any order. The swaps are saved med in the order they appear in the output, from the first to the last. It is allowed to printI? =?JAnd 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
55 2 5 1 4
Output
20 34 2
Input
610 20 20 40 60 60
Output
0
Input
2101 100
Output
10 1
Greedy after sorting
/*************************************** * *********************************> File Name: cf. cpp> Author: acvlinoleic> QQ:> Mail: acvcla@gmail.com> Created Time: monday, November 17, 2014, 13 seconds ******************************** **************************************** /# include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
Using namespace std; typedef long LL; const int maxn = 3e3 + 10; # define rep (I, a, B) for (int I = (); I <= (B); I ++) # define pb push_backint A [maxn], B [maxn]; std: vector
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
> A [I]; B [I] = A [I];} sort (B, B + n); for (int I = 0; I
B. BerSU Balltime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output
The Berland State University is hosting a ballroom dance in celebration of its 100500-th anniversary!NBoys andMGirls 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 fromNBoys andMGirls.
Input
The first line contains an integerN(1? ≤?N? ≤? 100)-the number of boys. The second line contains sequenceA1 ,?A2 ,?...,?AN(1? ≤?AI? ≤? 100), whereAIIsI-Th boy's dancing skill.
Similarly, the third line contains an integerM(1? ≤?M? ≤? 100)-the number of girls. The fourth line contains sequenceB1 ,?B2 ,?...,?BM(1? ≤?BJ? ≤? 100), whereBJIsJ-Th girl's dancing skill.
Output
Print a single number-the required maximum possible number of pairs.
Sample test (s) input
41 4 6 255 1 5 7 9
Output
3
Input
41 2 3 4410 11 12 13
Output
0
Input
51 1 1 1 131 2 3
Output
2
Just greedy.
/*************************************** * *********************************> File Name: cf. cpp> Author: acvlinoleic> QQ:> Mail: acvcla@gmail.com> Created Time: monday, November 17, 2014, 13 seconds ******************************** **************************************** /# include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
Using namespace std; typedef long LL; const int maxn = 1e3 + 10; # define rep (I, a, B) for (int I = (); I <= (B); I ++) # define pb push_backint 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 <
C. Given Length and Sum of Digits... time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output
You have a positive integerMAnd a non-negative integerS. Your task is to find the smallest and the largest of the numbers that have lengthMAnd sum of digitsS. The required numbers shocould be non-negative integers written in the decimal base without leading zeroes.
Input
The single line of the input contains a pair of integersM,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
Greedy and detailed
/*************************************** * *********************************> File Name: cf. cpp> Author: acvlinoleic> QQ:> Mail: acvcla@gmail.com> Created Time: monday, November 17, 2014, 13 seconds ******************************** **************************************** /# include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
Using namespace std; typedef long LL; const int maxn = 1e5 + 10; # define rep (I, a, B) for (int I = (); I <= (B); I ++) # define pb push_backint 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" <
1 | s> m * 9) {cout <"-1-1" <
0; I --) ans1 [I] = '0'; ans2 [t1] = '0' + d; for (int I = t1 + 1; I
D. Unbearable Controversy of Beingtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard 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 intersectionsA,B,CAndD, Such that there are two paths fromAToC-One throughBAnd the other one throughD, He callthe group a "damn rhombus". Note that pairs (A,?B),(B,?C),(A,?D),(D,?C) Shoshould be directly connected by the roads. Schematically, a damn fig is shown on the figure below:
Other roads between any of the intersections don "t make the fig any more appealing to Tomash, so the four intersections remain a" damn fig "for him.
Given that the capital of Berland hasNIntersections andMRoads and all roads are unidirectional and are known in advance, find the number of "damn rhombi" in the city.
When fig are compared, the order of intersectionsBAndDDoesn' t matter.
Input
The first line of the input contains a pair of integersN,M(1? ≤?N? ≤? 3000 ,? 0? ≤?M? ≤? (30000)-the number of intersections and roads, respectively. NextMLines list the roads, one per line. Each of the roads is given by a pair of integersAI,?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 ctions.
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 41 22 31 44 3
Output
1
Input
4 121 21 31 42 12 32 43 13 23 44 14 24 3
Output
12
It is good to be violent. If there are x paths with 2 lengths between u and v, and x> 1, then the quadrilateral with u and v as the starting point and the ending point is c (x, 2)
/*************************************** * *********************************> File Name: cf. cpp> Author: acvlinoleic> QQ:> Mail: acvcla@gmail.com> Created Time: monday, November 17, 2014, 13 seconds ******************************** **************************************** /# include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
Using namespace std; typedef long LL; const int maxn = 3e3 + 10; # define rep (I, a, B) for (int I = (); I <= (B); I ++) # define pb push_backint n, m; std: vector
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
> 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 <
F. Special Matricestime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output
AnN? ×?NSquare 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 givenNAnd the firstMRows of the matrix. Print the number of specialN? ×?NMatrices, such that the firstMRows coincide with the given ones.
As the required value can be rather large, print the remainder after dividing the value by the given numberMod.
InputThe first line of the input contains three integersN,M,Mod(2? ≤?N? ≤? 500, 0? ≤?M? ≤?N, 2? ≤?Mod? ≤? 109). ThenMLines follow, each of them containsNCharacters-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 givenM? ×?NTable contains at most two numbers one.
OutputPrint the remainder after dividing the required value by numberMod.
Sample test (s) input3 1 1000011
Output2
Input4 4 1005000110101001011001
Output1
NoteFor the first test the required matrices are:
011101110011110101
In the second test the required matrix is already fully given, so the answer is 1.
Based on Row dp, because the sum of each row must be 2, you can select two columns and add them at the position of 1 or 0 when creating a row. Until the final column and all are 2.Specific Code
/*************************************** * *********************************> File Name: cf. cpp> Author: acvlinoleic> QQ:> Mail: acvcla@gmail.com> Created Time: monday, November 17, 2014, 13 seconds ******************************** **************************************** /# include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
Using namespace std; typedef long LL; const int maxn = 500 + 10; # define rep (I, a, B) for (int I = (); I <= (B); I ++) # define pb push_backLL 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) {// currently, the x column is 1, and the y column is 0. The number of solutions that reach the target status, this is because the sum of each line must be 2if (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; // select the two columns for 1 to add 1, in this case, the sum of 1 is the X-2, and the sum of 0 is yif (y> = 2) d [x] [y] + = cn2 (y) * dfs (x + 2, y-2) % mod; // select the 0 column and Add 1 to it. The sum of 1 is x + 2, for 0 is y-2if (x> = 1 & y> = 1) d [x] [y] + = x * y * dfs (x, Y-1) % mod; // select one of the columns plus 1, and the sum of 1 is x, and the sum of 0 is the y-1return 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
2) {cout <0 <