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 }
codeforce C. Maximal Intersection的更多相关文章
- 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 ...
- CF1029C Maximal Intersection 暴力枚举
Maximal Intersection time limit per test 3 seconds memory limit per test 256 megabytes input standar ...
- CodeForces C. Maximal Intersection
http://codeforces.com/contest/1029/problem/C You are given nn segments on a number line; each endpoi ...
- F - Maximal Intersection --------暴力求解题
You are given n segments on a number line; each endpoint of every segment has integer coordinates. S ...
- C. Maximal Intersection(STL)
这道题,关键在于怎么求多个区间的交集,使用multiset就可以 分别将 r , l 存在不同的mutiset中. 然后,我们来看一下 是不是 交集的 l 是最大的, 交集的 r 是最小的 #incl ...
- Codeforces | CF1029C 【Maximal Intersection】
论Div3出这样巨水的送分题竟然还没多少人AC(虽说当时我也没A...其实我A了D...逃) 这个题其实一点都不麻烦,排序都可以免掉(如果用\(priority \_ queue\)的话) 先考虑不删 ...
- CF C. Maximal Intersection(贪心 || STL)
题意 给你N个线段(一条直线上),问删去一个之后,最长公共长度 : 分析:首先我们得先知道n条线段公共的线段一定是(LMAX,RMIN) ,那我们可以先排序,然后枚举删除边: #include< ...
- CF1029C Maximal Intersection
https://www.luogu.org/problem/show?pid=CF1029C #include<bits/stdc++.h> using namespace std ; # ...
- Codeforces Round #506 (Div. 3) 题解
Codeforces Round #506 (Div. 3) 题目总链接:https://codeforces.com/contest/1029 A. Many Equal Substrings 题意 ...
- cf 1029 C
C. Maximal Intersection time limit per test 3 seconds memory limit per test 256 megabytes input stan ...
随机推荐
- JavaScript 深拷贝的循环引用问题
如果说道实现深拷贝最简单的方法,我们第一个想到的就是 JSON.stringify() 方法,因为JSON.stringify()后返回的是字符串,所以我们会再使用JSON.parse()转换为对象, ...
- RedisTemplate设置redis的key时出现\xac\xed\x00\x05t\x00\x0f前缀
1.问题描述 使用redisTemplate设置redis的key-value,程序运行没有问题,但是却在redis客户端查不到设置的key-value. 2.产生原因 出现这种乱码前缀的原因是没有进 ...
- Kaliの一些网络操作
KAlIの一些网络操作 arping -c 192.168.10.1 缺点是只能对单一ip进行探测,可利用shell脚本进行网段探测扫描 netdiscover -i eth0 -r 192.168. ...
- Redis 数据结构-双向链表
Redis 数据结构-双向链表 最是人间留不住,朱颜辞镜花辞树. 1.简介 Redis 之所以快主要得益于它的数据结构.操作内存数据库.单线程和多路 I/O 复用模型,进一步窥探下它常见的五种基本数据 ...
- .NetCore下基于FreeRedis实现的Redis6.0客户端缓存之缓存键条件优雅过滤
前言 众所周知内存缓存(MemoryCache)数据是从内存中获取,性能表现上是最优的,但是内存缓存有一个缺点就是不支持分布式,数据在各个部署节点上各存一份,每份缓存的过期时间不一致,会导致幻读等各种 ...
- 请求量突增一下,系统有效QPS为何下降很多?
原创:扣钉日记(微信公众号ID:codelogs),欢迎分享,转载请保留出处. 简介 最近我观察到一个现象,当服务的请求量突发的增长一下时,服务的有效QPS会下降很多,有时甚至会降到0,这种现象网上也 ...
- Ubuntu下的LGT8F328P MiniEVB Arduino开发和烧录环境
基于 LGT8F328P LQFP32 的 Arduino MiniEVB, 这个板型资料较少, 记录一下开发环境和烧录过程以及当中遇到的问题. 关于 LGT8F328P 芯片参数 8位RISC内核 ...
- 城市路(Dijkstra)
这道题目需要用到 Dijkstra plus 版(堆优化) 模板还是一样就是有几个点值得注意 1.这里用的是优先队列,原版需要搜出最小,并且没用过的点,省时间就剩在这里用小根堆就可以完美解决这个问题. ...
- 高效、优雅的对象copy之MapStruct入门到精通,实战踩坑版
一.前言 大家在开发中,最让人头疼的就是:对象之间的拷贝,前端的VO和数据库的Entity不一致! 性能最好的就是手动set,主要是枯燥且无技术含量,不仅耗费大量时间而且很容易出错: 所以我们要成为优 ...
- JDBC工具类,减少代码冗余好帮手
首先要在scr下创建一个file文件 当然 需要初始的注册驱动和数据库操作都可以实现,才可以用jdbc工具类进行减多少代码冗余~可以看前面一篇的博客,就是写如何连接jdbc哈~代码运行成功的快乐真的好 ...