codeforce C. Maximal Intersection

 

http://codeforces.com/contest/1029/problem/C

 

From the first day to do the next day for dinner meal ...... You can not imagine my code had 150 lines o (¯┰¯ *) ゞ

 

Find the line furthest to the left and the right of all recently, when the left or right of a line segment coincides with an attempt to delete the above.

 

 1 import java.util.Arrays;
 2 import java.util.Scanner;
 3 
 4 public class A {
 5     public static void main(String[] args) {
 6         Scanner io = new Scanner(System.in);
 7         int n = io.nextInt();
 8         if (n == 1) {
 9             System.out.println(-(io.nextInt() - io.nextInt()));
10             return;
11         }
12         int[] a = new int[n];
13         int[] b = new int[n];
14         int[] A = new int[n];
15         int[] B = new int[n];
16         int[] minLeft = new int[n];
17         int[] minRight = new int[n];
18         int[] maxLeft = new int[n];
19         int[] maxRight = new int[n];
20         
21         for (int i = 0; i < n; i++) {
22             A[i] = a[i] = io.nextInt();
23             B[i] = b[i] = io.nextInt();
24             if (i != 0) {
25                 maxLeft[i] = Math.max(maxLeft[i - 1], a[i]);
26                 minLeft[i] = Math.min(minLeft[i - 1], b[i]);
27             } else {
28                 maxLeft[0] = a[0];
29                 minLeft[0] = b[0];
30             }
31         }
32         for (int i = n - 1; i >= 0; i--) {
33             if (i != n - 1) {
34                 maxRight[i] = Math.max(maxRight[i + 1], a[i]);
35                 minRight[i] = Math.min(minRight[i + 1], b[i]);
36             } else {
37                 maxRight[n - 1] = a[n - 1];
38                 minRight[n - 1] = b[n - 1];
39             }
40         }
41         Arrays.sort(A);
42         Arrays.sort(B);
43 
44         int len = 0,min,max;
45         for (int i = 0; i < n; i++) {
46             if (a[i] == A[n - 1] || b[i] == B[0]) {
47                 if (i == 0) {
48                     min = minRight[1];
49                     max = maxRight[1];
50                 } else if (i == n - 1) {
51                     min = minLeft[n - 2];
52                     max = maxLeft[n - 2];
53                 } else {
54                     min = Math.min(minLeft[i - 1], minRight[i + 1]);
55                     max = Math.max(maxLeft[i - 1], maxRight[i + 1]);
56                 }
57                 len = Math.max(min - max, len);
58             }
59         }
60         System.out.println(len);
61     }
62 }

 

Guess you like

Origin www.cnblogs.com/towerbird/p/11715215.html