Codeforce round# 380 E(贪心,构造)
来源:互联网 发布:python分析web日志 编辑:程序博客网 时间:2024/05/21 09:34
题目大意
题目链接
E. Subordinates
给你一棵有根树,让树上的节点挨着报告自己的祖先个数,问至少多少个节点说错了
分析
先排序,
贪心,想想,如果树的长度越长那么出现错误的节点就会越少,所以如果有一个错误的节点就让他增加当前扫描到的树的长度,并且如果有节点会发生错误,显然让最长的节点出现错误比较优(有点绕~)具体说来就是,每次扫描的时候如果当前节点发生错误就让最后面的节点来补这个位置。
AC代码
#include <cstdio>#include <cstring>#include <queue>#include<algorithm>#include<iostream>#include<cmath>#include<map>#include<string>#include<vector>#define INF 0x3f3f3f3f#define maxn 2e5+10#define fi first#define se secondusing namespace std;typedef long long LL;typedef unsigned long long uLL;typedef pair<int,int> PI;typedef map<int,int> Mii;typedef map<int,int>::iterator MI;int a[(int)maxn];int main(){ //freopen("H:\\c++\\file\\stdin.txt","r",stdin); int n,s; cin>>n>>s; for(int i=0 ; i<n ; ++i) { scanf("%d",&a[i]); } int ans =0; if(a[s-1]!=0){a[s-1] = 0;ans =1;} for(int i = 0; i<n ; ++i) if(i !=s-1 && a[i]==0)a[i] = n+1; sort(a,a+n); int L =1; for(int i = 1; i<n ; ++i) { if(a[i]<=L){L = max(a[i]+1,L) ; continue;} while(a[i]>L &&i<n) { L++;n--;ans++; } L++; } cout<<ans<<endl; return 0;}
1 0
- Codeforce round# 380 E(贪心,构造)
- codeforce round 399# E
- CodeForce 626E Simple Skewness (贪心+三分)
- codeforce 732e Sockets (贪心)
- Codeforce Round #424E Colored Jenga
- codeforce Round #379 (Div. 2)C(贪心)
- Codeforce Round #438 C.Bus(贪心)
- CROC 2016 - Elimination Round (Rated Unofficial Edition) E. Intellectual Inquiry 贪心 构造 dp
- Codeforces Round #394 (Div. 2) E. Dasha and Puzzle —— 构造 + 贪心
- codeforce 825E. Minimal Labels 拓扑排序 贪心思想
- Codeforce Round #437 div2 A B C E 题解
- codeforce 509C (很有趣的贪心模拟构造)
- 【Codeforces Round 324 (Div 2)E】【贪心 构造】Anton and Ira 全排列交换 最小距离成本
- Codeforces 432E Square Tiling(构造+贪心)
- codeforce 777e Hanoi Factory 叠塔(贪心,从后往前推)
- E. Cubes codeforce 180/E
- COdeforce 14E
- codeforce 18E
- writeup-flag
- JDK8 API——java.io.serializable
- 第十二周实践题目之爬楼梯
- [unreal4入门系列之三] 初探UE4安装文件目录结构
- spring下应用@Resource, @Autowired 和 @Inject注解进行依赖注入的差异
- Codeforce round# 380 E(贪心,构造)
- java和软件开发
- Android 一键加速
- 第一个java程序HelloWorld
- 搭建java开发环境
- Dos命令行常用命令
- 报错,但不影响运行ERROR: JDWP Unable to get JNI 1.2 environment, jvm->GetEnv() return code = -2
- java标示符和关键字
- java基本数据类型