Codeforces Round #380 (Div. 2) E. Subordinates

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

There are n workers in a company, each of them has a unique id from1 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 integersa1, a2, ..., an (0 ≤ ai ≤ n - 1), where ai is the number of superiors (not only immediate) the worker with idi 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 he chief.


  • 思路: 除了标明位置为 s 的chief , 每一个员工都有上司,则编号为0的 普通员工一定是错的,置为INF;将员工编号从小到大排序,从小到大模拟判断,若员工序列存在断层,则用最后一个员工来填补。

  • #include <queue>
    #include <functional>
    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    #include <algorithm>
    #include <stack>
    #include <vector>
    #include <set>
    #include <map>
    #include <string>
    #include <cmath>
    #include <cstdlib>
    #include <ctime>
    #include <assert.h>
    #define REP(i,k,n) for(int i=k;i<n;i++)
    #define REPP(i,k,n) for(int i=k;i<=n;i++)
    #define scan(d) scanf("%d",&d)
    #define scann(n,m) scanf("%d%d",&n,&m)
    #define mst(a,k)  memset(a,k,sizeof(a));
    #define LL long long
    #define eps 1e-8
    #define INF 0x3f3f3f3f
    #define mod 1000000007
    #define N 200005
    #define M 15
    #define fre  freopen("input.txt","r",stdin)
    using namespace std;
    int num[N];
    
    int main()
    {
      int n,s;
      scann(n,s);
      REPP(i,1,n) {
         scan(num[i]);
         if(i == s) continue;        // chief  跳过
         if(!num[i]) num[i] = INF;   //员工编号为0,错误,置为INF
      } 
      int ans = 0;
      int l = 0;                     //编号
      if(num[s]) ans++,num[s]=0;     //cheif 编号不为0,错误,置为0
      sort(num+1,num+n+1);       
      int cnt = 0;                   //记录 将靠后的员工填补序列断层的数目
      REPP(i,1,n){
        if(i+cnt>n) break;          
        if(num[i] <= l) continue;
        if(num[i] == l+1){            //刚好接上
          l++;
          continue;
        }                
          l++;                     
          i--;
          cnt++;
          ans++;
      }
      cout<<ans; 
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值