Educational Codeforces Round 97 (Rated for Div. 2) D. Minimal Height Tree (贪心)

题意:有一个从根节点\(BFS\)得来的序列(每次\(bfs\)子节点的时候保证是升序放入队列的),现在让你还原树(没必要和之前相同),问能构造出的最小的树的深度.
题解:不看根节点,我们从第二个位置开始,如果某一段元素升序,那么就让他们变为上一层某个结点的儿子,否则,如果上一层还有另外的父节点的话,就作为它的子儿子,如果没有父亲结点可以连的话,就只能去下一层了,具体实现的话,我们可以统计每层结点的个数,如果下一层出现了降序的情况,就减去之前统计的个数,不断往复即可.
代码:
int t;
int n;
int a[N];
int cnt[N];
int ans; int main() {
//ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
scanf("%d",&t);
while(t--){
ans=1;
scanf("%d",&n);
for(int i=0;i<n;++i) scanf("%d",&a[i]);
int i=1;
cnt[0]=1;
while(i<n){
int now=cnt[ans-1]; //上一层的结点数
cnt[ans]=0;
while(now>0){
now--;
if(i==n) continue;
i++;
cnt[ans]++; //统计当前这一层的结点数
while(i<n && a[i]>a[i-1]){ //升序,全部连给上一层的某个结点
cnt[ans]++; //统计当前这一层的结点数
i++;
}
}
ans++;
}
printf("%d\n",ans-1);
}
return 0;
}
Educational Codeforces Round 97 (Rated for Div. 2) D. Minimal Height Tree (贪心)的更多相关文章
- Educational Codeforces Round 97 (Rated for Div. 2)
补了一场Edu round. A : Marketing Scheme 水题 #include <cstdio> #include <algorithm> typedef lo ...
- Educational Codeforces Round 97 (Rated for Div. 2)【ABCD】
比赛链接:https://codeforces.com/contest/1437 A. Marketing Scheme 题解 令 \(l = \frac{a}{2}\),那么如果 \(r < ...
- Educational Codeforces Round 97 (Rated for Div. 2) E. Make It Increasing(最长非下降子序列)
题目链接:https://codeforces.com/contest/1437/problem/E 题意 给出一个大小为 \(n\) 的数组 \(a\) 和一个下标数组 \(b\),每次操作可以选择 ...
- Educational Codeforces Round 97 (Rated for Div. 2) C. Chef Monocarp (DP)
题意:有\(n\)个菜在烤箱中,每个时刻只能将一个菜从烤箱中拿出来,第\(i\)个时刻拿出来的贡献是\(|i-a[i]|\),你可以在任意时刻把菜拿出来,问将所有菜拿出的最小贡献是多少? 题解: 先对 ...
- Educational Codeforces Round 89 (Rated for Div. 2) C. Palindromic Paths(贪心)
题目链接:https://codeforces.com/contest/1366/problem/C 题意 有一个 $n \times m$ 的 $01$迷宫,要使从 $(1,1)$ 到 $(n,m) ...
- Educational Codeforces Round 89 (Rated for Div. 2) A. Shovels and Swords (贪心)
题意:你有\(a\)个树枝和\(b\)个钻石,\(2\)个树枝和\(1\)个钻石能造一个铁铲,\(1\)个树枝和\(2\)个钻石能造一把剑,问最多能造多少铲子和剑. 题解:如果\(a\le b\),若 ...
- Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship
Problem Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...
- Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)
Problem Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...
- Educational Codeforces Round 43 (Rated for Div. 2)
Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...
随机推荐
- 断言封装整合到requests封装中应用(纠错False,Result循环,tag测试)
检查json_key_value: 检查: requests.py # -*- coding: utf-8 -*-#@File :demo_04.py#@Auth : wwd#@Time : 2020 ...
- Linux 中软链接和硬链接的使用
Linux 链接分两种,一种被称为硬链接(Hard Link),另一种被称为符号链接(Symbolic Link). 硬链接和软链接 硬链接 --- ln 要链接的文件 新硬链接名 软连接 --- l ...
- 【Linux】使用grep快速比较两个文件不同
两个文件的比较,会有同学说使用diff,和vimdiff就可以快速比较,为什么还要使用grep呢? 有些时候,diff和vimdiff的时候环境不符合,这样的情况,就可以使用grep来解决这个问题. ...
- LeetCode965. 单值二叉树
题目 1 class Solution { 2 public: 3 int flag = 0; 4 bool isUnivalTree(TreeNode* root){ 5 isUnivalTree1 ...
- Vue案例之todoLIst实现
使用Vue实现todolist案例,如有不对敬请大佬多多指教 功能: 1.增加功能:在新增版块里面的输入框内输入数据,点击后面的"添加"按钮,将输入的数据添加到列表中,默认是未完成 ...
- Django orm中related_name/related_query_name区别
related_name/related_query_name区别 class Department(models.Model): title = models.CharField(verbose_n ...
- centos系统磁盘扩容
1.查看磁盘空间大小,使用df -h 命令. 2. 增加磁盘空间,例如下图使用VM虚拟机增加的方式.物理机直接安装挂载上去. 3. 使用fdisk /dev/sda, 创建新分区. 4.重启Linux ...
- flume agent的内部原理
flume agent 内部原理 1.Source采集数据,EventBuilder.withBody(body)将数据封装成Event对象,source.getChannelProcessor( ...
- Qedis实现
对比redis的Qedis 实现在github 和 实验楼都有资料
- c++ 三五法则 自己理解
简介 三五法则规定了什么时候需要 1 拷贝构造函数 2 拷贝赋值函数 3 析构函数 1. 需要析构函数的类也需要拷贝构造函数和拷贝赋值函数. 通常,若一个类需要析构函数,则代表其合成的析构函数 ...