fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. Scanner sc = new Scanner(System.in);
  13. int t = sc.nextInt();
  14. for(int j=0;j<t;j++){
  15. int n= sc.nextInt();
  16. int h= sc.nextInt();
  17. int k= sc.nextInt();
  18. int [] arr= new int[n];
  19. int sum=0;
  20. int [] suffix= new int[n];
  21. int [] prefix= new int[n];
  22. for(int m=0;m<n;m++){
  23. arr[m]=sc.nextInt();
  24. sum=sum+arr[m];
  25. }
  26. prefix[0]=arr[0];
  27. for(int m =1;m<n;m++){
  28. prefix[m]=Math.min(arr[m],prefix[m-1]);
  29. }
  30. suffix[n-1]=arr[n-1];
  31. for(int m =n-2;m>=0;m--){
  32. suffix[m]=Math.max(arr[m],prefix[m+1]);
  33. }
  34. double dps=(double)sum/n;
  35. double timeRequired = Math.ceil(h/dps);
  36. int reloads = (int)(timeRequired/n)-1;
  37. reloads= reloads>=0 ? reloads : 0;
  38. int reloadShot = (int)timeRequired%n;
  39. if(reloadShot==0){
  40. System.out.println(timeRequired+(k*reloads));
  41. }
  42. else{
  43. double remaining = h-(dps*n*reloads);
  44. int dpsLeft=0;
  45. for(int m=0;m<reloadShot-1;m++){
  46. dpsLeft=dpsLeft+arr[m];
  47. }
  48. remaining=remaining-dpsLeft;
  49. timeRequired=timeRequired-(int)(timeRequired%n);
  50. if(remaining<=0){
  51. System.out.println(timeRequired+(k*reloads)+reloadShot-1);
  52. }
  53. else{
  54. remaining=remaining-suffix[reloadShot]+prefix[reloadShot-1];
  55. if(remaining<=0){
  56. System.out.println(timeRequired+(k*reloads)+reloadShot-1);
  57. }
  58. else{
  59. System.out.println((int)timeRequired+(k*reloads)+reloadShot);
  60.  
  61. }
  62. }
  63. }
  64. }
  65. }
  66. }
Success #stdin #stdout 0.18s 54460KB
stdin
1
5 10 1
4 2 3 5 3
stdout
3