publicclassMain{ publicstaticvoidmain(String[] args){ Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int m = scanner.nextInt(); ArrayList<Integer> arrayList = new ArrayList<>(); for (int i = 0; i < n; i++) { // 计算第一个 连续m个数的和 arrayList.add(scanner.nextInt()); // 接受n个数 } int temp = 0; for (int i = 0; i < m; i++) { temp = temp + arrayList.get(i); } int min = temp; for (int i = 0; i < n - m; i++) { // 循环n-m次,因为第一次已经计算并存储到min中了 temp = temp + arrayList.get(i+m) - arrayList.get(i); if (temp < min){ min = temp; } } System.out.println(min); } }
publicclassMain{ publicstaticvoidmain(String[] args){ Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); Interval[] intervals = new Interval[n]; for (int i = 0; i < n; i++) { intervals[i] = new Interval(scanner.nextInt(), scanner.nextInt()); } Arrays.sort(intervals); // 根据我们制定的规则排序 int sum = 0; int now = 0; for (Interval interval : intervals) { if (now <= interval.begin){ // 当前天数小于等于开始时间才可以参加(从给出的例子中可以看出是小于等于) sum++; now = interval.end; } } System.out.println(sum);
}
privatestaticclassIntervalimplementsComparable<Interval>{ int begin; int end;
publicclassMain{ publicstaticvoidmain(String[] args)throws IOException { InputStreamReader inputStreamReader = new InputStreamReader(System.in); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String[] strings = bufferedReader.readLine().split(" "); int n = new Integer(strings[0]); // 接水人数 int m = new Integer(strings[1]); // 龙头个数 int[] arr = newint[n]; String[] strings2 = bufferedReader.readLine().split(" "); for (int i = 0; i < n; i++) { arr[i] = new Integer(strings2[i]); } PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(m); for (int i = 0; i < m; i++) { priorityQueue.add(arr[i]); } int waitTime = 0; // 一个同学完成接水后花费的时间(即下一个同学等待的时间) for (int i = m; i < n; i++) { waitTime = priorityQueue.poll(); priorityQueue.add(arr[i]+waitTime); // 关键的一步,将进入队列的同学的等待时间加上 } int result = 0; while (!priorityQueue.isEmpty()){ // 上一个for循环结束后,队列中还有m个元素 result = priorityQueue.poll(); } System.out.println(result); } }
long sum = a; ArrayList<Long> arrayList = new ArrayList<>(); for (long l = n; l > 0; l--) { if (sum-l<0){ continue; } if (sum==0){ break; } arrayList.add(l); sum = sum - l; }
publicclassMain{ publicstaticvoidmain(String[] args)throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); String[] strings = bufferedReader.readLine().split(" "); long a = new Long(strings[0]); // 接受a的值 long b = new Long(strings[1]); // 接受b的值 long n = func(a + b); if (n==-1||a==0&&b==0){ System.out.println("No"); }else {// 这一段代码是关键部分(即在连续的1,2,3...n这n个数中找到若干个数相加为a) // 即经典的寻找和为定值的多个数问题(背包问题的应用) // 但是由于题目只需要一种可能的解,我们可以用贪心思想来实现输出一种可能的解 StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append(n); long sum = a; ArrayList<Long> arrayList = new ArrayList<>(); for (long l = n; l > 0; l--) { if (sum-l<0){ continue; } if (sum==0){ break; } arrayList.add(l); sum = sum - l; } for (Long aLong : arrayList) { stringBuilder.append(" ").append(aLong); // 注意是先加空格再加数字,这样才能保证最后一个数之后没有空格 } stringBuilder.append(" "); System.out.print(stringBuilder); } }
privatestaticlongfunc(long c){ long x = (long) Math.sqrt(2*c); // 根据求跟公式求出正数解 if (x*(x+1)==2*c){ // 说明是整数解 return x; }else { // 没有整数解 return -1; } }
publicclassMain{ privatestatic LinkedList<Long> linkedList = new LinkedList<>(); privatestaticint count = 0; publicstaticvoidmain(String[] args)throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); String[] strings = bufferedReader.readLine().split(" "); long a = new Long(strings[0]); // 接受a的值 long b = new Long(strings[1]); // 接受b的值 long n = func(a + b); if (n==-1){ System.out.println("No"); }else {// 这一段代码是关键部分(即在连续的1,2,3...n这n个数中找到若干个数相加为a) // 即经典的寻找和为定值的多个数问题(背包问题的应用) System.out.print(n); sumOfkNumber(a,n); }
}
privatestaticlongfunc(long c){ long x = (long) Math.sqrt(2*c); // 根据求跟公式求出正数解 if (x*(x+1)==2*c){ // 说明是整数解 return x; }else { // 没有整数解 return -1; } }
privatestaticvoidsumOfkNumber(long sum, long n){ // 递归出口 if (n <= 0 || sum <= 0) return; // 输出链表 if (sum == n) { if (count==0){ StringBuilder stringBuilder = new StringBuilder(" "); for (Long num : linkedList) { stringBuilder.append(num+ " "); } stringBuilder.append(n); // 别忘了最后还有一个n要输出 System.out.println(stringBuilder); count++; } } linkedList.add(n); //典型的01背包问题 sumOfkNumber(sum-n,n-1); //放n,前n-1个数填满sum-n linkedList.remove(linkedList.size()-1); sumOfkNumber(sum,n-1); } }
结果报Runtime Error的错。于是我把代码改进成我的题解代码,最开始还是报错。。。报的是WA错,描述为:Wrong Answer. wrong output format Unexpected character #10 , but ' ' expected。万万没想到,是我的输出语句System.out.println(stringBuilder);出了问题,因为println最后会有一个回车,改成System.out.print(stringBuilder);就通过了!