Advertisement
Guest User

Untitled

a guest
Apr 5th, 2020
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.79 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.HashSet;
  3.  
  4. public class PageLayout {
  5.  
  6.  
  7.     private static ArrayList<HashSet<Integer>> fitting_stories;
  8.     private static int[] areas;
  9.     private static int N;
  10.  
  11.  
  12.     public static void main(String[] args) {
  13.         Kattio kat = new Kattio(System.in);
  14.         while (true) {
  15.             N = kat.getInt();
  16.             if (N == 0)
  17.                 break;
  18.             ArrayList<int[]> stories = new ArrayList<>();
  19.             for (int i = 0; i < N; i++) {
  20.                 int[] line = new int[4];
  21.                 line[0] = kat.getInt();
  22.                 line[1] = kat.getInt();
  23.                 line[2] = kat.getInt();
  24.                 line[3] = kat.getInt();
  25.                 stories.add(line);
  26.             }
  27.             areas = new int[N];
  28.             for (int i = 0; i < N; i++) {
  29.                 areas[i] = story_size(stories.get(i));
  30.             }
  31.             fitting_stories = new ArrayList<>();
  32.             for (int i = 0; i < N; i++) {
  33.                 fitting_stories.add(new HashSet<>());
  34.             }
  35.             for (int i = 0; i < N; i++) {
  36.                 fitting_stories.get(i).add(i);
  37.                 for (int j = i + 1; j < N; j++) {
  38.                     if (fit_together(stories.get(i), stories.get(j))) {
  39.                         fitting_stories.get(i).add(j);
  40.                         fitting_stories.get(j).add(i);
  41.                     }
  42.                 }
  43.             }
  44.             kat.println(opti_page(0, 0));
  45.             kat.flush();
  46.         }
  47.     }
  48.  
  49.  
  50.     static int story_size(int[] story) {
  51.         return story[0] * story[1];
  52.     }
  53.  
  54.  
  55.     static boolean fit_together(int[] story1, int[] story2) {
  56.         int w1 = story1[0];
  57.         int h1 = story1[1];
  58.         int x1 = story1[2];
  59.         int y1 = story1[3];
  60.         int w2 = story2[0];
  61.         int h2 = story2[1];
  62.         int x2 = story2[2];
  63.         int y2 = story2[3];
  64.         if (x1 < x2) {
  65.             if (x1 + w1 > x2) {
  66.                 if (y1 < y2) {
  67.                     if (y1 + h1 > y2) {
  68.                         return false;
  69.                     }
  70.                 } else if (y2 < y1) {
  71.                     if (y2 + h2 > y1) {
  72.                         return false;
  73.                     }
  74.                 } else {
  75.                     return false;
  76.                 }
  77.             }
  78.         } else if (x2 < x1) {
  79.             if (x2 + w2 > x1) {
  80.                 if (y1 < y2) {
  81.                     if (y1 + h1 > y2) {
  82.                         return false;
  83.                     }
  84.                 } else if (y2 < y1) {
  85.                     if (y2 + h2 > y1) {
  86.                         return false;
  87.                     }
  88.                 } else {
  89.                     return false;
  90.                 }
  91.             }
  92.         } else {
  93.             if (y1 < y2) {
  94.                 if (y1 + h1 > y2) {
  95.                     return false;
  96.                 }
  97.             } else if (y2 < y1) {
  98.                 if (y2 + h2 > y1) {
  99.                     return false;
  100.                 }
  101.             } else {
  102.                 return false;
  103.             }
  104.         }
  105.         return true;
  106.     }
  107.  
  108.  
  109.     static int opti_page(int current_story, int current_page) {
  110.         if (current_story == N)
  111.             return 0;
  112.  
  113.         int prev_area = opti_page(current_story + 1, current_page);
  114.         for (int j = 0; j < current_story; j++) {
  115.             if ((current_page >> j) % 2 == 1) {
  116.                 if (!fitting_stories.get(j).contains(current_story)){
  117.                     return prev_area;
  118.                 }
  119.             }
  120.         }
  121.         int new_area = opti_page(current_story + 1, current_page | 1 << current_story) + areas[current_story];
  122.  
  123.         if (new_area > prev_area) {
  124.             return new_area;
  125.         } else {
  126.             return prev_area;
  127.         }
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement