CF796A - Buying A House

题目描述

Zane the wizard had never loved anyone before, until he fell in love with a girl, whose name remains unknown to us.

The girl lives in house m of a village. There are n houses in that village, lining in a straight line from left to right: house 1, house 2, …, house n. The village is also well-structured: house i and house i + 1 (1 ≤ i < n) are exactly 10 meters away. In this village, some houses are occupied, and some are not. Indeed, unoccupied houses can be purchased.

You will be given n integers a1, a2, …, an that denote the availability and the prices of the houses. If house i is occupied, and therefore cannot be bought, then ai equals 0. Otherwise, house i can be bought, and ai represents the money required to buy it, in dollars.

As Zane has only k dollars to spare, it becomes a challenge for him to choose the house to purchase, so that he could live as near as possible to his crush. Help Zane determine the minimum distance from his crush’s house to some house he can afford, to help him succeed in his love.

输入

The first line contains three integers n, m, and k (2 ≤ n ≤ 100, 1 ≤ m ≤ n, 1 ≤ k ≤ 100) — the number of houses in the village, the house where the girl lives, and the amount of money Zane has (in dollars), respectively.

The second line contains n integers a1, a2, …, an (0 ≤ ai ≤ 100) — denoting the availability and the prices of the houses.

It is guaranteed that am = 0 and that it is possible to purchase some house with no more than k dollars.

输出

Print one integer — the minimum distance, in meters, from the house where the girl Zane likes lives to the house Zane can buy.

样例

Input

1
2
5 1 20
0 27 32 21 19

Output

1
40

Input

1
2
7 3 50
62 0 0 0 99 33 22

Output

1
30

Input

1
2
10 5 100
1 0 1 0 0 0 0 0 1 1

Output

1
20

注意

In the first sample, with k = 20 dollars, Zane can buy only house 5. The distance from house m = 1 to house 5 is 10 + 10 + 10 + 10 = 40 meters.

In the second sample, Zane can buy houses 6 and 7. It is better to buy house 6 than house 7, since house m = 3 and house 6 are only 30 meters away, while house m = 3 and house 7 are 40 meters away.

题解思路

水题,村庄有n个房子,女孩住在第m个房子里,每两个房子相距10米。

可以购买并且距离女孩最近的房子与女孩的距离。

从女孩所住的房子向左右两边依次判断即可。

AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
int n, m, k; //村子房子数、女孩住的房子、钱
int cnt = 0; //统计距离最近的是第几个房子
int price[100] = { 0 }; //存储各个房子的价格

cin >> n >> m >> k;

for (int i = 0; i < n; i++)
{
cin >> price[i];
}

while (1)
{
if (m - 1 - cnt >= 0 && price[m - 1 - cnt] <= k && price[m - 1 - cnt] != 0) //判断左侧房子
{
cout << (10 * cnt) << endl;
break;
}
else if (m - 1 + cnt <= n - 1 && price[m - 1 + cnt] <= k && price[m - 1 + cnt] != 0) //判断右侧房子
{
cout << (10 * cnt) << endl;
break;
}

cnt++;
}

return 0;
}

English单词积累

denote—标志

CF796B - Find The Bone

题目描述

Zane the wizard is going to perform a magic show shuffling the cups.

There are n cups, numbered from 1 to n, placed along the x-axis on a table that has m holes on it. More precisely, cup i is on the table at the position x = i.

The problematic bone is initially at the position x = 1. Zane will confuse the audience by swapping the cups k times, the i-th time of which involves the cups at the positions x = ui and x = vi. If the bone happens to be at the position where there is a hole at any time, it will fall into the hole onto the ground and will not be affected by future swapping operations.

Do not forget that Zane is a wizard. When he swaps the cups, he does not move them ordinarily. Instead, he teleports the cups (along with the bone, if it is inside) to the intended positions. Therefore, for example, when he swaps the cup at x = 4 and the one at x = 6, they will not be at the position x = 5 at any moment during the operation.

Zane’s puppy, Inzane, is in trouble. Zane is away on his vacation, and Inzane cannot find his beloved bone, as it would be too exhausting to try opening all the cups. Inzane knows that the Codeforces community has successfully helped Zane, so he wants to see if it could help him solve his problem too. Help Inzane determine the final position of the bone.

输入

The first line contains three integers n, m, and k (2 ≤ n ≤ 10^6^, 1 ≤ m ≤ n, 1 ≤ k ≤ 3·10^5^) — the number of cups, the number of holes on the table, and the number of swapping operations, respectively.

The second line contains m distinct integers h1, h2, …, hm (1 ≤ hi ≤ n) — the positions along the x-axis where there is a hole on the table.

Each of the next k lines contains two integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi) — the positions of the cups to be swapped.

输出

Print one integer — the final position along the x-axis of the bone.

样例

Input

1
2
3
4
5
6
7 3 4
3 4 6
1 2
2 5
5 7
7 1

Output

1
1

Input

1
2
3
4
5 1 2
2
1 2
2 4

Output

1
2

注意

In the first sample, after the operations, the bone becomes at x = 2, x = 5, x = 7, and x = 1, respectively.

In the second sample, after the first operation, the bone becomes at x = 2, and falls into the hole onto the ground.

题解思路

题目有坑,TLE两次。首先需要考虑一种特殊情况,可以减少运行时间。其次,必须使用stdio或者使用下列语句,否则TLE:

1
std::ios::sync_with_stdio(false);

题意也不难,只要考虑上述两个点就很容易AC了。桌子上有n个杯子,m个洞,骨头在第一个杯子里。

然后模拟交换杯子。直到骨头掉进洞里,求骨头最后掉落在哪个洞里。

AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <algorithm>
#include <stdio.h>

using namespace std;

int hole[1000000] = { 0 }; //存储洞的序号
int main()
{
int n, m, k;
int now = 0;

scanf("%d %d %d", &n, &m, &k); //n个杯子 m个洞 k次交换

for (int i = 0; i < m; i++)
{
int tmp = 0;
scanf("%d", &tmp);
hole[tmp - 1] = 1; //设置该位置有洞
}

if (hole[0] != 1) //这是一个需要注意的点,没有这个会超时:若骨头刚开始就在洞里不需要模拟操作。
{
for (int i = 0; i < k; i++)
{
int a, b;

scanf("%d %d", &a, &b); //直接用cin会导致超时

if (a == now + 1) //考虑a和b的顺序
{
if (hole[b - 1] == 1) //掉进洞里,结束
{
now = b - 1;
break;
}
else
{
now = b - 1; //骨头现在的位置
}
}
else if (b == now + 1)
{
if (hole[a - 1] == 1) //掉进洞里,结束
{
now = a - 1;
break;
}
else
{
now = a - 1; //骨头现在的位置
}
}
}
}

cout << now + 1 << endl;

return 0;
}

English单词积累

shuffling—洗牌

x-axis—x轴

teleports—传送

intended—预定

CF796C - Bank Hacking

CF796D - Police Stations

CF796E - Exam Cheating

CF796F - Sequence Recovery