package Qualification2019; import java.io.*; import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; /** * Created by ooptG15 on 2019-02-28 at 09:25. */ public class SolutionQ2019 { static ArrayList verticals = new ArrayList<>(); static ArrayList horizontals = new ArrayList<>(); static ArrayList slides = new ArrayList<>(); public static void main(String[] args) throws IOException { readFile(); // makeSlides(); makeSimple(); writeSolution(); } private static void readFile() throws FileNotFoundException { String filename = ""; Scanner scanner = new Scanner(System.in); System.out.println("Which file? (0-4)"); int in = Integer.parseInt(scanner.next()); if (in == 0) { filename = "a_example"; } else if (in == 1) { filename = "b_lovely_landscapes"; } else if (in == 2) { filename = "c_memorable_moments"; } else if (in == 3) { filename = "d_pet_pictures"; } else if (in == 4) { filename = "e_shiny_selfies"; } System.out.println(filename + " selected."); FileReader fileReader = new FileReader( "Qualification2019" + "/input_qualification_round_2019/" + filename + ".txt"); Scanner fileScanner = new Scanner(fileReader); int numPics = fileScanner.nextInt(); for (int i = 0; i < numPics; i++) { String o = fileScanner.next(); ArrayList l = new ArrayList<>(); int numTags = fileScanner.nextInt(); for (int j = 0; j < numTags; j++) { l.add(fileScanner.next()); } if (o.equals("V")) { verticals.add(new Photo(true, l, i)); } else { horizontals.add(new Photo(false, l, i)); } } } static void writeSolution() throws IOException { File file = new File("Qualification2019" + "/output_qualification_round_2019/submission.txt"); FileWriter fileWriter = new FileWriter(file); fileWriter.write(slides.size() + "\n"); for (Photo p : slides) { System.out.println(p.id + " " + p.id2); if (p.id2 == -1) { fileWriter.write(p.id + "\n"); } else { fileWriter.write(p.id + " " + p.id2 + "\n"); } } fileWriter.close(); } static void makeSimple() { for (int i = 0; i < verticals.size() - 1; i++) { if (i % 2 == 0) { System.out .println(verticals.get(i) + " " + verticals.get(i + 1)); horizontals.add( new Photo(verticals.get(i), verticals.get(i + 1))); } } slides.add(horizontals.get(0)); horizontals.remove(0); boolean flag = true; while (horizontals.size() > 0 && flag) { flag = false; for (int i = 0; i < horizontals.size(); i++) { if (slides.get(slides.size() - 1) .commonality(horizontals.get(i))) { slides.add(horizontals.get(i)); horizontals.remove(horizontals.get(i)); flag = true; } } } for (int i = 0; i < slides.size(); i++) { if (horizontals.size() > 0) { if (horizontals.get(0).commonality(slides.get(i)) && horizontals.get(0).commonality(slides.get(i + 1))) { slides.add(i + 1, horizontals.get(0)); horizontals.remove(0); } } } } static void makeSlides() { int j = 0; slides.add(horizontals.get(0)); horizontals.remove(0); for (Photo p : horizontals) { int before = 0; int best = 0, bestval = -1; int i = 0; Photo old = new Photo(true, new ArrayList<>(), -1); for (Photo s : slides) { if (bestval < (old.tags.size() + s.tags.size()) / 2) { int current = p.calculateScore(s); if (before + current - s.calculateScore(old) > bestval) { best = i; bestval = before + current; if (bestval >= p.tags.size() / 20) { j++; break; } } before = current; old = s; i++; } else { System.out.println("no wasted time"); } } if (before > bestval) { best = i; bestval = before; } slides.add(best, p); } System.out.println("time saved " + j); System.out.println(slides); } } class Photo { // V = true, H = false boolean Orientation; ArrayList tags; int id; int id2 = -1; Photo(boolean orientation, ArrayList tags, int id) { Orientation = orientation; this.tags = tags; Collections.sort(tags); this.id = id; } Photo(Photo v1, Photo v2) { Orientation = false; this.tags = v1.tags; this.tags.addAll(v2.tags); Collections.sort(tags); this.id = v1.id; this.id2 = v2.id; } int calculateScore(Photo other) { int equalTags = 0; for (String t : tags) { int i = Collections.binarySearch(other.tags, t); if (i > 0) { equalTags++; } } return Math.min(Math.min(equalTags, this.tags.size() - equalTags), other.tags.size() - equalTags); } boolean commonality(Photo other) { for (String s : tags) { int i = Collections.binarySearch(other.tags, s); if (i >= 0) { break; } return true; } return false; } @Override public String toString() { return "Photo{" + "Orientation='" + Orientation + '\'' + ", tags=" + tags + '}'; } } class Slides { } class C3 { } class C4 { }