Source code for submission s1028

Main.java

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package fence;
  7.  
  8. import java.io.BufferedReader;
  9. import java.io.IOException;
  10. import java.io.InputStreamReader;
  11. import java.util.ArrayList;
  12. import java.util.Arrays;
  13.  
  14. /**
  15.  *
  16.  * @author cteam028
  17.  */
  18. public class Main {
  19.  
  20. /**
  21.   * @param args the command line arguments
  22.   */
  23. public static void main(String[] args) throws IOException {
  24. Main m = new Main();
  25. m.init();
  26. }
  27.  
  28. public void init() throws IOException{
  29. String line;
  30. while((line = br.readLine()) != null) {
  31. int N = Integer.valueOf(line);
  32.  
  33. Point[] pole = new Main.Point[N];
  34.  
  35. double tx = 0;
  36. double ty = 0;
  37.  
  38. for (int i = 0; i < N; i++) {
  39. line = br.readLine();
  40. String a[] = line.split(" ");
  41.  
  42. pole[i] = new Point();
  43. Point p = pole[i];
  44.  
  45. p.x = Integer.valueOf(a[0]);
  46. p.y = Integer.valueOf(a[1]);
  47.  
  48. tx += p.x;
  49. ty += p.y;
  50. }
  51.  
  52. tx /= N;
  53. ty /= N;
  54.  
  55. for (int i = 0; i < N; i++) {
  56. Point p = pole[i];
  57.  
  58. p.angle = Math.atan2(p.y-ty, p.x-tx);
  59. p.lensq = (tx - p.x)*(tx - p.x) + (ty - p.y)*(ty - p.y);
  60. }
  61.  
  62. Arrays.sort(pole);
  63.  
  64.  
  65. double maxlen = 0;
  66. int maxidx = 0;
  67. for (int i = 0; i < N; i++) {
  68. Point p = pole[i];
  69.  
  70. if(p.lensq > maxlen) {
  71. maxlen = p.lensq;
  72. maxidx = i;
  73. }
  74. }
  75.  
  76. pole[maxidx].out = true;
  77. ArrayList<Point> array = new ArrayList<Main.Point>();
  78. array.add(pole[maxidx]);
  79.  
  80. for (int i = 1; i < N; i++) {
  81. Point left = pole[(maxidx + i - 1) % N];
  82. Point p = pole[(maxidx + i) % N];
  83. Point right = pole[(maxidx + i + 1) % N];
  84.  
  85. int dx = right.x - left.x;
  86. int dy = right.y - left.y;
  87.  
  88. int nx = dy;
  89. int ny = -dx;
  90.  
  91. if((((tx - left.x)*nx + (ty - left.y)*ny) > 0 ) != (((p.x - left.x)*nx + (p.y - left.y)*ny) > 0)
  92. || (Math.abs((tx - left.x)*nx + (ty - left.y)*ny) < Math.abs((p.x - left.x)*nx + (p.y - left.y)*ny) )
  93. ){
  94. p.out = true;
  95. array.add(p);
  96. }
  97. }
  98.  
  99. double lenght = Double.MAX_VALUE;
  100.  
  101. Point last = array.get(array.size() - 1);
  102. for (Main.Point point : array) {
  103. double dirx = point.x - last.x;
  104. double diry = point.y - last.y;
  105. double dirlen = Math.sqrt(dirx * dirx + diry*diry);
  106. dirx /= dirlen;
  107. diry /= dirlen;
  108. //System.out.println("dirx " + dirx);
  109. //System.out.println("diry " + dirx);
  110.  
  111. double norx = diry;
  112. double nory = -dirx;
  113.  
  114. double maxdir = Double.MIN_VALUE;
  115. double maxnor = Double.MIN_VALUE;
  116. double mindir = Double.MAX_VALUE;
  117. double minnor = Double.MAX_VALUE;
  118.  
  119. for (Point p : array) {
  120. double pn = (p.x - last.x)*norx + (p.y - last.y)*nory;
  121. double pd = (p.x - last.x)*dirx + (p.y - last.y)*diry;
  122. maxdir = Math.max(maxdir, pd);
  123. mindir = Math.min(mindir, pd);
  124. maxnor = Math.max(maxnor, pn);
  125. minnor = Math.min(minnor, pn);
  126. }
  127.  
  128. lenght = Math.min(lenght, (maxdir - mindir)*2 + (maxnor - minnor)*2);
  129. last = point;
  130. }
  131.  
  132. System.out.println(lenght);
  133. }
  134. }
  135.  
  136. public class Point implements Comparable<Point>{
  137. int x, y;
  138. double angle;
  139. double lensq;
  140. boolean out = false;
  141.  
  142. public int compareTo(Main.Point arg0) {
  143. return (angle < arg0.angle)?-1:1;
  144. }
  145.  
  146. }
  147.  
  148. }
  149.