Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2020
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.65 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Comparator;
  3. import java.util.List;
  4. import java.util.Scanner;
  5. import java.util.stream.Collectors;
  6.  
  7. class ScheduleEntry implements Comparable<ScheduleEntry> {
  8.     private int id;
  9.     private int start;
  10.     private int end;
  11.     private String name;
  12.  
  13.     public ScheduleEntry(int id, int start, int end) {
  14.         this.id = id;
  15.         this.start = start;
  16.         this.end = end;
  17.     }
  18.  
  19.     public int getId() {
  20.         return id;
  21.     }
  22.  
  23.     public void setId(int id) {
  24.         this.id = id;
  25.     }
  26.  
  27.     public int getStart() {
  28.         return start;
  29.     }
  30.  
  31.     public String getName() {
  32.         return name;
  33.     }
  34.  
  35.     public void setName(String name) {
  36.         this.name = name;
  37.     }
  38.  
  39.     public void setStart(int start) {
  40.         this.start = start;
  41.     }
  42.  
  43.     public int getEnd() {
  44.         return end;
  45.     }
  46.  
  47.     public void setEnd(int end) {
  48.         this.end = end;
  49.     }
  50.  
  51.     @Override
  52.     public int compareTo(ScheduleEntry o) {
  53.         return Integer.compare(getStart(), o.getStart());
  54.     }
  55. }
  56.  
  57. public class Solution {
  58.     public static void main(String[] args) {
  59.         Scanner input = new Scanner(System.in);
  60.  
  61.         int t = input.nextInt();
  62.         String[] result = new String[t];
  63.  
  64.         for (int i = 0; i < t; i++) {
  65.             int n = input.nextInt();
  66.             List<ScheduleEntry> entries = new ArrayList<>();
  67.  
  68.             for (int j = 0; j < n; j++) {
  69.                 entries.add(new ScheduleEntry(j + 1, input.nextInt(), input.nextInt()));
  70.             }
  71.  
  72.             entries = entries.stream().sorted().collect(Collectors.toList());
  73.  
  74.             ScheduleEntry cSchedule = null;
  75.             ScheduleEntry jSchedule = null;
  76.             boolean impossible = false;
  77.  
  78.             List<ScheduleEntry> scheduleEntryList = new ArrayList<>();
  79.             for (int j = 0; j < n; j++) {
  80.                 if (cSchedule == null) {
  81.                     cSchedule = entries.get(j);
  82.                     scheduleEntryList.add(entries.get(j));
  83.                     scheduleEntryList.get(j).setName("C");
  84.                 } else if (jSchedule == null) {
  85.                     jSchedule = entries.get(j);
  86.                     scheduleEntryList.add(entries.get(j));
  87.                     scheduleEntryList.get(j).setName("J");
  88.                 } else {
  89.                     if (cSchedule.getEnd() <= entries.get(j).getStart()) {
  90.                         cSchedule = entries.get(j);
  91.                         scheduleEntryList.add(entries.get(j));
  92.                         scheduleEntryList.get(j).setName("C");
  93.                     } else if (jSchedule.getEnd() <= entries.get(j).getStart()) {
  94.                         jSchedule = entries.get(j);
  95.                         scheduleEntryList.add(entries.get(j));
  96.                         scheduleEntryList.get(j).setName("J");
  97.                     } else {
  98.                         impossible = true;
  99.                         break;
  100.                     }
  101.                 }
  102.             }
  103.  
  104.             if (!impossible) {
  105.                 scheduleEntryList = scheduleEntryList.stream().sorted(Comparator.comparingInt(ScheduleEntry::getId)).collect(Collectors.toList());
  106.                 StringBuilder sb = new StringBuilder();
  107.                 for (int j = 0; j < scheduleEntryList.size(); j++) {
  108.                     sb.append(scheduleEntryList.get(j).getName());
  109.                 }
  110.                 result[i] = sb.toString();
  111.             } else {
  112.                 result[i] = "IMPOSSIBLE";
  113.             }
  114.         }
  115.  
  116.         for (int i = 0; i < t; i++) {
  117.             System.out.println(String.format("Case #%d: %s", i+1, result[i]));
  118.         }
  119.  
  120.  
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement