codeforce C. Maximal Intersection

 

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

 

从第一天吃晚饭做到第二天吃完饭……你无法想象我的代码曾经150行 o( ̄┰ ̄*)ゞ

 

找到所有线段最远的左边和最近的右边,当一个线段的左边或右边与上述重合就尝试删除。

 

 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 }

 

posted @ 2019-10-21 18:38  dodoBehind  阅读(124)  评论(0编辑  收藏  举报