zoukankan      html  css  js  c++  java
  • 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;
    		}
    		 
    	}
    	
    
    }
    
    
  • 相关阅读:
    SDK Manager.exe和AVD Manager.exe缺失,Android SDK Tools在检查java环境时卡住了,未响应卡死!
    GetLastError结果列表
    VS2008 远程调试器未成功安装,没法使用?
    远程桌面连接无法相互拷贝文件了?
    VS2008编译错误:error C2065: 'PMIB_TCPSTATS' : undeclared identifier c:program files (x86)microsoft sdkswindowsv7.0aincludeiphlpapi.h 411
    C++ 保存Excel文件(带密码保护)
    UTF8与std:string互转
    将windbg设置为默认调试器命令
    VC++ 链接错误LINK : fatal error LNK1104: cannot open file "*.lib"
    IAP升级程序中Bootloader和APP程序中断复用的解决办法
  • 原文地址:https://www.cnblogs.com/a1439775520/p/13075293.html
Copyright © 2011-2022 走看看