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
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,他没有上司,剩下每个人都有一个顶头上司,现在让每个人回答自己有几个上司,问至少有几个人说错了?
  • 思路:因为每个人都有顶头上司,所以上司的个数必然是连续的,也就是说,如果有一个人有3个上司,那么必然有人有2个上司,有人有1个上司。
  • 当然,不是大boss的人不能有0个上司,大boss必然只有0个上司,如果不满足这两点那么ans都要++,并且将前者纳入叫错但还未纠正的人(记为j个)
  • 搜索一遍,记录vis[i]表示有i上司的人有几个。再从i=1开始遍历,sum(人数)初始值为1(1为那个大boss),如果vis[i]>0则往后遍历,记录sum(人数)+=vis[i],如果vis[i]==0,那么说明这个位置应该有人但是现在却没人,所以有人说错了,这个时候从之前的j个人里过来一个作为填补,所以j--,sum++,
  • 一旦sum等于总人数,那么终止遍历,如果j==0之后,如果vis[i]==0 那么就说明后面有人说错了,所以sum++,ans++。
    • 最后ans即为答案

       #include <cstdio>
      #include <ctime>
      #include <cstdlib>
      #include <iostream>
      #include <cstring>
      #include <cmath>
      #include <queue>
      #include <algorithm>
      #define N 200005
      #define inf 1e18+5
      typedef long long ll;
      #define rep(i,n) for(i=1;i<=n;i++)
      using namespace std;
      int i,j,k,m,n,t,cc,ans;
      int a[N],vis[N];
      int s;
      int main()
      {
      while(scanf("%d%d",&n,&s)!=EOF){
      memset(vis,,sizeof(vis));
      ans=;
      j=;
      rep(i,n){
      scanf("%d",&a[i]);
      vis[a[i]]++;
      if(i!=s&&a[i]==){
      ans++;
      j++;
      }
      if(i==s&&a[i]!=){
      vis[a[i]]--;
      ans++;
      a[i]=;
      vis[]++;
      }
      }
      i=;
      int sum=;
      while(sum<n){
      if(vis[i]){
      if(sum+vis[i]<n) sum+=vis[i];
      else break;
      }
      else{
      if(j){
      j--;
      }
      else {
      ans++;
      }
      sum++; }
      i++;
      }
      // if(a[1]==121&&a[2]==158) for(i=100;i<=200;i++) printf("%d ",a[i]); printf("%d\n",ans);
      // printf("%d aaa",a[142]);
      } return ;
      }

Codeforces #380 div2 E(729E) Subordinates的更多相关文章

  1. Codeforces Round #380 (Div. 2)/729E Subordinates 贪心

    There are n workers in a company, each of them has a unique id from 1 to n. Exaclty one of them is a ...

  2. Codeforces #380 div2 D(729D) Sea Battle

    D. Sea Battle time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  3. Codeforces #380 div2 C(729C) Road to Cinema

    C. Road to Cinema time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  4. Codeforces #380 div2 B(729B) Spotlights

    B. Spotlights time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  5. Codeforces #180 div2 C Parity Game

    // Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...

  6. Codeforces #541 (Div2) - E. String Multiplication(动态规划)

    Problem   Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...

  7. Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)

    Problem   Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...

  8. Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)

    Problem   Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...

  9. Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)

    Problem   Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...

随机推荐

  1. 试一下CANVAS

    // 此应用源代码如下: document.getElementById("codetext").innerHTML = document.getElementById(" ...

  2. KeepAlived主备/主主模型高可用Nginx

    部署准备: 两台CentOS 7主机HA1和HA2 CentOS 7 基于rpm包安装Nginx: 由于Base源中没有Nginx,所以要安装EPEL源,命令如下: wget http://dl.fe ...

  3. 第四十章 微服务CICD(2)- jenkins(war版)

    一.下载 官网下载war包,放在tomcat下的webapps下, 第一章 tomcat安装与启动 第二章 部署war包到tomcat jenkins:2.19.1版本. 二.修改编码为utf-8 在 ...

  4. java.lang.NoClassDefFoundError: org/apache/juli/logging/LogFactory的解决(碰到问题,转载答案)

    自己前一段时间出现了这个问题,通过在网上搜索,大概知道了原因,整理下一,以供大家参考. 将项目部署好后,启动tomcat后报错,java.lang.NoClassDefFoundError: org/ ...

  5. mysql中left join ,right join 以及inner join 比较

    下面是例子分析表A记录如下: aID        aNum 1           a20050111 2           a20050112 3           a20050113 4   ...

  6. Linux进程管理、任务管理

    查看进程 Linux中的进程可以使用ps.pstree命令查看. 一般使用 ps aux (注意,没有短划线-:虽然加上不影响执行,只是会提示),还可以使用 ps -le,都是查看所有进程,区别在于显 ...

  7. SQL server清空数据库日志脚本

    /*设置为简单模式*/ USE [master] } SET RECOVERY SIMPLE WITH NO_WAIT } SET RECOVERY SIMPLE /*获取日志文件名称*/ } ) / ...

  8. angularjs 作用域

    1.指令属性取值:通过attr.someAttribute属性名字获取 以下,通过$eval(attr.data)获取value <div ng-controller="personC ...

  9. hibernate(1)

    1. 环境配置 1.1 hiberante环境配置 hibernate可实现面向对象的数据存储.hibernate的官网:http://hibernate.org/ 官网上选择hibernate OR ...

  10. 关于Linux 下 Mysql 远程访问时出现的Access denied for user '用户名'@'IP地址' (using password:NO)

    大概是因为MySQL不允许远程访问时候不带密码吧,所以还是设定一个密码 如下这样做: 打开终端 ,即terminal的那个(RedHat5.x为例 在左上角(可能会移位)那个带着红帽的家伙点击,--- ...