fork download
  1. // your code goes here// your code goes here
  2. let t=parseInt(readline()); //testcase
  3. while(t--){
  4. let n=parseInt(readline());//number of ships
  5. let arr=readline().split(' ').map(Number); //array of ships
  6. let arr2=readline().split(' ').map(Number);
  7. let x=arr2[0];
  8. let y=arr2[1];
  9.  
  10. let arr3=readline().split(' ').map(Number);
  11. let h=arr3[0];
  12. let d=arr3[1];
  13.  
  14. let shot=0; // variable to store count of number od shots.
  15. arr.sort((a,b)=>(b-a));//sort array in descending order so that
  16. //we can hit the ship with highest health first
  17.  
  18. for(let i=0;i<n;i++){
  19. arr[0]=arr[0]-x; //reduce health of first ship by x
  20. for(let j=1;j<n;j++){
  21. arr[j]=arr[j]-y; //reduce health of remaining ships by y
  22. }
  23. h=h-d; // reduce health of cannon by d after each shot
  24. shot++;
  25. console.log(arr);
  26. if(!checkArray(arr,n) && h>0){ //check if all ships are destroyed or not and
  27. console.log(shot); // if yes then print shots and break out of loop
  28. break;
  29. }
  30. if(h<=0 && checkArray(arr,n)){ //check health of cannon in each iteration and
  31. // if found less than equal to 0,print -1 and exit loop
  32. console.log(-1);
  33. break;
  34. }
  35.  
  36.  
  37. arr.sort((a,b)=>(b-a));// again sort in descending so that
  38. //we can hit ship with highest health first
  39.  
  40. }
  41.  
  42.  
  43. }
  44. function checkArray(arr,n){
  45. for(let i=0;i<n;i++){
  46. if(arr[i]>0)
  47. return true;
  48. }
  49. return false;
  50. }
  51.  
Success #stdin #stdout 0.03s 17424KB
stdin
2
4
8 7 5 1
5 3
10 2
2 
10 20
10 4
10 5
stdout
3,4,2,-2
-1,0,-1,-5
2
10,6
0,2
-1