• Java实现蓝桥杯VIP算法训练 自行车停放


    试题 算法训练 自行车停放

    资源限制
    时间限制:1.0s 内存限制:256.0MB
    问题描述
      有n辆自行车依次来到停车棚,除了第一辆自行车外,每辆自行车都会恰好停放在已经在停车棚里的某辆自行车的左边或右边。(e.g.停车棚里已经有3辆自行车,从左到右编号为:3,5,1。现在编号为2的第4辆自行车要停在5号自行车的左边,所以现在停车棚里的自行车编号是:3,2,5,1)。给定n辆自行车的停放情况,按顺序输出最后停车棚里的自行车编号。
    输入格式
      第一行一个整数n。
      第二行一个整数x。表示第一辆自行车的编号。
      以下n-1行,每行3个整数x,y,z。
      z=0时,表示编号为x的自行车恰停放在编号为y的自行车的左边
      z=1时,表示编号为x的自行车恰停放在编号为y的自行车的右边
    输出格式
      从左到右输出停车棚里的自行车编号
    样例输入
    4
    3
    1 3 1
    2 1 0
    5 2 1
    样例输出
    3 2 5 1
    数据规模和约定
      n<=100000
      自行车编号为不超过100000的正整数。

    package 第九次模拟;
    
    import java.util.ArrayList;
    import java.util.Scanner;
    
    public class 自行车排放 {
    	public static class TreeNode{
    		int left;
    		int right;
    	}
    	public static void main(String[] args) {
    //		ArrayList<Integer> list = new ArrayList<Integer>();
    //		ArrayList<Integer> index = new ArrayList<Integer>();
    		 Scanner sc = new Scanner(System.in);
    		 int n = sc.nextInt();
    		 TreeNode [] node = new TreeNode[100000+2];
    //		 for (int i = 0; i < 100000; i++) {
    //			node[i]=new TreeNode();
    //			node[i].left=-1;
    //			node[i].right=-1;
    //		}
    		 int k = sc.nextInt();
    //		node[k]=new TreeNode();
    //		 node[k].left=0;
    //		 node[k].right=100000;
    ////		 node[100000]=new TreeNode();
    //		 node[n+1] = new TreeNode();
    //		 node[n+1].left=k;
    ////		 node[100000].right=100000;
    		 for (int i = 0; i <= 100001; i++) {
    			 node[i]=new TreeNode();
    				node[i].left = -1;
    				node[i].right = -1;
    			}
    		 node[k].left = 0;
    		
    		 node[k].right = 100001;
    		 node[0].right = k;
    		 node[n + 1].left = k;
    		 for (int i = 0; i <n-1; i++) {
    			int x=sc.nextInt();
    			int y = sc.nextInt();
    			int z = sc.nextInt();
    //			node[x]=new TreeNode(); 
    			
    			if (z == 0) {
    				node[x].left = node[y].left;
    				node[x].right = y;
    				node[node[y].left].right = x;
    				node[y].left = x;
    			} else {
    				
    				node[x].right = node[y].right;
    				node[x].left = y;
    				node[node[y].right].left = x;
    				node[y].right = x;
    			}
    		}
    		 int index=0;
    		 for (;;) {
    			 if(node[index].right==100001) break;
    			System.out.print (node[index].right+" ");
    			index=node[index].right;
    		}
    		 
    	}
    	
    
    }
    
    
  • 相关阅读:
    累加和校验算法(CheckSum算法)
    云锵投资 2021 年 09 月简报
    云锵投资 2021 年 08 月简报
    断言与忽略断言
    出现 undefined reference to `cv::String::deallocate()'的解决方法
    about of string
    esp32: A stack overflow in task spam_task has been detected.
    IDEA部署Tomcat报错:No artifacts marked for deployment
    在safari浏览器上使用php导出文件失败
    laravel中使用vue热加载时 Cannot read property 'call' of undefined BUG解决方案
  • 原文地址:https://www.cnblogs.com/a1439775520/p/13075941.html
Copyright © 2020-2023  润新知