Codeforces Round #408 Div.2 A B C D
来源:互联网 发布:java office转换成pdf 编辑:程序博客网 时间:2024/06/08 05:46
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.
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.
5 1 200 27 32 21 19
40
7 3 5062 0 0 0 99 33 22
30
10 5 1001 0 1 0 0 0 0 0 1 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.
水。
#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;}
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 ≤ 106, 1 ≤ m ≤ n, 1 ≤ 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 ≤ n, ui ≠ vi) — the positions of the cups to be swapped.
Print one integer — the final position along the x-axis of the bone.
7 3 43 4 61 22 55 77 1
1
5 1 221 22 4
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.
继续水。
#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;}
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:
- Bank x is online. That is, bank x is not hacked yet.
- Bank x is neighboring to some offline bank.
- 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.
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 ≤ n, ui ≠ 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.
Print one integer — the minimum strength of the computer Inzane needs to accomplish the goal.
51 2 3 4 51 22 33 44 5
5
738 -29 87 93 39 28 -551 22 53 22 41 77 6
93
51 2 7 6 71 55 33 42 4
8
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 4, 2, 3, 1, 5, 7, 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;}
又是一个树上的题目。
树上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;}
- Codeforces Round #408 Div.2 A B C D
- Codeforces Round #179 (Div. 2)A、B、C、D
- Codeforces Round #186 (Div. 2)A、B、C、D、E
- Codeforces Round #202 (Div. 2) (A、B、C、D)
- Codeforces Round #211 (Div. 2)(A,B,C,D)
- Codeforces Round #220 (Div. 2)(A,B,C,D)
- Codeforces Round #231 (Div. 2)A, B, C, D
- Codeforces Round #258 (Div. 2)-(A,B,C,D,E)
- Codeforces Round #246 (Div. 2) A,B,C,D
- Codeforces Round #261 (Div. 2) A,B,C,D,E
- Codeforces Round #259 (Div. 2) A/B/C/D
- Codeforces Round #257 (Div. 2) A/B/C/D
- Codeforces Round #256 (Div. 2) A/B/C/D
- Codeforces Round #262 (Div. 2)-A,B,C,D
- Codeforces Round #263 (Div. 2) A,B,C,D,E
- Codeforces Round #264 (Div. 2) A,B,C,D,E
- Codeforces Round #276 (Div. 2) A B C D
- Codeforces Round #277 (Div. 2) A,B,C,D,E
- 利用集合模拟斗地主(2)
- Type interface com.mybatis.mapper.StudentMapper is not known to the MapperRegistry
- 你必须知道的261个Java语言问题笔记- Java流和文件操作2
- Linux 出现-bash: grunt: command not found 错误解决
- mysql5.7安装之后,mysqladmin无法设置root密码
- Codeforces Round #408 Div.2 A B C D
- js中的事件onchange案例
- Hadoop(4-2)-MapReduce程序案例-WordCount(Intellij Idea环境)
- spring boot:No converter found for return value of type: class另类解决办法
- ubuntu16.04安装qt5
- Leetcode655. Print Binary Tree
- codeforces 339D 简单的线段树操作
- 【HDU1950】Bridging signals (最长上升子序列DP(nlogn))
- AFNetworking 请求工具类