参考代码:
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); // 排序元素个数 long[] arrN = new long[n]; // 排序元素数组 int[] arrQuery = new int[n]; // 该数组每位数字查询的次数 查询次数最多的赋予数组元素的最大值 Arrays.fill(arrQuery, 0); // 将arrQuery中元素初始化置为0 for (int i = 0; i < n; i++) { arrN[i] = scanner.nextLong(); } int m = scanner.nextInt(); // 查询个数 long count1 = 0; // 初始的数组查询和 for (int i = 0; i < m; i++) { int Li = scanner.nextInt(); int Ri = scanner.nextInt(); for (int j = Li - 1; j <= Ri - 1; j++) { count1 += arrN[j]; // 求初始的数组查询和 arrQuery[j] ++; // 每查询一次该数组,将该数组查询次数+1 } } scanner.close(); // System.out.println(count1); long[] arrSort = new long[n]; arrSort = arrN; Arrays.sort(arrSort); Arrays.sort(arrQuery); long count2 = 0; // 重新排序之后的最大和 for (int i = 0; i < n; i++) { if(arrQuery[i] != 0) { count2 += (long)(arrSort[i] * arrQuery[i]); } } // System.out.println(count2); System.out.println(count2 - count1); } }
0.0分
3 人评分
点我有惊喜!你懂得!浏览:1338 |
校门外的树 (C语言代码)浏览:1125 |
C二级辅导-同因查找 (C语言代码)浏览:585 |
【出圈】 (C语言代码)浏览:560 |
C语言程序设计教程(第三版)课后习题6.4 (C语言代码)浏览:747 |
求圆的面积 (C语言代码)浏览:1272 |
本人酷爱递归实现很多问题,这里也是浏览:558 |
简单的a+b (C语言代码)浏览:632 |
WU-输出九九乘法表 (C++代码)浏览:1671 |
C语言程序设计教程(第三版)课后习题8.8 (C语言代码)浏览:546 |