Gym101158G Placing Medals on a Binary Tree(二进制模拟)

Gym101158G-Placing Medals on a Binary Tree

题意

一颗完全二叉树,给出n个点,xi的值表示深度为xi的点。问能否在当前状态下使得从根节点到该点的路径中不会遇到其他点。

思路

其实本题的意思就是 1/2,1/4,1/8等等等等,看剩下的点能不能减。然而由于k值并不小,所以long double也无法处理。其实完全二叉树给了启发,也就是如果当前深度的点占据了,相当于之后的子树都不能进行选择了。所以运算方式上类似二进制。相当于利用map对二进制进行模拟。

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
int T;

map<int,int>mp;
int n;

void init(){
    mp.clear();
}

int main(){

    scanf("%d",&n);
    int fa = 0,flag = 0,mmax = 0;
    init();
    for(int i=0;i<n;i++){
        int x;
        scanf("%d",&x);
        if(flag==1) printf("No\n");
        else if(x>fa){
            printf("Yes\n");
            mp[x]++;
            int cur=x;
            while(mp[cur]>1&&cur>fa){
                mp[cur]=0;
                cur--;
                mp[cur]++;
            }
            while(mp[fa+1]>=1)  fa++;

        }else if(x==fa){
            int f = 0;
            for(int i=x;i>=0;i--){
                if(mp[i]<2) f = 1;
            }
            if(mmax>fa&&f)  printf("No\n");
            else{
                printf("Yes\n");
                flag=1;
            }
        }else   printf("No\n");
        mmax = max(mmax,x);
    }

    return 0;
}

原文地址:https://www.cnblogs.com/caomingpei/p/9637589.html

时间: 2024-10-18 04:23:54

Gym101158G Placing Medals on a Binary Tree(二进制模拟)的相关文章

Maximum Depth of Binary Tree

这道题为简单题 题目: Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 思路: 我是用递归做的,当然也可以用深搜和广搜,递归的话就是比较左右子树的深度然后返回 代码: 1 # Definition for a binary tre

226反转二叉树 Invert Binary Tree

Invert a binary tree. 4 / 2 7 / \ / 1 3 6 9 to 4 / 7 2 / \ / 9 6 3 1 Trivia:This problem was inspired by this original tweet by Max Howell: Google: 90% of our engineers use the software you wrote (Homebrew), but you can't invert a binary tree on a wh

[leetcode] 104. Maximum Depth of Binary Tree

Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 递归遍历 左子树 和 右子树 一刷: public int maxDepth(TreeNode root) { if(root == null){ return 0; } int

LeetCode 145 Binary Tree Postorder Traversal(二叉树的后续遍历)+(二叉树、迭代)

翻译 给定一个二叉树,返回其后续遍历的节点的值. 例如: 给定二叉树为 {1, #, 2, 3} 1 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你可以用迭代来完成它吗? 原文 Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recur

LeetCode Binary Tree Inorder Traversal

LeetCode解题之Binary Tree Inorder Traversal 原题 不用递归来实现树的中序遍历. 注意点: 无 例子: 输入: {1,#,2,3} 1 2 / 3 输出: [1,3,2] 解题思路 通过栈来实现,从根节点开始,不断寻找左节点,并把这些节点依次压入栈内,只有在该节点没有左节点或者它的左子树都已经遍历完成后,它才会从栈内弹出,这时候访问该节点,并它的右节点当做新的根节点一样不断遍历. AC源码 # Definition for a binary tree node

Java [Leetcode 107]Binary Tree Level Order Traversal II

题目描述: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order

Lowest Common Ancestor of a Binary Tree

题目连接 https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ Common Ancestor of a Binary Tree Description Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According to the definition of LCA on

Invert Binary Tree

package cn.edu.xidian.sselab; /** * title:Invert Binary Tree * content: * nvert a binary tree.  *     4 *   /   \ *  2     7 * / \   / \ *1   3 6   9 *to *     4 *   /   \ *  7     2 * / \   / \ *9   6 3   1 */public class InvertBinaryTree { /**    

leetcode笔记:Minimum Depth of Binary Tree

一. 题目描述 Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 二. 题目分析 这道题属于二叉树的深度优先搜索,然后返回深度最小的值,可以递归(当然,也可以使用迭代)来实现.递归退出的条件是到达叶子节点或者到达空子树,使用空子树