Codeforces #380 div2 E(729E) Subordinates
1 second
256 megabytes
standard input
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.
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.
Print the minimum number of workers that could make a mistake.
3 2
2 0 2
1
5 3
1 0 0 4 1
2
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 ;
}
- 最后ans即为答案
Codeforces #380 div2 E(729E) Subordinates的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- Codeforces #380 div2 B(729B) Spotlights
B. Spotlights time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Codeforces #180 div2 C Parity Game
// Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...
- Codeforces #541 (Div2) - E. String Multiplication(动态规划)
Problem Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...
- Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)
Problem Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...
- Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)
Problem Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...
- Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)
Problem Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...
随机推荐
- jdk环境变量的配置并检测是否配置成功
JDK环境变量配置进行java开发,首先安装JDK,安装后进行环境变量配置1,下载JDK(http://java.sun.com/javase/downloads/index.jsp)2.安装jdk- ...
- JSP(include指令与<jsp:include>动作的区别)
<%@ page language= "java" contentType="text/html;charset=UTF-8" %><html ...
- 反射——反射API,使用反射创建数组
反射API Java.lang.Reflect库 ① Class类与Java.lang.Reflect类库一起对反射的概念进行支持. ② java.lang包下: a) Cla ...
- 事务BEGIN TRANSACTION
事务(Transaction)是并发控制的基本单位.所谓的事务,它是一个操作序列,这些操作要么都执行,要么都不执行,它是一个不可分割的工作单位.例如,银行转账工作:从一个账号扣款并使另一个账号增款,这 ...
- history对象的一些知识点
history对象可以保存用户的上网的历史记录,即从窗口被打开的那一刻算起.这里有个比较纠结的问题,出于安全因素的考虑,开发人员无法得知用户浏览过的URL, 只能通过用户访问过的页面列表,实现后退和前 ...
- 一次疏忽导致的bug
NBB_PUT_SHORT 这个宏是按char* 类型算指针的实际工作中,没有注意这一点,输入指针类型 强转为了 SHORT* 导致 填充的内容错误 这是第一次在工作中遇到 指针类型相关的问题值得记录 ...
- C# 将文件嵌入DLL 。Log4net 配置
最近在弄使用Log4net记录日志. 将配置文件封装到的DLL中. 封装步骤: 1.将配置文件添加到类库中. 2.在配置文件上右键,选择属性. 3. 此时生成类库.DLL中就存在该配置文件啦.如图: ...
- vs2010 A selected drive is no longer valid
visual studio 2010重新安装添加组件,报A selected drive is no longer valid错误. 这个是由于已经安装了sp1,此时需要将sp1卸载掉,然后就可以安装 ...
- agsXMPP参考代码
agsXMPP 1.删除好友 XmppCon.RosterManager.RemoveRosterItem(node.RosterItem.Jid); 2.注销用户 void userConn_OnL ...
- JavaScript基础知识整理(1)
粗略理解,努力入门中 1.在html中引入外部脚本: <script src="filename.js"></script> 2.注释: 多于一行的长注 ...