problems
problems
You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots:
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'. The wheels can rotate freely and wrap around:
for example we can turn '9' to be '0', or '0' to be '9'. Each move consists of turning
one wheel one slot.
The lock initially starts at '0000', a string representing the state of the 4 wheels.
You are given a list of deadends, meaning if the lock displays any of these codes,
the wheels of the lock will stop turning and you will be unable to open it.
Given a target representing the value of the wheels that will unlock the lock, print
the minimum total number of turns required to open the lock, or -1 if it is
impossible.
Input
The first line contains a single integer N (1≤ N ≤ 500) — the number of dead
ends.The next N lines each contain a single string of length 4, consisting of digits
only, representing a deadend.
The third line contains a single string S of length 4 consisting of digits only,
representing the target, and it is guaranteed that S is not in the deadends.
Output
Print in a single line the minimum total number of turns required to open the lock,
or -1 if it is impossible.
2
Example
8 -1
8887
8889
8878
8898
8788
8988
7888
9888
8888
1 1
8888
0009
Note:
In the first sample test :
A sequence of valid moves would be "0000" -> "1000" -> "1100" -> "1200" ->
"1201" -> "1202" -> "0202".
Note that a sequence like "0000" -> "0001" -> "0002" -> "0102" -> "0202" would
be invalid,
because the wheels of the lock become stuck after the display becomes the dead
end "0102".
3
Shaass has decided to hunt some birds. There are n horizontal electricity wires
aligned parallel to each other. Wires are numbered 1 to n from top to bottom. On
each wire there are some oskols sitting next to each other. Oskol is the name of
a delicious kind of birds in Shaass's territory. Supposed there are ai oskols sitting
on the i-th wire.
Sometimes Shaass shots one of the birds and the bird dies (suppose that this
bird sat at the i-th wire). Consequently all the birds on the i-th wire to the left of
the dead bird get scared and jump up on the wire number i - 1, if there exists no
upper wire they fly away. Also all the birds to the right of the dead bird jump down
on wire number i + 1, if there exists no such wire they fly away.
Shaass has shot m birds. You're given the initial number of birds on each wire,
tell him how many birds are sitting on each wire after the shots.
Input
4
The first line of the input contains an integer n, (1 ≤ n ≤ 100). The next line
contains a list of space-separated integers 𝑎 , 𝑎 , ..., 𝑎 , (0 ≤ 𝑎 ≤ 100).
1 2 𝑛 𝑖
The third line contains an integer m, (0 ≤ m ≤ 100). Each of the next m lines
contains two integers 𝑥𝑖 and 𝑦𝑖. The integers mean that for the i-th time Shaass
shoot the 𝑦𝑖-th (from left) bird on the 𝑥 -th wire, (1 ≤ 𝑥𝑖 ≤ n, 1 ≤𝑦𝑖). It's guaranteed
𝑖
Output
On the i-th line of the output print the number of birds on the i-th wire.
Examples
3 3
241 0
1 3
22
5
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 (1 ≤ l ≤ r ≤ n), 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 (1 ≤ l ≤ r ≤ n), 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 (1 ≤ n ≤ 10⁵). The second line contains n
integers: v1, v2, ..., vn (1 ≤ vi ≤ 10⁹) — costs of the stones.
6
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
Standard input Standard input
6 24
642727 9
3 28
236
134
116
4 10
5523 15
10 5
124 15
214 5
111 5
214 2
212 12
111 3
133 5
113
144
122
Note : Please note that the answers to the questions may overflow 32-bit integer
type.
7
D. Magic Elevator
Input : standard input
Output : standard output
Ballon color : Black
Coach Mohamed Abd – Elwahab wants you to take a balloon, so he asks for
your help with this simple problem. The Coach visits a magic building where if
you are on the Nth floor and you ride the magic elevator you will ascend 10
floors.
Input
Only one line contains a number N (1 ≤ N ≤ 90).
Output
Print the number of the floor that the Coach will be on after riding the magic
elevator.
Examples
2 12
8
E. King's Path
Input : standard input
Output : standard output
Ballon color : Orange
The black king is standing on a chess field consisting of 10⁹ rows and 10⁹
columns. We will consider the rows of the field numbered with integers from 1 to
10⁹ from top to bottom. The columns are similarly numbered with integers from 1
to 10⁹ from left to right. We will denote a cell of the field that is located in the i-th
row and j-th column as (i, j).
You know that some squares of the given chess field are allowed. All allowed
cells of the chess field are given as n segments. Each segment is described by
three integers 𝑟𝑖, 𝑎𝑖,𝑏𝑖 (𝑎𝑖 ≤ 𝑏𝑖), denoting that cells in columns from number 𝑎𝑖 to
Your task is to find the minimum number of moves the king needs to get from
square (𝑥0, 𝑦0) to square (𝑥1, 𝑦1), provided that he only moves along the allowed
cells. In other words, the king can be located only on allowed cells on his way.
Let us remind you that a chess king can move to any of the neighboring cells in
one move. Two cells of a chess field are considered neighboring if they share at
least one point.
Input
9
The first line contains four space-separated integers 𝑥0, 𝑦0, 𝑥1, 𝑦1 (1 ≤ 𝑥0, 𝑦0, 𝑥1, 𝑦1
≤ 10⁹), denoting the initial and the final positions of the king.
The second line contains a single integer n (1 ≤ n ≤ 10⁵), denoting the number of
segments of allowed cells. Next n lines contain the descriptions of these
segments. The i-th line contains three space-separated integers 𝑟𝑖 , 𝑎𝑖 , 𝑏𝑖
number 𝑏𝑖 inclusive in the 𝑟𝑖 -th row are allowed. Note that the segments of the
It is guaranteed that the king's initial and final position are allowed cells. It is
guaranteed that the king's initial and the final positions do not coincide. It is
guaranteed that the total length of all given segments doesn't exceed 10⁵.
Output
If there is no path between the initial and final position along allowed cells, print -1.
3 4 3 10 6
3
314
459
3 10 10
1 1 2 10 -1
2
113
2 6 10
10
F. Ice Cave
Input : standard input
Output : standard output
Ballon color : Blue
The level of the cave where you are is a rectangular square grid of n rows and m
columns. Each cell consists either from intact or from cracked ice. From each cell
you can move to cells that are side-adjacent with yours (due to some limitations
of the game engine you cannot make jumps on the same place, i.e. jump from a
cell to itself). If you move to the cell with cracked ice, then your character falls
down through it and if you move to the cell with intact ice, then the ice on this cell
becomes cracked.
Let's number the rows with integers from 1 to n from top to bottom and the
columns with integers from 1 to m from left to right. Let's denote a cell on the
intersection of the r-th row and the c-th column as (r, c).
You are staying in the cell (𝑟1, 𝑐1) and this cell is cracked because you've just
fallen here from a higher level. You need to fall down through the cell (𝑟2, 𝑐2)
since the exit to the next level is there. Can you do this?
Input
The first line contains two integers, n and m (1 ≤ n, m ≤ 500) — the number of rows
and columns in the cave description.
11
Each of the next n lines describes the initial state of the level of the cave, each
line consists of m characters "." (that is, intact ice) and "X" (cracked ice).
coordinates of the cell through which you need to fall. The final cell may coincide
with the starting one.
Output
If you can reach the destination, print 'YES', otherwise print 'NO'.
54 NO
.X..
...X
X.X.
....
.XX.
53
11
12
47 YES
..X.XX.
.XX..X.
X...X..
X......
22
16
Note
In the first sample test one possible path is:
After the first visit of cell (2, 2) the ice on it cracks and when you step there for the
second time, your character falls through the ice as intended.
13
G. House Robber
Input : standard input
Output : standard output
Ballon color : Gold
You are a professional robber planning to rob houses along a street. Each house
has a certain amount of money stashed, the only constraint stopping you from
robbing each of them is that adjacent houses have security systems connected
and it will automatically contact the police if two adjacent houses were broken
into on the same night.
Given an integer array nums representing the amount of money of each house,
print the maximum amount of money you can rob tonight without alerting the
police.
Input:
The first line contains an integers n (1 ≤ n ≤ 100) — the length of the array.
The second line contains n integers separated by spaces (𝑎1 , 𝑎2 , 𝑎3 , ... , 𝑎𝑛) ,
(0 ≤ 𝑎𝑖 ≤ 400).
Output :
Output a single integer — the maximum amount of money you can rob tonight
without alerting the police.
4 4
1231
5 12
27931