第二题: import java.util.Scanner; import java.util.Stack; public class Problem2 { private static void solution(int[] players, int[] res) { int[] projection = new int[players.length + 1]; for (int i = 0; i < players.length; ++ i) { projection[players[i]] = i; } int[] resProjection = new int[players.length]; for (int i = 0; i < res.length; ++ i) { resProjection[i] = projection[res[i]]; } Stack<Integer> stack = new Stack<>(); int count = 0; for (int i = 0; i < res.length; ++ i) { while (! stack.empty() && resProjection[stack.peek()] > resProjection[i]) { stack.pop(); count ++; } stack.push(i); } System.out.println(count); } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int length = scanner.nextInt(); int[] players = new int[length]; int[] res = new int[length]; for (int i = 0; i < length; ++ i) { players[i] = scanner.nextInt(); } for (int i = 0; i < length; ++ i) { res[i] = scanner.nextInt(); } solution(players, res); } }
点赞 评论

相关推荐

给🐭🐭个面试机会...:我擦seed✌🏻
点赞 评论 收藏
分享
酷酷的喜马拉雅山:感觉这比一直在初筛不动的好多了
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务