【codeforces 797D】Broken BST
【题目链接】:http://codeforces.com/contest/797/problem/D
【题意】
给你一个二叉树;
然后问你,对于二叉树中每个节点的权值;
如果尝试用BST的方法去找;
能不能找到这样一个权值的节点;
(只要找到这个权值的节点就好,没有说特定找哪一个)
【题解】
对于二叉树中的每一个节点,我们都能确定;
可以到达这个节点的权值的范围;
对于节点不在这个权值范围内的点;
答案递增;
但有可能会有
3
2 -1 -1
1 1 3
2 -1 -1
这样的数据
这里的答案应该是0而不是1;
因为2是能够通过bst的方法找到的;
虽然找左儿子的时候返回的是右儿子的;但是找到了!
所以最后如果有某些权值在某一些地方找不到,但是在另外一些地方找到了则也是可以的.
【Number Of WA】
2
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1e5+100;
const int INF = 1e9+1000;
int n,a[N],l[N],r[N],bo[N],root,ans;
int siz[N];
map <int,int> dic1,dic2;
void dfs2(int x,int fa,int mi,int ma)
{
if (mi<=a[x]&&a[x]<=ma)
{
dic1[a[x]] = 1;
}
else
{
ans++;
dic2[a[x]]++;
}
if (l[x]!=-1) dfs2(l[x],x,mi,min(ma,a[x]));
if (r[x]!=-1) dfs2(r[x],x,max(mi,a[x]),ma);
}
int main()
{
//freopen("F:\\rush.txt","r",stdin);
ios::sync_with_stdio(false),cin.tie(0);
cin >> n;
rep1(i,1,n)
{
cin >> a[i] >> l[i] >> r[i];
if (l[i]!=-1) bo[l[i]] = true;
if (r[i]!=-1) bo[r[i]] = true;
}
rep1(i,1,n)
if (!bo[i])
root = i;
dfs2(root,0,-INF,INF);
rep1(i,1,n)
if (dic1[a[i]] && dic2[a[i]]>0)
{
dic1[a[i]]=0;
ans-=dic2[a[i]];
}
cout << ans << endl;
return 0;
}
【codeforces 797D】Broken BST的更多相关文章
- 【26.34%】【codeforces 722A】Broken Clock
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- Codeforces 797 D. Broken BST
D. Broken BST http://codeforces.com/problemset/problem/797/D time limit per test 1 second memory lim ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
随机推荐
- CentOS 安装 MRTG 软件完成后的 403 Forbidden(转载)
用 yum 安装 MRTG 並设定好之后也把 apache 的 httpd.conf 加上 mrtg 的目录,但 http://server/mrtg 卻一直出現 403 Forbidden.在 ht ...
- DCloud-MUI:代码块
ylbtech-DCloud-MUI:代码块 1.返回顶部 1. 怎么用? html 此底色代表最小触发字符 此底色代表非必要完整触发字符 *需HBuilder7.1+,或者下载m ...
- PCB 周期日历
在PCB行业一直以来没找到适合我们这行业的日历,主要存在2个差异导致. 1.周期差异:由于PCB 周期计算的复杂性,市面上无法找到符合PCB行业计算周期方式 (另一遍博文中有写周期计算逻辑) http ...
- rancher导入k8s集群后添加监控无数据
1.日志报错 rancher导入k8s集群后添加监控无数据,rancher日志报错: k8s.io/kube-state-metrics/pkg/collectors/builder.go:: Fai ...
- go并发编程 WaitGroup, Mutex
1.背景 记录一下,方便后续写代码直接使用. 需要注意几点: chan 默认支持多协程工作,不需要加锁. 其他变量操作需要使用锁保护(map多协程并发写会panic, 并且无法捕获). 启动gorou ...
- 工具分享2:Python 3.6.4、文本编辑器EditPlus、文本编辑器Geany
工具官网下载地址: https://www.python.org/downloads/ python 3.6.0下载链接: 链接:https://pan.baidu.com/s/1snuSxsx 密码 ...
- Blender插件编写指南
前言 Blender插件是Blender的利器, 用户可以使用各种插件扩充Blender的功能. Blender Python插件以bpy.props, bpy.types.Operator, bpy ...
- B - Substrings Sort
Problem description You are given nn strings. Each string consists of lowercase English letters. Rea ...
- P2241 统计方形(数据加强版)
题目背景 1997年普及组第一题 题目描述 有一个n*m方格的棋盘,求其方格包含多少正方形.长方形 输入输出格式 输入格式: n,m因为原来数据太弱,现规定m小于等于5000,n小于等于5000(原来 ...
- Java中last_insert_id的使用
last_insert_id的作用是:在当前表中的主键是自增时,插入一条新记录的同时使用last_insert_id可以获取当前的新记录的主键id. 下面是一个例子: import java.sql. ...