您的位置:首页 > 其它

Codeforces Round #361 (Div. 2)

2016-07-07 15:46 363 查看
A. Mike and Cellphone

time limit per test
1 second

memory limit per test
256 megabytes

input
standard input

output
standard output

While swimming at the beach, Mike has accidentally dropped his cellphone into the water. There was no worry as he bought a cheap replacement phone with an old-fashioned keyboard. The keyboard has only ten digital equal-sized keys, located in the following way:



Together with his old phone, he lost all his contacts and now he can only remember the way his fingers moved when he put some number in. One can formally consider finger
movements as a sequence of vectors connecting centers of keys pressed consecutively to put in a number. For example, the finger movements for number "586" are the same as finger movements for number "253":





Mike has already put in a number by his "finger memory" and started calling it, so he is now worrying, can he be sure that he is calling the correct number? In other words, is there
any other number, that has the same finger movements?

Input
The first line of the input contains the only integer n (1 ≤ n ≤ 9) —
the number of digits in the phone number that Mike put in.
The second line contains the string consisting of n digits
(characters from '0' to '9') representing the number that
Mike put in.

Output
If there is no other phone number with the same finger movements and Mike can be sure he is calling the correct number, print "YES"
(without quotes) in the only line.
Otherwise print "NO" (without quotes) in the first line.

Examples

input
3
586


output
NO


input
2
09


output
NO


input
9
123456789


output
YES


input
3
911


output
YES


Note
You can find the picture clarifying the first sample case in the statement above.
啊,这次好悲伤,爆零了,打了好几次cf了,这次直接0题啊,心中无限悲伤,感觉这不仅与自己的做题能力有关,还与心态有关,因为A题不会,直接影响了下面的读题,导致后面的题都读不下去,心态也是比赛要锻炼的吧。比赛完看大牛们做的,有点是最后几十分钟有干掉的A题,自己太愚蠢啊,策略,懂不懂,,,
A题没想出来思路啊,这可是A题,刚一看,题意那么长,应该不难,就读了一下,题意也不是很难理解,就是找相同形状的路线,找不到YES,反之NO。
开始我怎么想的呢,将这些数一起向四个方向平移,如果还在棋盘内,就是NO了。然后,思想错误方向,,,用dfs将这些数扫一遍,dfs吧感觉不好写,但是想不到其他的思路,中间看了下B,然而题意有点长,心态也不好,知道这是个最短路的,但是就是没状态了,so又回来dfs乱搜,悲伤
那么该怎么想呢,直接判断是否还能向四个方向移动就行了,怎么判断呢,用四个标记变量啊,如果有个可以就YES了。说那么多,该反省一下了,因为考试都没怎么刷题了,感觉也没了。

/*
无尽的悲伤,竟然没想到这个做法
*/
#include<bits/stdc++.h>
using namespace std;

int main()
{
char str[30];
int n;
while(~scanf("%d",&n))
{
cin>>str;
int left = 1,right = 1;
int up = 1,down = 1;
for(int i = 0;i < n;i++)
{
if(str[i] == '0')
left = right = down = 0;
if(str[i] == '1' ||str[i] == '2' ||str[i] == '3')
up = 0;
if(str[i] == '1' ||str[i] == '4' ||str[i] == '7')
left = 0;
if(str[i] == '3' ||str[i] == '6' ||str[i] == '9')
right = 0;
if(str[i] == '7' ||str[i] == '9')
down = 0;
}
if(up || down || right || left)
cout<<"NO"<<endl;
else
cout<<"YES"<<endl;
}
}


B. Mike and Shortcuts

time limit per test
3 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

Recently, Mike was very busy with studying for exams and contests. Now he is going to chill a bit by doing some sight seeing in the city.
City consists of n intersections
numbered from 1 to n.
Mike starts walking from his house located at the intersection number 1 and goes along some sequence of intersections.
Walking from intersection number i to intersection j requires |i - j| units
of energy. The total energy spent by Mike to visit a sequence of intersections p1 = 1, p2, ..., pk is
equal to 

 units
of energy.
Of course, walking would be boring if there were no shortcuts. A shortcut is a special
path that allows Mike walking from one intersection to another requiring only 1 unit of energy. There are exactly n shortcuts
in Mike's city, the ith of
them allows walking from intersection ito intersection ai (i ≤ ai ≤ ai + 1)
(but not in the opposite direction), thus there is exactly one shortcut starting at each intersection. Formally, if Mike chooses a sequence p1 = 1, p2, ..., pk then
for each 1 ≤ i < k satisfying pi + 1 = api and api ≠ pi Mike
will spend only 1unit of energy instead
of |pi - pi + 1| walking
from the intersection pi to
intersection pi + 1.
For example, if Mike chooses a sequencep1 = 1, p2 = ap1, p3 = ap2, ..., pk = apk - 1,
he spends exactly k - 1 units of total energy walking around them.
Before going on his adventure, Mike asks you to find the minimum amount of energy required to reach each of the intersections from his home. Formally, for each 1 ≤ i ≤ n Mike
is interested in finding minimum possible total energy of some sequence p1 = 1, p2, ..., pk = i.

Input
The first line contains an integer n (1 ≤ n ≤ 200 000) —
the number of Mike's city intersection.
The second line contains n integers a1, a2, ..., an (i ≤ ai ≤ n , 

,
describing shortcuts of Mike's city, allowing to walk from intersection i to intersection ai using
only 1 unit of energy. Please note that the shortcuts don't allow walking in opposite directions (from ai to i).

Output
In the only line print n integers m1, m2, ..., mn,
where mi denotes
the least amount of total energy required to walk from intersection 1 to intersection i.

Examples

input
3
2 2 3


output
0 1 2


input
5
1 2 3 4 5


output
0 1 2 3 4


input
7
4 4 4 4 7 7 7


output
0 1 2 1 2 3 3


Note
In the first sample case desired sequences are:
1: 1; m1 = 0;
2: 1, 2; m2 = 1;
3: 1, 3; m3 = |3 - 1| = 2.
In the second sample case the sequence for any intersection 1 < i is
always 1, i and mi = |1 - i|.
In the third sample case — consider the following intersection sequences:
1: 1; m1 = 0;
2: 1, 2; m2 = |2 - 1| = 1;
3: 1, 4, 3; m3 = 1 + |4 - 3| = 2;
4: 1, 4; m4 = 1;
5: 1, 4, 5; m5 = 1 + |4 - 5| = 2;
6: 1, 4, 6; m6 = 1 + |4 - 6| = 3;
7: 1, 4, 5, 7; m7 = 1 + |4 - 5| + 1 = 3.

待补充。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: