Codeforces Round #380 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 2) E. Subordinates 贪心
2016-11-23 15:08
537 查看
E. Subordinates
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
There are n workers in a company, each of them has a unique id from 1 to n. Exaclty one of them is a chief, his id is s. Each worker except the chief has exactly one immediate superior.
There was a request to each of the workers to tell how how many superiors (not only immediate). Worker's superiors are his immediate superior, the immediate superior of the his immediate superior, and so on. For example, if there are three workers in the company, from which the first is the chief, the second worker's immediate superior is the first, the third worker's immediate superior is the second, then the third worker has two superiors, one of them is immediate and one not immediate. The chief is a superior to all the workers except himself.
Some of the workers were in a hurry and made a mistake. You are to find the minimum number of workers that could make a mistake.
Input
The first line contains two positive integers n and s (1 ≤ n ≤ 2·105, 1 ≤ s ≤ n) — the number of workers and the id of the chief.
The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ n - 1), where ai is the number of superiors (not only immediate) the worker with id i reported about.
Output
Print the minimum number of workers that could make a mistake.
Examples
Input
Output
Input
Output
Note
In the first example it is possible that only the first worker made a mistake. Then:
the immediate superior of the first worker is the second worker,
the immediate superior of the third worker is the first worker,
the second worker is the chief.
题意:给你n个人,每个人只有一个大佬,大佬可能有大大佬,但是只有一个boss,boss没有大佬;
告诉你哪个是boss,和每个人大佬,大大佬,大大大佬。。。的个数,问最小几个人是错误的;
思路:如果当前深度为空,用最深的那个堵上,答案加一;
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
There are n workers in a company, each of them has a unique id from 1 to n. Exaclty one of them is a chief, his id is s. Each worker except the chief has exactly one immediate superior.
There was a request to each of the workers to tell how how many superiors (not only immediate). Worker's superiors are his immediate superior, the immediate superior of the his immediate superior, and so on. For example, if there are three workers in the company, from which the first is the chief, the second worker's immediate superior is the first, the third worker's immediate superior is the second, then the third worker has two superiors, one of them is immediate and one not immediate. The chief is a superior to all the workers except himself.
Some of the workers were in a hurry and made a mistake. You are to find the minimum number of workers that could make a mistake.
Input
The first line contains two positive integers n and s (1 ≤ n ≤ 2·105, 1 ≤ s ≤ n) — the number of workers and the id of the chief.
The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ n - 1), where ai is the number of superiors (not only immediate) the worker with id i reported about.
Output
Print the minimum number of workers that could make a mistake.
Examples
Input
3 2 2 0 2
Output
1
Input
5 3 1 0 0 4 1
Output
2
Note
In the first example it is possible that only the first worker made a mistake. Then:
the immediate superior of the first worker is the second worker,
the immediate superior of the third worker is the first worker,
the second worker is the chief.
题意:给你n个人,每个人只有一个大佬,大佬可能有大大佬,但是只有一个boss,boss没有大佬;
告诉你哪个是boss,和每个人大佬,大大佬,大大大佬。。。的个数,问最小几个人是错误的;
思路:如果当前深度为空,用最深的那个堵上,答案加一;
#include<bits/stdc++.h> using namespace std; const int N=200010,mod=1e9+7,MOD=7; int a ,flag ; int main() { int n,s; scanf("%d%d",&n,&s); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); flag[a[i]]++; } int now=0,ans=0,z=n; if(a[s]!=0) ans+=flag[0]+1,now=flag[0],z-=flag[0]+1,flag[a[s]]--; else ans+=flag[0]-1,now=flag[0]-1,z-=flag[0]; for(int i=1;i<n;i++) { if(z<=0)break; if(!flag[i]) { ans++; if(now) { ans--; now--; } else { z--; } } else { z-=flag[i]; } } printf("%d\n",ans); return 0; }
相关文章推荐
- Codeforces Round #380 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 2)
- Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3) C
- codeforces Technocup 2017 - Elimination Round 2/Codeforces Round #380 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 2) 题解
- Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3) E
- Codeforces Round #434 (Div. 2, based on Technocup 2018 Elimination Round 1)
- Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3) 圣诞之夜!
- Codeforces Round #380 (Div. 1, Rated, Based on Technocup 2017 - Elimination Round 2)
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) A. Andryusha and Socks
- Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2)A. Vicious Keyboard
- Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3) B
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 831D) - 贪心 - 二分答案 - 动态规划
- Codeforces Round #380 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 2) 题解
- Codeforces Round #380 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 2)
- Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3) E. Santa Claus and Tangerines
- Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3)
- Codeforces Round #440 (Div. 2, based on Technocup 2018 Elimination Round 2)
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) C Andryusha and Colored Balloons
- Codeforces Round #440 (Div. 2, based on Technocup 2018 Elimination) A. Search for Pretty Integers
- Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2) C Voltage Keepsake
- Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3) D