Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2020
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.44 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4. import java.util.stream.Collectors;
  5.  
  6. class ScheduleEntry implements Comparable<ScheduleEntry> {
  7.     private int start;
  8.     private int end;
  9.  
  10.     public ScheduleEntry(int start, int end) {
  11.         this.start = start;
  12.         this.end = end;
  13.     }
  14.  
  15.     public int getStart() {
  16.         return start;
  17.     }
  18.  
  19.     public void setStart(int start) {
  20.         this.start = start;
  21.     }
  22.  
  23.     public int getEnd() {
  24.         return end;
  25.     }
  26.  
  27.     public void setEnd(int end) {
  28.         this.end = end;
  29.     }
  30.  
  31.     @Override
  32.     public int compareTo(ScheduleEntry o) {
  33.         return Integer.compare(getStart(), o.getStart());
  34.     }
  35. }
  36.  
  37. public class Task3 {
  38.     public static void main(String[] args) {
  39.         Scanner input = new Scanner(System.in);
  40.  
  41.         int t = input.nextInt();
  42.         String[] result = new String[t];
  43.  
  44.         for (int i = 0; i < t; i++) {
  45.             int n = input.nextInt();
  46.             List<ScheduleEntry> entries = new ArrayList<>();
  47.  
  48.             for (int j = 0; j < n; j++) {
  49.                 entries.add(new ScheduleEntry(input.nextInt(), input.nextInt()));
  50.             }
  51.  
  52.             entries = entries.stream().sorted().collect(Collectors.toList());
  53.  
  54.             ScheduleEntry cSchedule = null;
  55.             ScheduleEntry jSchedule = null;
  56.             StringBuilder sb = new StringBuilder();
  57.             for (int j = 0; j < n; j++) {
  58.                 if (cSchedule == null) {
  59.                     cSchedule = entries.get(j);
  60.                     sb.append("C");
  61.                 } else if (jSchedule == null) {
  62.                     jSchedule = entries.get(j);
  63.                     sb.append("J");
  64.                 } else {
  65.                     if (cSchedule.getEnd() <= entries.get(j).getStart()) {
  66.                         cSchedule = entries.get(j);
  67.                         sb.append("C");
  68.                     } else if (jSchedule.getEnd() <= entries.get(j).getStart()) {
  69.                         jSchedule = entries.get(j);
  70.                         sb.append("J");
  71.                     } else {
  72.                         sb = new StringBuilder("IMPOSSIBLE");
  73.                         break;
  74.                     }
  75.                 }
  76.             }
  77.  
  78.             result[i] = sb.toString();
  79.         }
  80.  
  81.         for (int i = 0; i < t; i++) {
  82.             System.out.println(String.format("Case #%d: %s", i+1, result[i]));
  83.         }
  84.  
  85.  
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement