大厂笔试题
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.2 KiB

  1. /*
  2. m个箱子用于打包行李wn件行李排成一行ai
  3. ?
  4. 5 2 8
  5. 6 3 2 5 3
  6. 4
  7. */
  8. #include <stdio.h>
  9. int main(){
  10. int n, m;
  11. long long w;
  12. scanf("%d %d %lld",&n,&m, &w);
  13. long long weights[n];
  14. for(int i=0;i<n; ++i)scanf("%lld",&weights[i]);
  15. int boxcount =1;
  16. long long currentweight =0;
  17. int rightIndex=n-1;
  18. while(rightIndex>=0){
  19. if(currentweight + weights[rightIndex]<=w){
  20. currentweight += weights[rightIndex];
  21. } else {
  22. boxcount++;
  23. currentweight = weights[rightIndex];
  24. }
  25. rightIndex--;
  26. if(boxcount>m){
  27. rightIndex++;
  28. break;
  29. }
  30. }
  31. int itemsPacked=n - (rightIndex + 1);
  32. printf("%d\n",itemsPacked);
  33. return 0;
  34. }