Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.90 KB | None | 0 0
  1.     public static void main(String[] args) throws Exception
  2.     {
  3.         String fileOut = file + "out";
  4.         PrintWriter w = new PrintWriter(new FileWriter(fileOut));
  5.         int _k = Integer.valueOf("1");
  6.         if(test > 0) in = new BufferedReader(new InputStreamReader(System.in));
  7.         else in = new BufferedReader(new FileReader(file));
  8.         if(test < 0) {String[] str = in.readLine().split(" ");}
  9. /***********************************************************************/
  10. /***********************************************************************/
  11. /***********************************************************************/
  12. /***********************************************************************/
  13.         // System.out.println((-100 + 0) / 2);
  14.  
  15.  
  16.         int[] ary = toIntArray();
  17.        
  18.         int B = ary[0];
  19.         int L = ary[1];
  20.         int D = ary[2];
  21.  
  22.         int[] books = toIntArray();
  23.        
  24.         int[] numberOfBooksInLibrary = new int[L];
  25.         int[] numberOfDaysToSignup = new int[L];
  26.         int[] speedOfBooksScanned = new int[L];
  27.         List<List<Integer>> booksInLibrary = new ArrayList<>();
  28.  
  29.  
  30.         for (int i = 0; i < L; i++) {
  31.             ary = toIntArray();
  32.            
  33.             numberOfBooksInLibrary[i] = ary[0];
  34.             numberOfDaysToSignup[i] = ary[1];
  35.             speedOfBooksScanned[i] = ary[2];
  36.             booksInLibrary.add(new ArrayList<>());
  37.             ary = toIntArray();
  38.            
  39.             for (int j = 0; j < numberOfBooksInLibrary[i]; j++) {
  40.                 booksInLibrary.get(i).add(ary[j]);
  41.             }
  42.         }
  43.  
  44.         PriorityQueue<Integer> pq = new PriorityQueue<>(new Comparator<Integer>() {
  45.             @Override
  46.             public int compare(Integer o1, Integer o2) {
  47.                 return numberOfDaysToSignup[o1] - numberOfDaysToSignup[o2];
  48.             }
  49.         });
  50.  
  51.         int l = L;
  52.         for (int i = 0; i < L; i++)
  53.         {
  54.             pq.offer(i);
  55.         }
  56.  
  57.         // System.out.println(pq);
  58.        
  59.         for (List<Integer> list : booksInLibrary) {
  60.             Collections.sort(list, new Comparator<Integer>() {
  61.                 @Override
  62.                 public int compare(Integer o1, Integer o2) {
  63.                     return books[o2] - books[o1];
  64.                 }
  65.             });
  66.         }
  67.  
  68.        
  69.         //record books in the lib scan order
  70.         List<Integer>[] res = new ArrayList[l];
  71.         //current index
  72.         int[] index = new int[l];
  73.        
  74.         //the library is being processed
  75.         List<Integer> libQueue = new ArrayList<>();
  76.  
  77.         for (int i = 0; i < l; i++) {
  78.             res[i] = new ArrayList<>();
  79.         }
  80.  
  81.  
  82.         int lib = pq.poll();
  83.         int nextLibTime = numberOfDaysToSignup[lib] - 1;
  84.  
  85.         Set<Integer> scannedBooks = new HashSet<>();
  86.  
  87.         int totalLibraries = 0;
  88.  
  89.         // System.out.println(booksInLibrary);
  90.  
  91.         for(int i = 0; i < D; i++)
  92.         {
  93.  
  94.             if(nextLibTime == 0)
  95.             {
  96.                 if(pq.isEmpty()) break;
  97.                 totalLibraries++;
  98.                 // System.out.println(pq);
  99.                 int nextLib = pq.poll();
  100.                 libQueue.add(lib);
  101.  
  102.                 for(int nextBook : booksInLibrary.get(lib))
  103.                 {
  104.                     if(scannedBooks.contains(nextBook)) continue;
  105.                     scannedBooks.add(nextBook);
  106.                     res[lib].add(nextBook);
  107.                 }
  108.                 lib = nextLib;
  109.                 nextLibTime = numberOfDaysToSignup[lib] - 1;
  110.             }
  111.             else nextLibTime--;
  112.  
  113.         }
  114.  
  115.  
  116.         w.println(totalLibraries);
  117.  
  118.         for(int i : libQueue)
  119.         {
  120.             w.printf("%d %d\n", i, res[i].size());
  121.             for (int book : res[i])
  122.             {
  123.                 w.printf("%d ", book);
  124.             }
  125.             w.printf("\n");
  126.         }
  127.  
  128.         w.flush();
  129.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement