Codeforces Round #408 Div.2 A B C D

来源:互联网 发布:java office转换成pdf 编辑:程序博客网 时间:2024/06/08 05:46

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 airepresents 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
Note

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.


水。


#include <cstdio>#include <iostream>using namespace std;typedef long long ll;const int maxn=105;int n,m,k,i,a[maxn],min1,aa,min2;int main(){min2=1<<30;cin >> n >> m >> k;min1=-1<<30;for (i=1;i<=m-1;i++) {cin >> aa;if (aa!=0&&aa<=k) min1=i;}cin >> aa;for (i=m+1;i<=n;i++) {cin >> aa;if (aa!=0&&aa<=k) {min2=i;break;}}cout << min(m-min1,min2-m)*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.


继续水。


#include <cstdio>#include <iostream>#include <string.h>using namespace std;typedef long long ll;const int maxn=1000005;int a[maxn],q,x,y;int main(){int n,m,k,i,pos=1;scanf("%d%d%d",&n,&m,&k);memset(a,0,sizeof(a));for(i=1;i<=m;i++) {scanf("%d",&q);a[q]=1;}if (!a[1]) {for (i=1;i<=k;i++) {scanf("%d%d",&x,&y);if (x==pos) pos=y;else if (y==pos) pos=x;if (a[pos]) break;    }}cout << pos << endl;return 0;}

C. Bank Hacking
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Although Inzane successfully found his beloved bone, Zane, his owner, has yet to return. To search for Zane, he would need a lot of money, of which he sadly has none. To deal with the problem, he has decided to hack the banks.

There are n banks, numbered from 1 to n. There are also n - 1 wires connecting the banks. All banks are initially online. Each bank also has its initial strength: bank i has initial strength ai.

Let us define some keywords before we proceed. Bank i and bank j are neighboring if and only if there exists a wire directly connecting them. Bank i and bank j are semi-neighboring if and only if there exists an online bank k such that bank i and bank k are neighboring and bank k and bank j are neighboring.

When a bank is hacked, it becomes offline (and no longer online), and other banks that are neighboring or semi-neighboring to it have their strengths increased by 1.

To start his plan, Inzane will choose a bank to hack first. Indeed, the strength of such bank must not exceed the strength of his computer. After this, he will repeatedly choose some bank to hack next until all the banks are hacked, but he can continue to hack bank x if and only if all these conditions are met:

  1. Bank x is online. That is, bank x is not hacked yet.
  2. Bank x is neighboring to some offline bank.
  3. The strength of bank x is less than or equal to the strength of Inzane's computer.

Determine the minimum strength of the computer Inzane needs to hack all the banks.

Input

The first line contains one integer n (1 ≤ n ≤ 3·105) — the total number of banks.

The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109) — the strengths of the banks.

Each of the next n - 1 lines contains two integers ui and vi (1 ≤ ui, vi ≤ nui ≠ vi) — meaning that there is a wire directly connecting banks ui and vi.

It is guaranteed that the wires connect the banks in such a way that Inzane can somehow hack all the banks using a computer with appropriate strength.

Output

Print one integer — the minimum strength of the computer Inzane needs to accomplish the goal.

Examples
input
51 2 3 4 51 22 33 44 5
output
5
input
738 -29 87 93 39 28 -551 22 53 22 41 77 6
output
93
input
51 2 7 6 71 55 33 42 4
output
8
Note

In the first sample, Inzane can hack all banks using a computer with strength 5. Here is how:

  • Initially, strengths of the banks are [1, 2, 3, 4, 5].
  • He hacks bank 5, then strengths of the banks become [1, 2, 4, 5,  - ].
  • He hacks bank 4, then strengths of the banks become [1, 3, 5,  - ,  - ].
  • He hacks bank 3, then strengths of the banks become [2, 4,  - ,  - ,  - ].
  • He hacks bank 2, then strengths of the banks become [3,  - ,  - ,  - ,  - ].
  • He completes his goal by hacking bank 1.

In the second sample, Inzane can hack banks 423157, and 6, in this order. This way, he can hack all banks using a computer with strength 93.


这题我卡了很久。

一棵树上n个点,所有点最开始时有一个权值。现在让你依次选点删除,选了一个点之后,所有和这个点距离为1或2的点权值都+1.除了第一次,每次选点只能选和已经删过的点相邻的未删除过的点。问这个过程中,删除的点的最大权值最小是多少。


随便画个图,发现首先攻击的点的权值不变,与第一个点相邻的点权值+1,其他所有点权值+2.

所以,这题先找权值最大是多少,再记录权值为最大权值和最大权值减一的点各有多少个。


设最大权值为max.

当所有权值为max-1的点与唯一的权值最大点相连,答案是max.

当存在一个点和所有权值最大的点相连,答案是max+1.

其余情况,答案是max+2.


这题过的不容易!



#include <cstdio>#include <iostream>#include <queue>#include <vector>#include <string.h>using namespace std;typedef long long ll;const int maxn=300005;int a[maxn],visit[maxn];vector<int> v[maxn];int main() {int n,i,x,y,j,max1,ans,maxi;scanf("%d",&n);max1=-1<<30;if (n==1) {scanf("%d",&a[0]);cout << a[0];return 0;}for (i=1;i<=n;i++) {scanf("%d",&a[i]);if (a[i]>max1) {max1=a[i];maxi=i;    }}int nummax=0,nummax2=0;for (i=1;i<=n;i++) {if (a[i]==max1) nummax++;if (a[i]==max1-1) nummax2++;}ans=max1+2;for (i=1;i<=n-1;i++) {scanf("%d%d",&x,&y);    v[x].push_back(y);    v[y].push_back(x);    }    int num;    if (nummax==1) {    num=0;    for (i=0;i<v[maxi].size();i++)         if (a[v[maxi][i]]==max1-1) num++;        if (num==nummax2) {        ans-=2;             cout << ans;            return 0;        }    }    for (i=1;i<=n;i++) {    num=0;    if (a[i]==max1) num++;    for (j=0;j<v[i].size();j++) {    if (a[v[i][j]]==max1) num++;    }    if (num==nummax) {    ans--;    break;    }    }    cout << ans;return 0;}

D. Police Stations

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

Inzane finally found Zane with a lot of money to spare, so they together decided to establish a country of their own.

Ruling a country is not an easy job. Thieves and terrorists are always ready to ruin the country's peace. To fight back, Zane and Inzane have enacted a very effective law: from each city it must be possible to reach a police station by traveling at most d kilometers along the roads.

There are n cities in the country, numbered from 1 to n, connected only by exactly n - 1 roads. All roads are 1 kilometer long. It is initially possible to travel from a city to any other city using these roads. The country also has k police stations located in some cities. In particular, the city's structure satisfies the requirement enforced by the previously mentioned law. Also note that there can be multiple police stations in one city.

However, Zane feels like having as many as n - 1 roads is unnecessary. The country is having financial issues, so it wants to minimize the road maintenance cost by shutting down as many roads as possible.

Help Zane find the maximum number of roads that can be shut down without breaking the law. Also, help him determine such roads.

Input

The first line contains three integers nk, and d (2 ≤ n ≤ 3·1051 ≤ k ≤ 3·1050 ≤ d ≤ n - 1) — the number of cities, the number of police stations, and the distance limitation in kilometers, respectively.

The second line contains k integers p1, p2, ..., pk (1 ≤ pi ≤ n) — each denoting the city each police station is located in.

The i-th of the following n - 1 lines contains two integers ui and vi (1 ≤ ui, vi ≤ nui ≠ vi) — the cities directly connected by the road with index i.

It is guaranteed that it is possible to travel from one city to any other city using only the roads. Also, it is possible from any city to reach a police station within d kilometers.

Output

In the first line, print one integer s that denotes the maximum number of roads that can be shut down.

In the second line, print s distinct integers, the indices of such roads, in any order.

If there are multiple answers, print any of them.

Examples
input
6 2 41 61 22 33 44 55 6
output
15
input
6 3 21 5 61 21 31 41 55 6
output
24 5 
Note

In the first sample, if you shut down road 5, all cities can still reach a police station within k = 4 kilometers.

In the second sample, although this is the only largest valid set of roads that can be shut down, you can print either 4 5 or 5 4 in the second line.


又是一个树上的题目。

树上n个点k个警察局,要保证所有点到警察局的最近距离不大于d,问最多删去多少边。


bfs。

以k个警察局为起点,bfs树上所有节点。bfs过程中没用到的边,就是可以删除的边。


#include <cstdio>#include <iostream>#include <string.h>#include <string> #include <map>#include <queue>#include <vector>#include <set>#include <algorithm>#include <math.h>#include <cmath>#include <bitset>#define mem0(a) memset(a,0,sizeof(a))#define meminf(a) memset(a,0x3f,sizeof(a))using namespace std;typedef long long ll;typedef long double ld;const int maxn=300005,inf=0x3f3f3f3f;const ll llinf=0x3f3f3f3f3f3f3f3f; const ld pi=acos(-1.0L);int head[maxn],a[maxn];int num;bool visit[maxn],mark[maxn];struct node {int id,dist;node (int id,int dist): id(id),dist(dist) {}};struct Edge {int from,to,pre,id;};Edge edge[maxn*2];void addedge(int from,int to,int id) {edge[num]=(Edge){from,to,head[from],id};head[from]=num++;edge[num]=(Edge){to,from,head[to],id};head[to]=num++;}int main() {num=0;mem0(visit);memset(head,-1,sizeof(head));queue<node> q;int n,k,d,x,y,i;scanf("%d%d%d",&n,&k,&d);for (i=1;i<=k;i++) {scanf("%d",&a[i]);q.push(node(a[i],0));visit[a[i]]=1;}for (i=1;i<n;i++) {scanf("%d%d",&x,&y);addedge(x,y,i);}mem0(mark);while (!q.empty()) {node now=q.front();q.pop();for (int i=head[now.id];i!=-1;i=edge[i].pre) {int to=edge[i].to;if (!visit[to]) {mark[edge[i].id]=visit[to]=1;if (now.dist<d) q.push(node(to,now.dist+1));}}}int ans=0;for (i=1;i<n;i++) {if (!mark[i]) ans++;}printf("%d\n",ans);for (i=1;i<n;i++) {if (!mark[i]) printf("%d ",i);}return 0;}





原创粉丝点击