Practice_Codeforces Round #408 (Div. 2)

来源:互联网 发布:公司美工属于什么部门 编辑:程序博客网 时间:2024/06/06 03:01

蒟蒻的水题之路

好像又只是做了A+B呀,我好菜啊


A. Buying A House
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

The first line contains three integers nm, and k (2 ≤ n ≤ 1001 ≤ m ≤ n1 ≤ 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.

Output

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

Examples
input
5 1 200 27 32 21 19
output
40
input
7 3 5062 0 0 0 99 33 22
output
30
input
10 5 1001 0 1 0 0 0 0 0 1 1
output
20

题目链接:http://codeforces.com/contest/796/problem/A

题意:给定一个字符串、一个位置和一个数,找出该数大于数组中的数(0除外)的位置离给定位置的最近距离。

题解:暴力即可。

弱者的代码:

#include <iostream>#include <cstdlib>using namespace std;int a[100];const int INF = 0x3f3f3f3f;int main(){ios::sync_with_stdio(false);cin.tie(0);int n, m, k, ans = INF;cin >> n >> m >> k;for (int i = 1; i <= n; i++)cin >> a[i];for (int i = 1; i <= n; i++)if(k >= a[i] && a[i] != 0)ans = min(ans, abs(m-i));cout << ans*10 << endl;return 0;


B. Find The Bone
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

The first line contains three integers nm, and k (2 ≤ n ≤ 1061 ≤ m ≤ n1 ≤ k ≤ 3·105) — 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 ≤ nui ≠ vi) — the positions of the cups to be swapped.

Output

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

Examples
input
7 3 43 4 61 22 55 77 1
output
1
input
5 1 221 22 4
output
2
Note

In the first sample, after the operations, the bone becomes at x = 2x = 5x = 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.


题目链接:http://codeforces.com/contest/796/problem/B

题意:有n个位置,m次操作,每次操作方式是把将两个位置的杯子互换,同时在k个位置上有一个洞,该位置上的杯子里面若有骨头,则骨头会从杯子中掉下来,初始骨头的位置在1,问m次操作后骨头会在哪个位置。

题解:

This is another implementation problem.

Let’s create an array a of length n (with initial values set to 0), and set ai = 1 only for the positions x = i where there is a hole.

If there is a hole at x  =  1, obviously, the answer is 1, because the ball must fall onto the ground before any operation is applied.

Otherwise, consider each swapping operation chronologically (from the first to the last).

While doing so, we will also maintain a variable pos, the position where the ball is.

Let the involving cup positions be u and v. There are three cases to consider:

1) If pos is equal to u, set pos  =  v.

2) If pos is equal to v, set pos  =  u.

3) Otherwise, skip the operation.

Make sure to stop considering any more operations if apos equals 1.

After this procedure, pos will be the final answer.

This solution runs in O(k).

(看不懂就谷歌翻译一下...)

题坑:首先是1本身可能是有洞的,一开始的操作不一定从1开始,有些操作是无用功,有时候虽然换了位置,但是会在后面可能再换回来。

数组开小W了一发GG.


代码:

#include <iostream>#include <cstring>using namespace std;const int maxn = 1000010;bool a[maxn];int main(){    ios::sync_with_stdio(false);    cin.tie(0);    int n, m, k, t;    cin >> n >> m >> k;    memset(a, 0, sizeof(a));    for (int i = 1; i <= m; i++)    {        cin >> t;        a[t] = true;    }    int u, v, pos = 1;    bool f = false;    if (a[1])        f = true;    for (int i = 1; i <= k; i++)    {        cin >> u >> v;        if((pos == u || pos == v) && !f)            pos = u + v - pos;        if(a[pos])            f = true;    }    cout << pos << endl;    return 0;}

The end.

0 0
原创粉丝点击