fork(22) download
  1. import java.io.*;
  2. import java.util.Scanner;
  3.  
  4. public class Main {
  5. public static void main(String args[]) throws IOException {
  6. int n, rmw, trm, tt, tm, tw, tc, rm, rw, rc, i = 0;
  7. Scanner sc = new Scanner(System.in);
  8. n = sc.nextInt(); //No. of test cases
  9. int total[] = new int[n];
  10. int men[] = new int[n];
  11. int women[] = new int[n];
  12. int children[] = new int[n];
  13. while(i<n){
  14. total[i] = sc.nextInt();
  15. men[i] = sc.nextInt();
  16. women[i] = sc.nextInt();
  17. children[i] = sc.nextInt();//Store inputs in arrays, easy to access
  18. i++;
  19. }
  20. i = 0;
  21. while(i<n){
  22. tt = total[i];
  23. tm = men[i];
  24. tw = women[i];
  25. tc = children[i];
  26.  
  27. if(tc > tt)
  28. tc = tt;
  29.  
  30. /*if(tm==0){ //Special case 1
  31.  
  32. }
  33. if(tw==0){ //Special case 2
  34.  
  35. }
  36. if(tc==0){ //Special case 3
  37.  
  38. }*/
  39.  
  40. //Normal case
  41. if(tc%4==0)
  42. rmw = tc/4;
  43. else
  44. rmw = tc/4 +1;
  45.  
  46. while(rmw+tc>tt){
  47. tc--;
  48. if(tc%4==0)
  49. rmw = tc/4;
  50. else
  51. rmw = tc/4 +1;
  52. }
  53. if((tm+tw)<rmw){
  54. rmw = tm + tw;
  55. tc = rmw*4;
  56. }
  57. rc = tc; //Children rescued
  58. tt = tt - rc;
  59.  
  60. if(tw%2==0)
  61. trm = tw/2;
  62. else
  63. trm = tw/2 +1;
  64. while(trm+tw>tt){
  65. tw--;
  66. if(tw%2==0)
  67. trm = tw/2;
  68. else
  69. trm = tw/2 +1;
  70. }
  71. if(tm<trm){
  72. tw = tm*2;
  73. rmw = tm + tw;
  74. if(rmw*4<rc){
  75. tc = rmw*4;
  76. tt = tt + rc;
  77. rc = tc;
  78. tt = tt - rc;
  79. }
  80. }
  81. rw = tw; //Women rescued
  82. tt = tt - rw;
  83.  
  84.  
  85. if(tt>=tm)
  86. rm = tm;
  87. else
  88. rm = tt;
  89. System.out.println(rm+" "+rw+" "+rc);
  90. i++;
  91. }
  92. sc.close(); //cout<<men[2];
  93. }
  94. }
  95.  
  96.  
Success #stdin #stdout 0.1s 380608KB
stdin
1
9999 9999 9999 9999
stdout
667 1333 7999