求救!!第二个点wa,感觉思路没什么问题qwq
查看原帖
求救!!第二个点wa,感觉思路没什么问题qwq
776698
c0621zy楼主2023/6/25 13:11
import java.util.*;
public class t1160 {
	public static void main(String[] args) {
		int[] a = new int[100000];
		LinkedList<Integer> res = new LinkedList<>();
		res.addFirst(1);  //添加1号同学
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt(),k,p,index,m;
		for(int i = 2;i<=n;i++) {
			k = sc.nextInt();  
			p = sc.nextInt();
			index = res.indexOf(k);
			if(index == 0 && p == 0)  //若在0索引左边插入,相当于在头部插入
				res.addFirst(i);
			else if(index == (res.size() - 1) && p == 1)  //在length-1右边插入,相当于往末尾插入
				res.addLast(i);
			else if(p == 0)  //往左边插入
				res.add(index -1,(Integer)i);
			else  //往右边插入
				res.add(index + 1,(Integer)i);
		}
		m = sc.nextInt();  //读入删除的个数
		for(int i = 0; i < m ;i++) {
			k = sc.nextInt();  		
			if(a[k] == 0) {  //a[k]为0表示k号还没删除			
			res.remove((Integer)k);  //根据内容删除节点,不是根据索引
			a[k] = 1;
			}
		}
		for(Integer i:res) {
			System.out.print(i+" ");
		}
		
	}
}
2023/6/25 13:11
加载中...