点击题号跳转
A.Work Reduction回到顶部
题意
n变成m,两种操作,减1花费A,整除2花费B,问最小花费
题解
n变成n/2,操作1需要花费(n-n/2)*A,操作2需要花费B,比大小决定,复杂度O(logn)
代码
1 import java.util.*;2
3 public classMain {4
5 public static voidmain(String[] args) {6
7 Scanner sc = newScanner(System.in);8 Solution solver = newSolution();9 int t =sc.nextInt();10 for (int ca = 1; ca <= t; ca++) {11 System.out.println("Case " +ca);12 solver.solve(sc);13 }14 }15
16 }17
18 classSolution {19
20 classNode {21 String name;22 intsumCost;23
24 Node(String name, intsumCost) {25 this.name =name;26 this.sumCost =sumCost;27 }28 }29
30 public voidsolve(Scanner sc) {31 int n =sc.nextInt();32 int m =sc.nextInt();33 int agencies =sc.nextInt();34 sc.nextLine();//读回车
35 List list = new ArrayList();36 for (int i = 0; i < agencies; i++) {37 String s =sc.nextLine();38 String[] str = s.split(":");39 String[] str1 = str[1].split(",");40 int aCost = Integer.valueOf(str1[0]);41 int bCost = Integer.valueOf(str1[1]);42 int val =n;43 int sumCost = 0;44 while (val >m) {45 int updateTo = val / 2;46 int aSumCost = aCost * (val -updateTo);47 if (updateTo >= m && aSumCost >bCost) {48 sumCost +=bCost;49 val =updateTo;50 } else{51 //剩下一段全A
52 sumCost += (val - m) *aCost;53 break;54 }55 }56 list.add(new Node(str[0], sumCost));57 }58
59 Collections.sort(list, new Comparator() {60 public intcompare(Node o1, Node o2) {61 if (o1.sumCost >o2.sumCost)62 return 1;63 else if (o1.sumCost ==o2.sumCost) {64 if (o1.name.compareTo(o2.name) > 0)65 return 1;66 else
67 return -1;68 }69 else return -1;70 }71 });72
73 for (int i = 0; i < list.size(); i++)74 System.out.println(list.get(i).name + " " +list.get(i).sumCost);75 }76
77 }
A
B.Collecting Beepers
题意
10个点,给个起点,问经过每个点再回到起点的最小距离
题解
爆搜,最后加上到起点的距离,复杂度O(2^10)
代码
1 import java.util.*;2
3 public classMain {4
5 public static voidmain(String[] args) {6
7 Scanner sc = newScanner(System.in);8 Solution solver = newSolution();9 int t =sc.nextInt();10 for (int ca = 1; ca <= t; ca++) {11 solver.solve(sc);12 }13 }14
15 }16
17 classSolution {18
19 private int[] x;20 private int[] y;21 private boolean[] vis;22 private intq;23 private intsx, sy;24 private intmin;25
26 public voidsolve(Scanner sc) {27 int n =sc.nextInt();28 int m =sc.nextInt();29 sx =sc.nextInt();30 sy =sc.nextInt();31 q =sc.nextInt();32 x = new int[q];33 y = new int[q];34 vis = new boolean[q];35 min = 1000000000;36 for (int i = 0; i < q; i++) {37 x[i] =sc.nextInt();38 y[i] =sc.nextInt();39 }40 dfs(0, 0, sx, sy);41 System.out.println("The shortest path has length " +min);42 }43
44 private void dfs(int step, int cost, int ux, intuy) {45 if (step ==q) {46 min = Math.min(min, c