Sei sulla pagina 1di 17

A.

Boredom
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Alex doesn't like boredom. That's why whenever he gets bored, he comes up with
games. One long winter evening he came up with a game and decided to play it.

Given a sequence a consisting of n integers. The player can make several steps.
In a single step he can choose an element of the sequence (let's denote it ak) and
delete it, at that all elements equal to ak+1 and ak-1 also must be deleted from
the sequence. That step brings ak points to the player.

Alex is a perfectionist, so he decided to get as many points as possible. Help


him.
Input

The first line contains integer n (1n105) that shows how many numbers are
in Alex's sequence.

The second line contains n integers a1, a2, ..., an (1ai105).


Output

Print a single integer the maximum number of points that Alex can earn.

Examples
input
2
12
output
2

input
3
123
output
4

input
9
121322223
output
10

Note

Consider the third test example. At first step we need to choose any element
equal to 2. After that step our sequence looks like this [2,2,2,2]. Then we
do 4 steps, on each step we choose any element equals to 2. In total we
earn 10points.
Task :: z-01paths
Little Z is positioned in one of four a bit strange set of rooms with a teleport
machine in each of them. Rooms are named A, B, C and D. In each room
there are two buttons, which we will label as 0 and 1. Pressing 0 in the room
A teleports you to the room C and vice versa; pressing 0 in the room B
teleports you to the room D and vice versa. Pressing 1 in the room A
teleports you to the room B and vice versa; pressing 1 in the room C
teleports you to the room D and vice versa. These teleports are also shown
on the image below.

Pressing exactly n times button 0 or button 1 we will call n-length 01-path. It


is obvious that after each n-length 01-path Little Z will end in some room.
Little Z would like to finish at some of the four given rooms by pressing
buttons 0 and buttons 1. He is interested in K-th lexicographically
smallest n-length 01-path such that he ends up in the room R.
Little Z will always start at room A.

INPUT:
The first line of the standard input contains character R, one of the upper-
case letters A, B, C or D, and two space-separated integers n (1 <= n <=
50) and K (1 <= K <= 10<sup>9</sup>).

OUTPUT:
To the first line of the standard ouput you should print K-th lexicographically
smallest n-length 01-path if such exists, else print "impossible" (without
quotes).

Input:
B 3 2

Output:
010

Explanation:
All possible paths, sorted lexicographically, are: 000, 001, 010, 011, 100,
101, 110 and 111.
Paths 001 (A -> C -> A -> B), 010 (A -> C -> D -> B), 100 (A -> B -> D -> B)
and 111 (A -> B -> A -> B) are those that finish at B. Second one is 010.

Input:
D 1 1

Output:
impossible

Explanation:
It is not possible to get in D in only one move.
Mahmoud wrote a message s of length n. He wants to send it as a birthday
present to his friend Moaz who likes strings. He wrote it on a magical paper but
he was surprised because some characters disappeared while writing the string.
That's because this magical paper doesn't allow character number i in the
English alphabet to be written on it in a string of length more than ai. For
example, if a1=2 he can't write character 'a' on this paper in a string of
length 3 or more. String "aa" is allowed while string "aaa" is not.

Mahmoud decided to split the message into some non-empty substrings so that
he can write every substring on an independent magical paper and fulfill the
condition. The sum of their lengths should be n and they shouldn't overlap. For
example, if a1=2 and he wants to send string "aaa", he can split it into "a" and
"aa" and use 2 magical papers, or into "a", "a" and "a" and use 3 magical
papers. He can't split it into "aa" and "aa" because the sum of their lengths is
greater than n. He can split the message into single string if it fulfills the
conditions.

A substring of string s is a string that consists of some consecutive characters


from string s, strings "ab", "abc" and "b" are substrings of string "abc", while
strings "acb" and "ac" are not. Any string is a substring of itself.

While Mahmoud was thinking of how to split the message, Ehab told him that
there are many ways to split it. After that Mahmoud asked you three questions:
How many ways are there to split the string into substrings such that every
substring fulfills the condition of the magical paper, the sum of their lengths
is n and they don't overlap? Compute the answer modulo 109+7.
What is the maximum length of a substring that can appear in some valid
splitting?
What is the minimum number of substrings the message can be spit in?
Two ways are considered different, if the sets of split positions differ. For example,
splitting "aa|a" and "a|aa" are considered different splittings of message
"aaa".
Input

The first line contains an integer n (1n103) denoting the length of the
message.

The second line contains the message s of length n that consists of lowercase
English letters.

The third line contains 26 integers a1,a2,...,a26 (1ax103) the maximum


lengths of substring each letter can appear in.
Output

Print three lines.

In the first line print the number of ways to split the message into substrings and
fulfill the conditions mentioned in the problem modulo 109+7.

In the second line print the length of the longest substring over all the ways.

In the third line print the minimum number of substrings over all the ways.

Examples
input
3
aab
23111111111111111111111111
output
3
2
2

input
10
abcdeabcde
55554111111111111111111111
output
401
4
3

Note

In the first example the three ways to split the message are:
a|a|b
aa|b
a|ab

The longest substrings are "aa" and "ab" of length 2.

The minimum number of substrings is 2 in "a|ab" or "aa|b".

Notice that "aab" is not a possible splitting because the letter 'a' appears in a
substring of length 3, while a1=2.
B. Kuriyama Mirai's Stones
time limit per test
2 seconds
memory limit per test
input
standard input
output
standard output

Kuriyama Mirai has killed many monsters and got many (namely n) stones. She
numbers the stones from 1 to n. The cost of the i-th stone is vi. Kuriyama Mirai
wants to know something about these stones so she will ask you two kinds of
questions:

1. She will tell you two numbers, l and r (1lrn), and you should tell

her .
2. Let ui be the cost of the i-th cheapest stone (the cost that will be on the i-th
place if we arrange all the stone costs in non-decreasing order). This time
she will tell you two numbers, l and r (1lrn), and you should tell

her .

For every question you should give the correct answer, or Kuriyama Mirai will say
"fuyukai desu" and then become unhappy.
Input
The first line contains an integer n (1n105). The second line
contains n integers: v1,v2,...,vn (1vi109) costs of the stones.

The third line contains an integer m (1m105) the number of Kuriyama


Mirai's questions. Then follow m lines, each line contains three
integers type, l and r (1lrn; 1type2), describing a question.
If type equal to 1, then you should output the answer for the first question, else
you should output the answer for the second one.
Output

Print m lines. Each line must contain an integer the answer to Kuriyama
Mirai's question. Print the answers to the questions in the order of input.

Examples
input
6
6 42727
3
2 36
1 34
1 16
output
24
9
28

input
4
55 23
10
12 4
21 4
11 1
21 4
21 2
11 1
13 3
11 3
144
122
output
10
15
5
15
5
5
2
12
3
5

Please note that the answers to the questions may overflow 32-bit integer type.

C. New Year and Domino


time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

They say "years are like dominoes, tumbling one after the other". But would a
year fit into a grid? I don't think so.

Limak is a little polar bear who loves to play. He has recently got a rectangular
grid with h rows and w columns. Each cell is a square, either empty (denoted by
'.') or forbidden (denoted by '#'). Rows are numbered 1 through h from top to
bottom. Columns are numbered 1 through w from left to right.

Also, Limak has a single domino. He wants to put it somewhere in a grid. A


domino will occupy exactly two adjacent cells, located either in one row or in one
column. Both adjacent cells must be empty and must be inside a grid.
Limak needs more fun and thus he is going to consider some queries. In each
query he chooses some rectangle and wonders, how many way are there to put a
single domino inside of the chosen rectangle?
Input

The first line of the input contains two integers h and w (1h,w500) the
number of rows and the number of columns, respectively.

The next h lines describe a grid. Each line contains a string of the length w. Each
character is either '.' or '#' denoting an empty or forbidden cell, respectively.

The next line contains a single integer q (1q100000) the number of


queries.

Each of the next q lines contains four integers r1i, c1i, r2i, c2i (1r1ir2i
h,1c1ic2iw) the i-th query. Numbers r1i and c1i denote the row and
the column (respectively) of the upper left cell of the rectangle.
Numbers r2i and c2i denote the row and the column (respectively) of the bottom
right cell of the rectangle.
Output

Print q integers, i-th should be equal to the number of ways to put a single
domino inside the i-th rectangle.

Examples
input
58
....#..#
.#......
##.#....
##..#.##
........
4
1123
4141
1245
2558
output
4
0
10
15

input
7 39
.......................................
.###..###..#..###.....###..###..#..###.
...#..#.#..#..#.........#..#.#..#..#...
.###..#.#..#..###.....###..#.#..#..###.
.#....#.#..#....#.....#....#.#..#..#.#.
.###..###..#..###.....###..###..#..###.
.......................................
6
1 1 3 20
2 10 6 30
2 10 7 30
2277
1777
1878
output
53
89
120
23
0
2

Note

A red frame below corresponds to the first query of the first sample. A domino
can be placed in 4 possible ways.
A. DZY Loves Sequences
time limit per test
1 second
memory limit per test
256 megabytes
input

DZY has a sequence a, consisting of n integers.

We'll call a sequence ai,ai+1,...,aj (1ijn) a subsegment of the


sequence a. The value (j-i+1) denotes the length of the subsegment.
Your task is to find the longest subsegment of a, such that it is possible to
change at most one number (change one number to any integer you want) from
the subsegment to make the subsegment strictly increasing.

You only need to output the length of the subsegment you find.
Input

The first line contains integer n (1n105). The next line


contains n integers a1,a2,...,an (1ai109).
Output

In a single line print the answer to the problem the maximum length of the
required subsegment.

Examples
input
6
723156
output
5

Note

You can choose subsegment a2,a3,a4,a5,a6 and change its 3rd element (that
is a4) to 4.

E. Tetrahedron
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a tetrahedron. Let's mark its vertices with


letters A, B, C and D correspondingly.

An ant is standing in the vertex D of the tetrahedron. The ant is quite active and
he wouldn't stay idle. At each moment of time he makes a step from one vertex to
another one along some edge of the tetrahedron. The ant just can't stand on one
place.

You do not have to do much to solve the problem: your task is to count the
number of ways in which the ant can go from the initial vertex D to itself in
exactly n steps. In other words, you are asked to find out the number of different
cyclic paths with the length of n from vertex D to itself. As the number can be
quite large, you should print it modulo 1000000007 (109+7).
Input

The first line contains the only integer n (1n107) the required length of
the cyclic path.
Output
Print the only integer the required number of ways modulo 1000000007 (109
+7).
Examples
input
2
output
3

input
4
output
21

Note

The required paths in the first sample are:


D-A-D
D-B-D
D-C-D

solution :

Div2 C/Div1 A. Bracket sequence


You were given a bracket sequence s consisting of brackets of two kinds. You
were to find regular bracket sequence that was a substring of s and contains as
many << >> braces as possible.

We will try to determine corresponding closing bracket for every opening one.
Formally, let a bracket on the i-th position be opening, then the closing bracket on
the position j is corresponding to it if and only if a substring si... sj is the shortest
regular bracket sequence that begins from the i-th position. In common case
there can be brackets with no corresponding ones.

We scan the sting s and put positions with the opening brackets into a stack. Let
us proceed the i-th position. If si is an opening bracket we simply put i on the top
of the stack. Otherwise, we have to clean the stack if the stack is empty or the
bracket on the top does not correspond to the current one. But if the bracket on
the top is ok we just remove the top of the stack and remember that the bracket
on position i is corresponding to the bracket removed from the top. So, we find all
the correspondings for all the brackets.

Then we can split s into blocks. Let block be a segment [l,r] such that the
bracket on the r-th position is corresponding for the bracket on the i-th and there
is no couple of corresponding brackets on positions x and y such
that and [l,r][x,y]. It is easy to understand that the blocks do
not intersect and the split is unique. We can join the consequent blocks into the
regular bracket sequences. We should join as many blocks as possible in order
to get the maximal number of braces. We get several substrings that are regular
bracket sequences after we join all the consecutive blocks. The answer is the
substring that has the largest amount of braces << >>. The complexity is O(|s|).

Potrebbero piacerti anche