第一题背包求解 暴力只过了0.3 public long Solve(int n, int m, int[] weights) { long min = 0; long max = 0; for(int i=0;i<n;i++){ min+=weights[i]; if(weights[i]>max) max = weights[i]; } if(min%m!=0) min=(long)(min/m)+1; else min = min/m; min=Math.max(min, max); while(beibao(min,n,m,weights)==false) { System.out.println(min); min++; } return min; } public boolean beibao(long min,int n,int m,int[] weights) { int t = m-1; //t是指还剩几个背包 long k = 0; //k指当前背包装了多少东西 for(int i=0;i<n;++i) { if(k+weights[i]<=min && t>=0) { k+=weights[i]; continue; }else if(t>0) { //当前背包装不下第i个,换个新背包装第i个,且最少有一个新背包 --t; k=weights[i]; } else{ //所有背包都用完,t=0 //还没有跳出循环,即还有石头没装下 return false; } } return true; }