2023 顺丰菁英挑战赛——编程方向Java题解
第一题
模拟
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String s = in.nextLine(); StringBuilder str1 = new StringBuilder(); StringBuilder str2 = new StringBuilder(); StringBuilder num = new StringBuilder(); for(char c : s.toCharArray()){ if(Character.isDigit(c)){ num.append(c); }else if(Character.isLowerCase(c)){ str1.append(c); }else if(Character.isUpperCase(c)){ str2.append(c); } } System.out.println(str1.toString()); System.out.println(str2.toString()); System.out.println(num.toString()); } }
第三题
建图最短路+贪心
import java.util.Scanner; import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(), m = sc.nextInt(), x = sc.nextInt(); List<int[]>[] g = new List[n + 1]; boolean[] vis = new boolean[n + 1]; long[] f = new long[n + 1]; for (int i = 1; i <= n; i++) { g[i] = new ArrayList<int[]>(); } for (int i = 0; i < m; i++) { int u = sc.nextInt(), v = sc.nextInt(), w = sc.nextInt(); g[u].add(new int[] {v, w}); g[v].add(new int[] {u, w}); } if (g[1].isEmpty() || g[n].isEmpty()) { System.out.println(-1); return; } Arrays.fill(f, Long.MAX_VALUE); PriorityQueue<long[]> que = new PriorityQueue<>((a, b)-> Long.compare(a[0], b[0])); //存放 [距起点 1 的距离, 节点索引] f[1] = 0; que.offer(new long[] {0, 1}); while (!que.isEmpty()) { long[] tmp = que.poll(); int idx = (int)tmp[1]; if (vis[idx]) { continue; } vis[idx] = true; for (int[] e : g[idx]) { int i = e[0], w = e[1]; if (f[i] > f[idx] + w) { f[i] = f[idx] + w; que.offer(new long[] {f[i], i}); } } } long ans = x, mn = Integer.MAX_VALUE; for (int[] e : g[1]) { mn = Math.min(mn, e[1]); } ans += mn; mn = Integer.MAX_VALUE; for (int[] e : g[n]) { mn = Math.min(mn, e[1]); } ans += mn; System.out.println( Math.min(ans, f[n])); } }
第四题
import java.util.Scanner; import java.util.PriorityQueue; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); //贪心,优先用工资更少的人,工资相同的优先用满意度更大的 PriorityQueue<int[]> st1 = new PriorityQueue<>((a,b)->a[0]==b[0]?b[1]-a[1]:a[0]-b[0]); PriorityQueue<int[]> st2 = new PriorityQueue<>((a,b)->a[0]==b[0]?b[1]-a[1]:a[0]-b[0]); int n = in.nextInt(); int x = in.nextInt(); int y = in.nextInt(); in.nextLine(); for(int i=0;i<n;++i){ String str = in.nextLine(); // System.out.println(str); String[] arr = str.split(" "); int a = Integer.parseInt(arr[1]); int b = Integer.parseInt(arr[2]); if(arr[0].charAt(0)=='A'){ st1.offer(new int[]{a,b}); }else{ st2.offer(new int[]{a,b}); } } if(st2.size()< y || st2.size()- y + st1.size()<x){ System.out.println(-1); return; } long res = 0; // 优先把跨城的用完,然后把剩下的跨城快递员安排到同城的里面 while(y-->0){ res += st2.poll()[1]; } while(!st2.isEmpty()){ st1.offer(st2.poll()); } while(x-->0){ res += st1.poll()[1]; } System.out.println(res); } }#笔试##算法题##比赛#