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 }

codeforce C. Maximal Intersection的更多相关文章

  1. Codeforces Round #506 (Div. 3) C. Maximal Intersection

    C. Maximal Intersection time limit per test 3 seconds memory limit per test 256 megabytes input stan ...

  2. CF1029C Maximal Intersection 暴力枚举

    Maximal Intersection time limit per test 3 seconds memory limit per test 256 megabytes input standar ...

  3. CodeForces C. Maximal Intersection

    http://codeforces.com/contest/1029/problem/C You are given nn segments on a number line; each endpoi ...

  4. F - Maximal Intersection --------暴力求解题

    You are given n segments on a number line; each endpoint of every segment has integer coordinates. S ...

  5. C. Maximal Intersection(STL)

    这道题,关键在于怎么求多个区间的交集,使用multiset就可以 分别将 r , l 存在不同的mutiset中. 然后,我们来看一下 是不是 交集的 l 是最大的, 交集的 r 是最小的 #incl ...

  6. Codeforces | CF1029C 【Maximal Intersection】

    论Div3出这样巨水的送分题竟然还没多少人AC(虽说当时我也没A...其实我A了D...逃) 这个题其实一点都不麻烦,排序都可以免掉(如果用\(priority \_ queue\)的话) 先考虑不删 ...

  7. CF C. Maximal Intersection(贪心 || STL)

    题意 给你N个线段(一条直线上),问删去一个之后,最长公共长度 : 分析:首先我们得先知道n条线段公共的线段一定是(LMAX,RMIN) ,那我们可以先排序,然后枚举删除边: #include< ...

  8. CF1029C Maximal Intersection

    https://www.luogu.org/problem/show?pid=CF1029C #include<bits/stdc++.h> using namespace std ; # ...

  9. Codeforces Round #506 (Div. 3) 题解

    Codeforces Round #506 (Div. 3) 题目总链接:https://codeforces.com/contest/1029 A. Many Equal Substrings 题意 ...

  10. cf 1029 C

    C. Maximal Intersection time limit per test 3 seconds memory limit per test 256 megabytes input stan ...

随机推荐

  1. 关于Git在Visual studio及Git Bush中的日常操作教程,有图有说明,会一直更新本页内容... (Git相对SVN具有更加安全的分布式存储, 分支版本之间切换秒级速度, 分支版本强大灵活等特点)

    >安装命令行和TortoiseGit UI程序. <git bash的安装> https://git-scm.com/downloads <windows可视化工具 Torto ...

  2. NOIP2022 退役记

    无所谓,我还能卡队线. 无所谓,我还能被卡校线.

  3. mysql 复制数据

    1.表存在 insert into table_name(key1,key2) select key3,key4 from table_name_2; 2.表不存在 create table test ...

  4. P5934 [清华集训2012]最小生成树

    简要题意 给你一个 \(N\) 个点,\(M\) 条边的 无向连通 带权图.给定一条边 \((u,v,L)\),请问需要在原图中删除多少条边,使得将 \((u,v,L)\) 插入图后,它既可能在最小生 ...

  5. 【Basic Knowledge】Self-Attention Generative Adversarial Networks

    Note   这是一篇将Self-Attention应用到GAN中的paper,Self-Attention模块是卷积模块的补充,能够有助于建模跨图像区域的长范围.多层次依赖关系.文中主要提到4点: ...

  6. Educational Codeforces Round 142 (Rated for Div. 2) A-D

    比赛链接 A 题解 知识点:贪心. 代码 #include <bits/stdc++.h> using namespace std; using ll = long long; bool ...

  7. python3异常打印堆栈信息

    import traceback try: a=1/0 except: print(traceback.format_exc())

  8. 通过pdf模板,填充内容,生成pdf文件---JAVA

    1 概述 我们通常会遇到需要生成某些固定格式,但是内容不同的文件,那么我们就可以使用⽤Adobe Acrobat DC来创建pdf模块,然后通过代码对模板进行填充,生成pdf文件 2 创建一个pdf模 ...

  9. 二分查找 & 移除元素

    一.二分查找 704.二分查找 leetcode链接 1.二分查找方法概述 二分查找是针对有序数组的一种查找方式.是利用(letf+right)/2 = mid的方式来对半缩短搜索范围的一种方法,一次 ...

  10. Windows服务安装小工具

    主要为了方便Windows服务的安装卸载,不需要使用CMD命令. 先给大家小工具的效果图: 使用此工具需要注意一下几点: 1.服务程序的.NET Framework版本: 2.服务名称与服务执行程序名 ...