点击题号跳转

A2441 B2293 C1724 D3462 E1608

F5065 G2427 H5466 I5603 J3299

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 class Main {
 4 
 5     public static void main(String[] args) {
 6 
 7         Scanner sc = new Scanner(System.in);
 8         Solution solver = new Solution();
 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 class Solution {
19 
20     class Node {
21         String name;
22         int sumCost;
23 
24         Node(String name, int sumCost) {
25             this.name = name;
26             this.sumCost = sumCost;
27         }
28     }
29 
30     public void solve(Scanner sc) {
31         int n = sc.nextInt();
32         int m = sc.nextInt();
33         int agencies = sc.nextInt();
34         sc.nextLine();// 读回车
35         List<Node> list = new ArrayList<Node>();
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<Node>() {
60             public int compare(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

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-11-01
  • 2021-12-24
  • 2021-11-01
  • 2021-09-16
  • 2021-05-07
  • 2021-06-12
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案