排序,构造。

相当于告诉我们一棵树$n$个节点,每个节点在哪一层,至少需要移动多少个节点,才能让这些节点变成一棵树。

按照层次排个序移动一下就可以了,优先选择那些不是$s$但是层次是$0$的节点,如果没有,那么再选择层次最高的。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<ctime>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0);
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
char c = getchar();
x = ;
while(!isdigit(c)) c = getchar();
while(isdigit(c))
{
x = x * + c - '';
c = getchar();
}
} struct X
{
int id,c;
}t[];
int n,s,ans; bool cmp(X a, X b)
{
return a.c<b.c;
} int flag[]; queue<int>Q; int main()
{
cin>>n>>s;
for(int i=;i<=n;i++)
{
t[i].id=i;
cin>>t[i].c;
if(t[i].c==&&i!=s)
{
flag[i]=;
ans++;
Q.push(i);
}
} if(t[s].c!=) ans++,t[s].c=; sort(t+,t++n,cmp); int wei=n; for(int i=;i<=n;i++)
{
if(flag[t[i].id]==) continue;
if(t[i].c==t[i-].c) continue;
if(t[i].c==t[i-].c+) continue; int need=t[i].c-t[i-].c-;
int f=; while()
{
f++;
if(!Q.empty()) Q.pop();
else
{
if(wei<i) break;
flag[t[wei].id]=;
wei--; ans++;
if(wei<i) break;
}
if(f==need) break;
}
if(wei<i) break;
} cout<<ans<<endl; return ;
}

CodeForces 738E Subordinates的更多相关文章

  1. 【codeforces 738E】Subordinates

    [题目链接]:http://codeforces.com/problemset/problem/738/E [题意] 给你一个类似树形的关系; 然后告诉你某个人头顶上有多少个上司numi; 只有fat ...

  2. Codeforces 729E Subordinates

    题目链接:http://codeforces.com/problemset/problem/729/E 既然每一个人都有一个顶头上司,考虑一个问题: 如果这些人中具有上司数目最多的人有$x$个上司,那 ...

  3. Codeforces #380 Subordinates(贪心 构造)

    从前往后扫,找到一出现次数为0的数,从后面找一个出现不为0的数转化而来.设置两指针l, r来处理. #include<cstdio> #include<iostream> #i ...

  4. Codeforces #380 div2 E(729E) Subordinates

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

  5. Codeforces Round #380 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 2) E. Subordinates 贪心

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

  6. Codeforces Technocup 2017 - Elimination Round 2 E Subordinates(贪心)

    题目链接 http://codeforces.com/contest/729/problem/E 题意:给你n个人,主管id为s,然后给你n个id,每个id上对应一个数字表示比这个人大的有几个. 最后 ...

  7. Subordinates CodeForces - 737C (树,构造)

    大意: 求构造一棵树, 每个节点回答它的祖先个数, 求最少打错次数. 挺简单的一个构造, 祖先个数等价于节点深度, 所以只需要确定一个最大深度然后贪心即可. 需要特判一下根的深度, 再特判一下只有一个 ...

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

  9. Codeforces Beta Round #6 (Div. 2 Only) B. President's Office 水题

    B. President's Office 题目连接: http://codeforces.com/contest/6/problem/B Description President of Berla ...

随机推荐

  1. 注意for循环中变量的作用域

    for e in collections: pass 在for 循环里, 最后一个对象e一直存在在上下文中.就是在循环外面,接下来对e的引用仍然有效. 这里有个问题容易被忽略,如果在循环之前已经有一个 ...

  2. 【BZOJ4540】【HNOI2016】序列 [莫队][RMQ]

    序列 Time Limit: 20 Sec  Memory Limit: 512 MB[Submit][Status][Discuss] Description 给定长度为n的序列:a1,a2,…,a ...

  3. 20155335《java程序设计》第一周学习总结

    18个章节的问题 (1)为什么需要JVM让java跨平台? (2)JVM与JDK,与JRE的关系? (3)为什么 -0/3 结果是 0,而 -0.0/3.0 结果是 -0.0?(注意后边的结果0带负号 ...

  4. JS之document对象(找元素、操作内容、操作属性、操作样式及4道例题)

    document对象 一.找元素 1.根据id找 示例: <input id = "a" type="button" value="找元素&qu ...

  5. Java 扑克牌发牌

    今天看到这个算法题,http://www.cnblogs.com/xishuai/p/3392981.html ,忍不住自己用Java做了一个. 初始化很重要,所有的52张牌按顺序放入到容器里边,标志 ...

  6. C# 文件操作常用方法总结

    需引用 System.IO Path为绝对路径 检测指定目录是否存在 Directory.Exists(Path) 创建目录 Directory.CreateDirectory(Path) 删除目录 ...

  7. Less & Sass

    CSS不是一种编程语言.它开发网页样式,但是没法用它编程.也就是说,CSS基本上是设计师的工具,它没有变量,也没有条件语句,只是一行行单纯的描述.有人就开始为CSS加入编程元素,这被叫做"C ...

  8. linux下暴力破解工具hydra【转】

    一.简介 Number one of the biggest security holes are passwords, as every password security study shows. ...

  9. perl输出重定向

    use utf8; open A, ">&STDOUT"; open STDOUT, ">AA.txt"; print STDOUT 'AB ...

  10. gradle问题总结与理解(一篇文章带你理解android studio 与gradle 的关系)

    前言:近日在网上找了个很不错的安卓二维码美化,由于下载的项目经常出问题,且不方便依赖使用,因此我想把它写个demo,并把源码发布到jcenter中,修改还是很顺利的,运行项目到手机也没问题,发布遇到了 ...