Advertisement
Guest User

Untitled

a guest
Nov 25th, 2014
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.04 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.ArrayList;
  7. import java.util.Arrays;
  8. import java.util.Comparator;
  9. import java.util.LinkedList;
  10. import java.util.Random;
  11. import java.util.StringTokenizer;
  12.  
  13. public class Main
  14. {
  15.  
  16.     public static void main(String[] args) throws IOException
  17.     {
  18.         Main main = new Main();
  19.         main.read();
  20.  
  21.     }
  22.  
  23.     private BufferedReader in;
  24.     private StringBuilder ans;
  25.     private ArrayList<Img> originalImages;
  26.     private Img min;
  27.  
  28.     private void read() throws IOException
  29.  
  30.     {
  31.         // streams
  32.         boolean file = false;
  33.         if (file)
  34.             in = new BufferedReader(new FileReader("input.txt"));
  35.         else
  36.             in = new BufferedReader(new InputStreamReader(System.in));
  37.         ans = new StringBuilder();
  38.         StringTokenizer tok;
  39.  
  40.         // read cases
  41.         int nCases = Integer.parseInt(in.readLine());
  42.         for (int cas = 1; cas <= nCases; cas++)
  43.         {
  44.             // read original images
  45.             originalImages = new ArrayList<Img>();
  46.             int nOriginalImages = Integer.parseInt(in.readLine());
  47.             for (int i = 0; i < nOriginalImages; i++)
  48.             {
  49.                 tok = new StringTokenizer(in.readLine());
  50.                 int w = Integer.parseInt(tok.nextToken());
  51.                 int h = Integer.parseInt(tok.nextToken());
  52.                 addImage(w, h);
  53.             }
  54.  
  55.             // output
  56.             ans.append("Case " + cas + ":\n");
  57.             int requiredImages = Integer.parseInt(in.readLine());
  58.             for (int i = 0; i < requiredImages; i++)
  59.             {
  60.                 tok = new StringTokenizer(in.readLine());
  61.                 int w = Integer.parseInt(tok.nextToken());
  62.                 int h = Integer.parseInt(tok.nextToken());
  63.                 ans.append(solve(w, h) + "\n");
  64.             }
  65.         }
  66.  
  67.         // output
  68.         System.out.print(ans.toString());
  69.     }
  70.  
  71.     private String solve(int w, int h)
  72.     {
  73.         // check all given images and all possible rotations
  74.         min = new Img(20000, 20000, 3);
  75.         for (Img img : originalImages)
  76.         {
  77.             // same
  78.             if (img.w == w && img.h == h)
  79.                 minimize(img.w, img.h, 0);
  80.  
  81.             // rotated
  82.             if (img.w == h && img.h == w)
  83.                 minimize(img.w, img.h, 1);
  84.  
  85.             // cropped
  86.             if (img.w >= w && img.h >= h)
  87.                 minimize(img.w, img.h, 1);
  88.  
  89.             // cropped and rotated
  90.             if (img.h >= w && img.w >= h)
  91.                 minimize(img.w, img.h, 2);
  92.         }
  93.  
  94.         return min.w + " " + min.h + " " + min.k;
  95.     }
  96.  
  97.     private void minimize(int w, int h, int k)
  98.     {
  99.         if (w*h != min.w * min.h)
  100.         {
  101.             if (w * h <= min.w * min.h)
  102.                 min = new Img(w, h, k);
  103.         }
  104.         else if (w != min.w)
  105.         {
  106.             if (w  <= min.w )
  107.                 min = new Img(w, h, k);
  108.         }
  109.         else if (k !=min.k)
  110.         {
  111.             if (k <= min.k)
  112.                 min = new Img(w, h, k);
  113.         }
  114.     }
  115.  
  116.     private void addImage(int w, int h)
  117.     {
  118.         // add all scaled image
  119.         int newW = w;
  120.         int newH = h;
  121.         while (true)
  122.         {
  123.             originalImages.add(new Img(newW, newH, 0));
  124.             if (Math.min(newW, newH) > 100)
  125.                 break;
  126.             newW += w;
  127.             newH += h;
  128.         }
  129.  
  130.     }
  131.  
  132. }
  133.  
  134. class Img
  135. {
  136.     int w;
  137.     int h;
  138.     int k;
  139.  
  140.     public Img(int w, int h, int k)
  141.     {
  142.         this.w = w;
  143.         this.h = h;
  144.         this.k = k;
  145.     }
  146.  
  147.     public String toString()
  148.     {
  149.         return w + ":" + h;
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement