Advertisement
AndresRicardoTorres

Copying books

Sep 27th, 2011
1,869
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.37 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.util.Arrays;
  4. import java.util.StringTokenizer;
  5.  
  6. public class Main {
  7.  
  8.     /**
  9.      * @param args
  10.      * @throws IOException
  11.      * @throws NumberFormatException
  12.      */
  13.     static int[] libros;
  14.     static int trabajadores;
  15.  
  16.     public static void main(String[] args) throws Exception {
  17.         // TODO Auto-generated method stub
  18.         BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
  19.         int casos, cantLibros, max, min, result, contSlashes, sumaTmp;
  20.         StringTokenizer sk;
  21.         boolean[] slash;
  22.  
  23.         casos = Integer.parseInt(bf.readLine());
  24.         for (int i = 0; i < casos; i++) {
  25.             sk = new StringTokenizer(bf.readLine());
  26.             cantLibros = Integer.parseInt(sk.nextToken());
  27.             trabajadores = Integer.parseInt(sk.nextToken());
  28.             contSlashes = cantLibros - 1;
  29.             slash = new boolean[contSlashes];// slash entre el libro i e i+1
  30.             Arrays.fill(slash, true);
  31.  
  32.             libros = new int[cantLibros];
  33.             sk = new StringTokenizer(bf.readLine());
  34.             max = 0;
  35.             min = 0;
  36.             for (int j = 0; j < cantLibros; j++) {
  37.                 libros[j] = Integer.parseInt(sk.nextToken());
  38.                 if (libros[j] > min)
  39.                     min = libros[j];
  40.                 max += libros[j];
  41.             }
  42.             result = binary_search(min, max);
  43.  
  44.             sumaTmp = 0;
  45.             for (int j = slash.length - 1; j > -1; j--) {
  46.                 if ((contSlashes <= (trabajadores - 1)))
  47.                     break;
  48.                 sumaTmp += libros[j + 1];
  49.                 if (sumaTmp + libros[j] <= result && slash[j]) {
  50.                     slash[j] = false;
  51.                     contSlashes--;
  52.                 }
  53.                 if (slash[j])
  54.                     sumaTmp = 0;
  55.             }
  56.  
  57.             System.out.print(libros[0]);
  58.             for (int j = 0; j < slash.length; j++) {
  59.                 if (slash[j])
  60.                     System.out.print(" / ");
  61.                 else if (j != libros.length - 1)
  62.                     System.out.print(" ");
  63.                 System.out.print(libros[j + 1]);
  64.             }
  65.             if (i != casos - 1)
  66.                 System.out.println();
  67.             // System.out.println(Arrays.toString(libros));
  68.             // System.out.println(Arrays.toString(slash));
  69.         }
  70.     }
  71.  
  72.     static public int binary_search(int lo, int hi) // El minimo con el que funciona
  73.     {
  74.         int mid = 0;
  75.         while (lo < hi) {
  76.             mid = lo + (hi - lo) / 2;
  77.             if (check(mid) == true)
  78.                 hi = mid;
  79.             else
  80.                 lo = mid + 1;
  81.         }
  82.  
  83.         if (check(lo) == false)
  84.             return -1;
  85.         else
  86.             return lo;
  87.     }
  88.  
  89.     static public boolean check(int carga) {
  90.         int trab = 0;
  91.         int tmpSum = 0;
  92.         for (int i = 0; i < libros.length; i++) {
  93.             if (tmpSum + libros[i] <= carga)
  94.                 tmpSum += libros[i];
  95.             else {
  96.                 trab++;
  97.                 tmpSum = libros[i];
  98.             }
  99.         }
  100.         if (tmpSum <= carga)
  101.             trab++;
  102.         return trab <= trabajadores;
  103.     }
  104. }
  105.  
  106.  
  107. /*
  108. 16
  109. 12 5
  110. 48 65 53 3 90 3 90 30 52 75 54 22
  111. 10 4
  112. 82 36 6 11 33 33 25 79 9 7
  113. 9 5
  114. 64 16 75 91 1 71 43 81 13
  115. 9 3
  116. 9 8 1 7 6 2 3 4 5
  117. 3 2
  118. 1 2 1
  119. 8 4
  120. 10 2 10 2 15 20 1 30
  121. 30 8
  122. 46 18 71 6 35 59 35 52 83 62 85 71 72 88 46 100 8 5 89 85 61 80 59 43 39 51 31 100 90 40
  123. 500 50
  124. 198429 225879 591107 583645 268500 151557 563518 248740 722030 674754 235494 165111 4520 807760 7269 710494 636769 334106 963142 541561 171837 78214 5965 883949 226710 299323 900222 102726 122447 163449 307908 873977 702390 901413 140877 843943 656618 304012 907339 34834 658439 148739 243226 352932 473060 122583 390594 545667 566414 769880 54794 927040 819584 300102 761380 412128 566990 766983 318949 691513 377512 157581 131870 155107 47900 261108 618196 849399 78421 650093 429882 664391 80298 592935 628864 370927 214123 492610 978070 61186 297876 235107 170694 414682 523083 901046 437256 306467 573591 359337 875292 778025 470884 701014 31795 345026 324202 791498 696259 900218 333581 770868 976227 701569 204631 312424 590689 135264 31786 850965 100374 642051 278100 311220 364042 446340 711410 5176 68123 756459 13430 189598 323062 63556 177330 535293 506933 866114 302950 30504 680091 868833 319799 503602 319554 961825 227936 774472 167523 456540 768584 831825 957276 292280 773926 128255 881208 991461 358430 762594 148155 225787 23283 46559 549856 505529 960914 242559 755090 753257 164380 608422 329481 922946 179008 203746 221602 476370 250162 493750 721156 595250 774471 135868 631691 960930 134439 152474 580281 869130 427253 894328 852244 275992 304562 228685 825766 165977 337864 608874 77083 83037 959566 836837 791824 454050 561105 776791 834506 480150 966327 281397 319832 395378 771665 442373 970685 467177 439158 159457 75347 935617 539027 343771 179989 418739 137422 529877 141552 174986 492927 177445 598931 232239 377814 661774 339044 361603 979889 820655 568922 662296 910813 894657 538153 203560 333530 625555 61562 510683 477338 255431 166783 836501 85264 83957 719628 101784 281429 443673 955446 780613 787113 243172 892947 612250 16267 419960 946403 279025 965493 749942 431565 409349 585400 93642 618251 749098 980731 685603 415042 976746 475490 660470 578759 487447 271643 638432 393958 891775 767131 676623 535567 232413 700618 348825 971863 445381 296164 2629 59751 431585 673073 864507 758631 785422 325968 140684 657034 355143 509594 636610 950725 932263 444819 516898 542988 834674 865585 881331 16093 963873 718120 796405 553691 331891 718001 51733 290238 80315 459943 376249 545094 433518 508487 386036 683954 860464 313346 120405 114055 268164 594847 122581 239915 658833 7172 568797 215259 428107 532799 55728 911849 475560 240223 698220 886813 455973 392124 270443 224029 661415 865820 739092 136893 551824 692232 841494 971008 280350 638878 448007 741625 968839 336399 302198 613335 154362 808391 497471 557747 847473 772339 37606 286386 618458 764286 777064 399574 301393 437041 185330 470170 129889 59897 23952 20660 210430 860125 593983 682568 196016 590736 790523 209102 302526 5104 996233 920238 681565 182650 683855 626972 880027 830125 695347 780381 201717 195558 289103 735989 755440 810417 701663 60882 900789 127515 223784 563781 937038 343232 730319 325741 883902 942707 297927 680658 299050 753989 12233 797143 604734 129908 536677 324282 496847 702928 452511 434497 244819 571622 365775 871803 115496 909997 293049 16069 759937 524808 447231 125365 487528 298355 674720 35886 750638 349355 138458 311402 36476 360745 668991 770441 990497 353708 21727 204313 330968 486560 938009 427241 582572 396421 52658 507560 51008 205684 573839 834945 411782 780269 161774 355807 250602 238580 637995 637956 642036 968597 489363 953260 910383 776319 17240 566176 672394 704016 928455 666896 557124
  125. 9 3
  126. 100 200 300 400 500 600 700 800 900
  127. 5 4
  128. 100 100 100 100 100
  129. 4 2
  130. 1 2 4 8
  131. 6 2
  132. 1 2 4 8 4 2
  133. 6 2
  134. 1 2 3 3 2 1
  135. 2 2
  136. 10000000 10000000
  137. 6 3
  138. 1000000 1 1 1 1 1
  139. 6 6
  140. 6 5 4 3 2 1
  141.  
  142. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement