代码,通过了100%的数据:
import java.util.Scanner;public class 接水问题 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int m=sc.nextInt(); int[] data=new int[n]; int[] shui=new int[m]; for (int i = 0; i < data.length; i++) { data[i]=sc.nextInt(); if(i<m) shui[i]=data[i]; } int count=0; while(getBool(shui)){ int min=getMin(shui); count+=min; for (int i = 0; i < shui.length; i++) { shui[i]-=min; if(shui[i]==0&&m<data.length) shui[i]=data[m++]; } } System.out.PRintln(count); } private static int getMin(int[] shui) { int k=100*100; for (int i = 0; i < shui.length; i++) { if(k>shui[i]&&shui[i]>0) k=shui[i]; } return k; } private static boolean getBool(int[] shui) { for (int i = 0; i < shui.length; i++) { if(shui[i]>0) return true; } return false; }}
新闻热点
疑难解答