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个点,每个点只有一个直接的父亲节点,然后给出每个点的上级数量(包括父亲的父亲),且规定第s个点是根节点,问其中最少改几个就能构造出合法树。
**思路:**开始想使用数据结构,然后判断深度,不对就改,但是这样贪心是不对的。我们可以想到如果符合要求,那么排好序后的上级数量,两两之间一定是相同的或者是连续的。而当某个点的值不连续了,那么说明其后的所有点都不对了,合理的贪心是该把排最后的数变成缺少的数字,然后继续判断。注意如果有多个0,如果它不是根,那么说明这个数一定是需要变换的。另外注意判断第s个点不为0的情况。

/** @Date    : 2016-11-20-18.15

* @Author : Lweleth (SoungEarlf@gmail.com)

* @Link : https://github.com/

* @Version :

*/

#include <stdio.h>

#include <iostream>

#include <string.h>

#include <algorithm>

#include <utility>

#include <vector>

#include <map>

#include <set>

#include <string>

#include <stack>

#include <queue>

//#include<bits/stdc++.h>

#define LL long long

#define MMF(x) memset((x),0,sizeof(x))

#define MMI(x) memset((x), INF, sizeof(x))

using namespace std;



const int INF = 0x3f3f3f3f;

const int N = 1e5+20;





int a[N*2];


int main()

{

int n, s, cnt;

cin >> n >> s;

for(int i = 0; i < n; i++)

{

scanf("%d", a + i);

if(i != s-1 && a[i] == 0)

a[i] = INF;

}

cnt = 0;

if(a[s-1] != 0)

cnt++;

a[s-1] = 0;

int nw = 1;

sort(a, a + n);

for(int i = 1; i < n; nw++)

{



if(nw != a[i])

{

cnt++;

n--;

}

else

{

while(a[i] == nw)

i++;

}

}

cout << cnt << endl;

return 0;

}


Codeforces Round #380 (Div. 2)/729E Subordinates 贪心的更多相关文章

  1. Codeforces Round #380 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 2) E. Subordinates 贪心

    E. Subordinates time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  2. Codeforces Round #202 (Div. 1) A. Mafia 贪心

    A. Mafia Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/348/problem/A D ...

  3. Codeforces Round #382 (Div. 2)B. Urbanization 贪心

    B. Urbanization 题目链接 http://codeforces.com/contest/735/problem/B 题面 Local authorities have heard a l ...

  4. Codeforces Round #164 (Div. 2) E. Playlist 贪心+概率dp

    题目链接: http://codeforces.com/problemset/problem/268/E E. Playlist time limit per test 1 secondmemory ...

  5. Codeforces Round #180 (Div. 2) B. Sail 贪心

    B. Sail 题目连接: http://www.codeforces.com/contest/298/problem/B Description The polar bears are going ...

  6. Codeforces Round #192 (Div. 1) A. Purification 贪心

    A. Purification Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/329/probl ...

  7. Codeforces Round #274 (Div. 1) A. Exams 贪心

    A. Exams Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/480/problem/A Des ...

  8. Codeforces Round #374 (Div. 2) B. Passwords 贪心

    B. Passwords 题目连接: http://codeforces.com/contest/721/problem/B Description Vanya is managed to enter ...

  9. Codeforces Round #303 (Div. 2) C. Woodcutters 贪心

    C. Woodcutters Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/545/probl ...

随机推荐

  1. POJ 3084 Panic Room(最大流最小割)

    Description You are the lead programmer for the Securitron 9042, the latest and greatest in home sec ...

  2. dataTables工作总结

    近期在工作中用到了dataTables,现在总结一下在工作中遇到的问题以及解决方法,如有不妥之处希望多多指教,定会改进. 首先这里用的是coloradmin框架,在vs环境下开发. 这里写一个容器用于 ...

  3. java连接数据库的两种方法总结

    方法一:使用jdbc-odbc桥连接sql server,作为中间媒介连接数据库 1.配置数据源:打开控制面版->管理工具->数据源(ODBC)->选用户DSN,按下添加按钮-> ...

  4. win7主题/默认账户图片路径

    账户图片位置 C:\ProgramData\Microsoft\User Account Pictures 主题位置 C:\Windows\Resources

  5. jquery计算器(改良版)

    代码: <!Doctype html> <html> <meta charset="UTF-8"> <title>计算器</t ...

  6. timer实现

    实现一个 timer 前段时间写过一篇 blog 谈到 用 timer 驱动游戏 的一个想法.当 timer 被大量使用之后,似乎自己实现一个 timer 比用系统提供的要放心一些.最近在重构以前的代 ...

  7. [剑指Offer] 65.矩阵中的路径

    题目描述 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径.路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子.如果一条路径经过了矩阵中 ...

  8. [OS] 进程互斥

    对互斥的正确软件实现算法(面包店算法)是非常耗时的,现代的计算机系统都会提供简单的硬件指令,使用这些指令能够有效地解决临界区问题. 硬件提供一个TestAndSet指令,来实现原子指令的功能: boo ...

  9. SQL Server的全局变量

    SQL Service中的全部变量不需要用户参与定义,在任何程序均可随时调用.并且全部变量是以@@开头 全局变量名称 描述 @@CONNECTIONS 返回 SQL Server 自上次启动以来尝试的 ...

  10. 【EF】EF实现大批量数据库插入操作

    通过参考http://www.itnose.net/news/171/6306259里面介绍的方法,我们可以在程序的解决方案的引用中选择NuGet管理包中添加  该扩展方法提供BulkInsert() ...