这是第一次笔试代码保留比较完整的,之前的笔试没保留代码,而且做的实在太差,只有看牛客大佬们帖子的份,这次回馈一下。
第一题 热点数据前十条 只通过了64%的测试用例
import javafx.util.Pair; import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.nextLine(); int n = Integer.valueOf(s); Map<Integer, Integer> map = new HashMap<>(); for(int i = 0; i < n; i++){ String s1 = sc.nextLine(); if(s1.equals("query")){ if(map.isEmpty()) System.out.println("null"); else{ Pair[] arr = new Pair[map.size()]; int index = 0; for(int key : map.keySet()){ Pair<Integer,Integer> pair = new Pair<>(key, map.get(key)); arr[index++] = pair; } Arrays.sort(arr, (a, b) -> { if(a.getValue() == b.getValue()){ return (int)a.getKey() - (int)b.getKey(); }else{ return (int)b.getValue() - (int)a.getValue(); } }); int j = 1; for(Pair pair : arr){ if(j > 10) break; System.out.print(pair.getKey() + " "); j++; } System.out.println(); } }else{ String[] split = s1.split(" "); int key = Integer.valueOf(split[1]); int val = Integer.valueOf(split[2]); map.put(key, map.getOrDefault(key, 0) + val); } } } }
第二题 战队
import java.util.*; public class Main2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int k = sc.nextInt(); long total_A = 0; long total_B = 0; for(int i = 0; i < n; i++){ int a = sc.nextInt(); int b = sc.nextInt(); if(b >= k) total_A += a * b; } for(int i = 0; i < m; i++){ int a = sc.nextInt(); int b = sc.nextInt(); if(b >= k) total_B += a * b; } System.out.print(total_A + " " + total_B); if(total_A > total_B) System.out.println("A"); else System.out.println("B"); } }
第三题 排车厢
import java.util.*; public class Main3 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = Integer.valueOf(sc.nextLine()); List<List<Integer>> list = new ArrayList<>(); Map<String, List> map = new HashMap<>(); for(int i = 0; i < N; i++){ String s = sc.nextLine(); String[] split = s.split(" "); int a = Integer.valueOf(split[0]); if(a == 1){ int b = Integer.valueOf(split[1]); String c = split[2]; if(map.containsKey(c)) map.get(c).add(b); else{ List<Integer> arrayList = new ArrayList<>(); arrayList.add(b); map.put(c, arrayList); list.add(arrayList); } }else{ String b = split[1]; String c = split[2]; List<Integer> b_list = map.get(b); List<Integer> c_list = map.get(c); int index_b = 0; int index_c = 0; for(int j = 0; j < list.size(); j++){ if(list.get(j) == b_list) index_b = j; if(list.get(j) == c_list) index_c = j; } list.remove(index_b); list.add(index_b, c_list); list.remove(index_c); list.add(index_c, b_list); } } for(List<Integer> list1 : list){ for(int num : list1){ System.out.print(num + " "); } } } }
第四题 树的不可到达节点
import java.util.*; public class Main4 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int k = sc.nextInt(); List<Node> list = new ArrayList<>(); for(int i = 0; i < n; i++){ list.add(new Node()); } for(int i = 0; i < m; i++){ int root = sc.nextInt() - 1; int left = sc.nextInt() - 1; int right = sc.nextInt() - 1; list.get(root).left = list.get(left); list.get(root).right = list.get(right); } for(int i = 0; i < n; i++){ list.get(i).val = sc.nextInt(); } Node root = list.get(k - 1); int ans = 0; dfs(root, 1, Integer.MAX_VALUE); for(Node node : list){ if(!node.flag){ ans += dfs1(node); } } System.out.println(ans); } public static void dfs(Node node, int left, int right){ if(node == null) return; if(left >= right) { node.flag = false; return; } dfs(node.left, left, Math.min(right, node.val)); dfs(node.right,Math.max(left, node.val), right); } public static int dfs1(Node node){ if(node == null) return 0; return dfs1(node.left) + dfs1(node.right) + 1; } static class Node{ int val; Node left; Node right; boolean flag = true; } }
第五题 扑克牌 没时间,骗分18%
import java.util.Scanner; public class Main5 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); for(int i = 0; i < N - 1; i++){ if(i % 2 == 0)System.out.println("Tuan"); else System.out.println("Draw"); } System.out.println("Mei"); } }
全部评论
(2) 回帖