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

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 nExaclty 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·1051 ≤ 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
题意:一个公司内有N名员工,其中有一位主任,剩下N-1名员工都有且仅有一位直属上司,如果A是B的上司,B是C的上司,那么A也是C的上司(并不是直属的),而主任不是任何人的上司。给出N名员工的上司个数与及主任的员工号码,哪种情况下员工的上司数目是错误的个数是最少的,输出此时的员工错误个数。
因为如果某个员工的上司数为p那么剩下的员工中一定要有上司数为p-1的,所以我们可以用贪婪算法从0到n-1去计算,当到k(1<=k<n)时,如果为0,那么在减去一个上司数最大的或是减去多余的上司数为0的员工(因为这N个员工中只有主任的上司数为0 ,在k这补上一个上司数为k的员工;除此之外要特判一下上司数为0与及主任的上司数不为0的错误情况。具体情况看代码!

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn = 2e5 + 7;
int a[maxn];
int main()
{
    int n, m, x, flag = 0, record = 0;
    cin >> n >> m;
    for(int i = 1; i <= n; i ++){
        cin >> x;
        if(i == m && x != 0) flag = 1; /// 判断主任的上司是否为0,如果不为0那么这种情况肯定错误的,不用把其记录到a[x]中
        else a[x] ++;
    }
    int ans = 0, sum = 0, num = n; /// ans 是错误人员数,sum已经访问过的人员的个数
    for(int i = 0; i < n; i ++){
        if(i == 0){ /// 特判上司数为0的情况
            sum ++;
            if(flag){ /// 此处是主任的上司不为为0
                ans += (a[i] + 1);
                record += a[i];
            }
            else{
                ans += (a[i] - 1);
                record += (a[i] - 1);
            } /// record是记录的多余的上司数为0的员工的个数
        }
        else{
            if(a[i] == 0){ /// 当上司数为i的员工不存在时
                sum ++;
                if(record > 0) record --; ///  此处是用上司数为0的多余的员工来补足上司数为i的员工
                else{ ///  此处是用上司数最大的员工来补足上司数为i的员工
                    ans ++;
                    while(a[num] == 0) num --; 
                    a[num] --;
                }
            }
            else sum += a[i];
        }
        if(sum == n) break;
    }
    cout << ans << endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值