博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[CF729E] Subordinates(贪心)
阅读量:6689 次
发布时间:2019-06-25

本文共 2532 字,大约阅读时间需要 8 分钟。

E. Subordinates

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·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.

 

目标序列是有一个零,后面是连续的。然后贪心地把后面的数调到前面就好了。

#include 
using namespace std;int n, s, ans;int a[200020];multiset
ss;int main() { #ifdef ULTMASTER freopen("a.in","r",stdin); #endif scanf("%d %d", &n, &s); for (int i = 0; i < n; ++i) scanf("%d", &a[i]); if (a[s - 1] != 0) { a[s - 1] = 0; ans++; } { int zero = 0; for (int i = 0; i < n; ++i) if (a[i] == 0) { if (zero == 0) zero = 1; else a[i] = 1e8; } ss.insert(a, a + n); } int prev = 0; while (!ss.empty()) { auto it = ss.begin(); if ((*it) - prev > 1) { auto itend = (--ss.end()); prev++; ans++; ss.erase(itend); } else { prev = (*it); ss.erase(it); } } printf("%d\n", ans); return 0;}

  

 

转载于:https://www.cnblogs.com/ultmaster/p/6087156.html

你可能感兴趣的文章
linux中awk学习小结
查看>>
MySQL主从的一致性校验及修复
查看>>
Skype For Business 2015实战系列5:安装后端数据库
查看>>
Microsoft Security Essentials 中文版正式发布
查看>>
JSF Note
查看>>
自定义支持多行显示的RadioGroup
查看>>
tty终端截屏软件FbGrab安装和使用
查看>>
Linux内核网络参数
查看>>
初尝Mcafee之终结篇:管理架构概述【09】
查看>>
WCF分布式开发常见错误(23):the fact that the server certificate isn't configured with HTTP.SYS...
查看>>
第一个Indigo Service
查看>>
《Pro ASP.NET MVC 3 Framework》学习笔记之三十二 【无入侵的Ajax】
查看>>
监听启动报TNS-12537、TNS-12560错误
查看>>
安腾还是Power7——Unix服务器你该如何选?
查看>>
Nginx实用指南V1 (连载之六:cacti监控)
查看>>
HP P2000 RAID-5两块盘离线的数据恢复报告
查看>>
XXX管理平台系统——项目教训
查看>>
会写代码的项目经理
查看>>
通过Lua解释器来扩展丰富nginx功能,实现复杂业务的处理
查看>>
禁用WPF窗体的最大化按钮
查看>>