Problems - Codeforces › 295 › f18 › 180905-problems.pdfProblems - Codeforces 9/5/18, 608 PM...

6
9/5/18, 6)08 PM Problems - Codeforces Page 1 of 6 file:///Users/sleator/Sync/contest-work/our-server/180905/problems.html 15-295 Fall 2018 #2 A. QAQ 1 second, 256 megabytes "QAQ" is a word to denote an expression of crying. Imagine "Q" as eyes with tears and "A" as a mouth. Now Diamond has given Bort a string consisting of only uppercase English letters of length n. There is a great number of "QAQ" in the string (Diamond is so cute!). illustration by https://twitter.com/nekoyaliu Bort wants to know how many subsequences "QAQ" are in the string Diamond has given. Note that the letters "QAQ" don't have to be consecutive, but the order of letters should be exact. Input The only line contains a string of length n (1 ≤ n ≤ 100). It's guaranteed that the string only contains uppercase English letters. Output Print a single integer — the number of subsequences "QAQ" in the string. input QAQAQYSYIOIWIN input QAQQQZZYNOIWIN In the first example there are 4 subsequences "QAQ": "QAQAQYSYIOIWIN", "QAQAQYSYIOIWIN", "QAQAQYSYIOIWIN", "QAQAQYSYIOIWIN". output 4 output 3 B. Woodcutters 1 second, 256 megabytes Little Susie listens to fairy tales before bed every day. Today's fairy tale was about wood cutters and the little girl immediately started imagining the choppers cutting wood. She imagined the situation that is described below. There are n trees located along the road at points with coordinates x , x , ..., x . Each tree has its height h . Woodcutters can cut down a tree and fell it to the left or to the right. After that it occupies one of the segments 1 2 n i

Transcript of Problems - Codeforces › 295 › f18 › 180905-problems.pdfProblems - Codeforces 9/5/18, 608 PM...

Page 1: Problems - Codeforces › 295 › f18 › 180905-problems.pdfProblems - Codeforces 9/5/18, 608 PM file:///Users/sleator/Sync/contest-work/our-server/180905/problems.html Page 2 of

9/5/18, 6)08 PMProblems - Codeforces

Page 1 of 6file:///Users/sleator/Sync/contest-work/our-server/180905/problems.html

15-295 Fall 2018 #2

A. QAQ1 second, 256 megabytes

"QAQ" is a word to denote an expression of crying. Imagine "Q" as eyes with tears and "A" as a mouth.

Now Diamond has given Bort a string consisting of only uppercase English letters of length n. There is a greatnumber of "QAQ" in the string (Diamond is so cute!).

illustration by https://twitter.com/nekoyaliu

Bort wants to know how many subsequences "QAQ" are in the string Diamond has given. Note that the letters"QAQ" don't have to be consecutive, but the order of letters should be exact.

InputThe only line contains a string of length n (1 ≤ n ≤ 100). It's guaranteed that the string only contains uppercaseEnglish letters.

OutputPrint a single integer — the number of subsequences "QAQ" in the string.

inputQAQAQYSYIOIWIN

inputQAQQQZZYNOIWIN

In the first example there are 4 subsequences "QAQ": "QAQAQYSYIOIWIN", "QAQAQYSYIOIWIN","QAQAQYSYIOIWIN", "QAQAQYSYIOIWIN".

output4

output3

B. Woodcutters1 second, 256 megabytes

Little Susie listens to fairy tales before bed every day. Today's fairy tale was about wood cutters and the little girlimmediately started imagining the choppers cutting wood. She imagined the situation that is described below.

There are n trees located along the road at points with coordinates x , x , ..., x . Each tree has its height h .Woodcutters can cut down a tree and fell it to the left or to the right. After that it occupies one of the segments

1 2 n i

Page 2: Problems - Codeforces › 295 › f18 › 180905-problems.pdfProblems - Codeforces 9/5/18, 608 PM file:///Users/sleator/Sync/contest-work/our-server/180905/problems.html Page 2 of

9/5/18, 6)08 PMProblems - Codeforces

Page 2 of 6file:///Users/sleator/Sync/contest-work/our-server/180905/problems.html

Woodcutters can cut down a tree and fell it to the left or to the right. After that it occupies one of the segments[x  - h , x ] or [x ;x  + h ]. The tree that is not cut down occupies a single point with coordinate x . Woodcutterscan fell a tree if the segment to be occupied by the fallen tree doesn't contain any occupied point. Thewoodcutters want to process as many trees as possible, so Susie wonders, what is the maximum number of treesto fell.

InputThe first line contains integer n (1 ≤ n ≤ 10 ) — the number of trees.

Next n lines contain pairs of integers x , h (1 ≤ x , h  ≤ 10 ) — the coordinate and the height of the і-th tree.

The pairs are given in the order of ascending x . No two trees are located at the point with the same coordinate.

OutputPrint a single number — the maximum number of trees that you can cut down by the given rules.

input51221510109191

input51221510109201

In the first sample you can fell the trees like that:

fell the 1-st tree to the left — now it occupies segment [ - 1;1]fell the 2-nd tree to the right — now it occupies segment [2;3]leave the 3-rd tree — it occupies point 5leave the 4-th tree — it occupies point 10fell the 5-th tree to the right — now it occupies segment [19;20]

In the second sample you can also fell 4-th tree to the right, after that it will occupy segment [10;19].

i i i i i i i

5

i i i i9

i

output3

output4

C. Zuma2 seconds, 512 megabytes

Genos recently installed the game Zuma on his phone. In Zuma there exists a line of n gemstones, the i-th ofwhich has color c . The goal of the game is to destroy all the gemstones in the line as quickly as possible.

In one second, Genos is able to choose exactly one continuous substring of colored gemstones that is apalindrome and remove it from the line. After the substring is removed, the remaining gemstones shift to form asolid line again. What is the minimum number of seconds needed to destroy the entire line?

Let us remind, that the string (or substring) is called palindrome, if it reads same backwards or forward. In ourcase this means the color of the first gemstone is equal to the color of the last one, the color of the secondgemstone is equal to the color of the next to last and so on.

Input

i

Page 3: Problems - Codeforces › 295 › f18 › 180905-problems.pdfProblems - Codeforces 9/5/18, 608 PM file:///Users/sleator/Sync/contest-work/our-server/180905/problems.html Page 2 of

9/5/18, 6)08 PMProblems - Codeforces

Page 3 of 6file:///Users/sleator/Sync/contest-work/our-server/180905/problems.html

InputThe first line of input contains a single integer n (1 ≤ n ≤ 500) — the number of gemstones.

The second line contains n space-separated integers, the i-th of which is c (1 ≤ c  ≤ n) — the color of the i-thgemstone in a line.

OutputPrint a single integer — the minimum number of seconds needed to destroy the entire line.

input3121

input3123

input71442321

In the first sample, Genos can destroy the entire line in one second.

In the second sample, Genos can only destroy one gemstone at a time, so destroying three gemstones takesthree seconds.

In the third sample, to achieve the optimal time of two seconds, destroy palindrome 4 4 first and then destroypalindrome 1 2 3 2 1.

i i

output1

output3

output2

D. Red-Green Towers2 seconds, 256 megabytes

There are r red and g green blocks for construction of the red-green tower. Red-green tower can be builtfollowing next rules:

Red-green tower is consisting of some number of levels;

Let the red-green tower consist of n levels, then the first level of this tower should consist of n blocks, secondlevel — of n - 1 blocks, the third one — of n - 2 blocks, and so on — the last level of such tower shouldconsist of the one block. In other words, each successive level should contain one block less than theprevious one;

Each level of the red-green tower should contain blocks of the same color.

Page 4: Problems - Codeforces › 295 › f18 › 180905-problems.pdfProblems - Codeforces 9/5/18, 608 PM file:///Users/sleator/Sync/contest-work/our-server/180905/problems.html Page 2 of

9/5/18, 6)08 PMProblems - Codeforces

Page 4 of 6file:///Users/sleator/Sync/contest-work/our-server/180905/problems.html

Let h be the maximum possible number of levels of red-green tower, that can be built out of r red and g greenblocks meeting the rules above. The task is to determine how many different red-green towers having h levels canbe built out of the available blocks.

Two red-green towers are considered different if there exists some level, that consists of red blocks in the onetower and consists of green blocks in the other tower.

You are to write a program that will find the number of different red-green towers of height h modulo 10  + 7.

InputThe only line of input contains two integers r and g, separated by a single space — the number of available redand green blocks respectively (0 ≤ r, g ≤ 2·10 , r + g ≥ 1).

OutputOutput the only integer — the number of different possible red-green towers of height h modulo 10  + 7.

input46

input97

input11

The image in the problem statement shows all possible red-green towers for the first sample.

9

5

9

output2

output6

output2

E. Wilbur and Trees2 seconds, 256 megabytes

Wilbur the pig really wants to be a beaver, so he decided today to pretend he is a beaver and bite at trees to cutthem down.

There are n trees located at various positions on a line. Tree i is located at position x . All the given positions ofthe trees are distinct.

The trees are equal, i.e. each tree has height h. Due to the wind, when a tree is cut down, it either falls left withprobability p, or falls right with probability 1 - p. If a tree hits another tree while falling, that tree will fall in the samedirection as the tree that hit it. A tree can hit another tree only if the distance between them is strictly less than h.

For example, imagine there are 4 trees located at positions 1, 3, 5 and 8, while h = 3 and the tree at position 1falls right. It hits the tree at position 3 and it starts to fall too. In it's turn it hits the tree at position 5 and it alsostarts to fall. The distance between 8 and 5 is exactly 3, so the tree at position 8 will not fall.

As long as there are still trees standing, Wilbur will select either the leftmost standing tree with probability 0.5 orthe rightmost standing tree with probability 0.5. Selected tree is then cut down. If there is only one tree remaining,Wilbur always selects it. As the ground is covered with grass, Wilbur wants to know the expected total length ofthe ground covered with fallen trees after he cuts them all down because he is concerned about his grass-eatingcow friends. Please help Wilbur.

i

Page 5: Problems - Codeforces › 295 › f18 › 180905-problems.pdfProblems - Codeforces 9/5/18, 608 PM file:///Users/sleator/Sync/contest-work/our-server/180905/problems.html Page 2 of

9/5/18, 6)08 PMProblems - Codeforces

Page 5 of 6file:///Users/sleator/Sync/contest-work/our-server/180905/problems.html

InputThe first line of the input contains two integers, n (1 ≤ n ≤ 2000) and h (1 ≤ h ≤ 10 ) and a real number p(0 ≤ p ≤ 1), given with no more than six decimal places.

The second line of the input contains n integers, x , x , ..., x ( - 10  ≤ x  ≤ 10 ) in no particular order.

OutputPrint a single real number — the expected total length of the ground covered by trees when they have all fallendown. Your answer will be considered correct if its absolute or relative error does not exceed 10 .

Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consideryour answer correct, if .

input220.50000012

input

430.44312

Consider the first example, we have 2 trees with height 2.

There are 3 scenarios:

1. Both trees falls left. This can either happen with the right tree falling left first, which has probability

(also knocking down the left tree), or the left tree can fall left and then the right tree can fall left, which has

probability. Total probability is .

2. Both trees fall right. This is analogous to (1), so the probability of this happening is .

3. The left tree fall left and the right tree falls right. This is the only remaining scenario so it must have

probability.

Cases 1 and 2 lead to a total of 3 units of ground covered, while case 3 leads to a total of 4 units of ground

covered. Thus, the expected value is .

8

1 2 n8

i8

 - 6

output3.250000000

output6.631200000

F. Painting The Wall

Page 6: Problems - Codeforces › 295 › f18 › 180905-problems.pdfProblems - Codeforces 9/5/18, 608 PM file:///Users/sleator/Sync/contest-work/our-server/180905/problems.html Page 2 of

9/5/18, 6)08 PMProblems - Codeforces

Page 6 of 6file:///Users/sleator/Sync/contest-work/our-server/180905/problems.html

Codeforces (c) Copyright 2010-2018 Mike MirzayanovThe only programming contests Web 2.0 platform

1 second, 256 megabytes

User ainta decided to paint a wall. The wall consists of n tiles, that are arranged in an n × n table. Some tiles arepainted, and the others are not. As he wants to paint it beautifully, he will follow the rules below.

1. Firstly user ainta looks at the wall. If there is at least one painted cell on each row and at least one painted cellon each column, he stops coloring. Otherwise, he goes to step 2.

2. User ainta choose any tile on the wall with uniform probability.3. If the tile he has chosen is not painted, he paints the tile. Otherwise, he ignores it.4. Then he takes a rest for one minute even if he doesn't paint the tile. And then ainta goes to step 1.

However ainta is worried if it would take too much time to finish this work. So he wants to calculate the expectedtime needed to paint the wall by the method above. Help him find the expected time. You can assume thatchoosing and painting any tile consumes no time at all.

InputThe first line contains two integers n and m (1 ≤ n ≤ 2·10 ; 0 ≤ m ≤ min(n , 2·10 )) — the size of the wall andthe number of painted cells.

Next m lines goes, each contains two integers r and c (1 ≤ r , c  ≤ n) — the position of the painted cell. It isguaranteed that the positions are all distinct. Consider the rows of the table are numbered from 1 to n. Considerthe columns of the table are numbered from 1 to n.

OutputIn a single line print the expected time to paint the wall in minutes. Your answer will be considered correct if it hasat most 10 absolute or relative error.

input522341

input

221112

input1111

2

3 2 4

i i i i

 - 4

output

11.7669491886

output2.0000000000

output

0.0000000000