顺丰9.7笔试 算法搬砖题
这题的思路我是先排序数组,从后面遍历k个可以用卡车运输的,如果卡车运的时间比手工搬长,就提前退出循环,剩下的砖都是手工搬。考虑了int溢出,还是通过不了全部,一直是82%,有友友说说思路嘛
#顺丰#
public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(), x = sc.nextInt(), y = sc.nextInt(), k = sc.nextInt(); int[] nums = new int[n]; for (int i = 0; i < n; i++) { nums[i] = sc.nextInt(); } Arrays.sort(nums); long ans = 0; int end = n - 1; for (int i = 0; i < k; i++) { long tmp = nums[end] * x; if (tmp < y) { break; } ans += y; end--; } for (int i = 0; i <= end; i++) { ans += nums[i] * x; } System.out.println(ans); } }
#顺丰#