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 ...
随机推荐
- Python 文件操作(IO 技术)
目录 Python 文件操作(IO 技术) 文本文件和二进制文件 文件操作相关模块概述 建文件对象 open() 文本文件的写入 write()/writelines()写入数据 close()关闭文 ...
- [OpenCV实战]48 基于OpenCV实现图像质量评价
本文主要介绍基于OpenCV contrib中的quality模块实现图像质量评价.图像质量评估Image Quality Analysis简称IQA,主要通过数学度量方法来评价图像质量的好坏. 本文 ...
- SICP:符号求导、集合表示和Huffman树(Python实现)
绪论 到目前为止,我们已经使用过的所有复合数据,最终都是从数值出发构造起来的(比如我们在上一篇博客<SICP 2.2: 层次性数据和闭包性质(Python实现)>所介绍的链表和树就基于数来 ...
- Linux利用crontab执行定时任务
Linux利用crontab执行定时任务 crond简介 crond是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程,与windows下的计划任务类似,当安装完成操作系统后,默认 ...
- Redis 数据结构-简单动态字符串
Redis 数据结构-简单动态字符串 无边落木萧萧下,不尽长江滚滚来. 1.简介 Redis 之所以快主要得益于它的数据结构.操作内存数据库.单线程和多路 I/O 复用模型,进一步窥探下它常见的五种基 ...
- centos7.6安装本地yum源
centos7.6安装本地yum源 前言:文章内容可能会因环境不同而有所差异,所谓集思广益说不定灵感就来了呢; 文章初衷旨在交流学习.记录个人成长,如果能帮助到您,那就点个赞噢. 环境说明: 1.本实 ...
- 异步async await 相关知识点总结以及代码练习
<script> const setTimeoutToPromise = duration => new Promise(resolve => { setTimeout(res ...
- angular配置多个系统 配置动态路由,缩短模块初次加载时间,快速打开界面,优化用户访问体验
1.配置一个文件,返回系统名称 2.配置routes-routing.module.ts 引入文件 const system = 服务.getsystem() const allROUTES: {UR ...
- Postgresql 使用Vscode开发指南
Postgresql 使用Vscode开发指南 depends libraries sudo apt install -y libsystemd-dev libxml2-dev libssl-dev ...
- XAMPP-Apache-解决跨域
很 意外啊,我这篇文章竟然说因为有广告,所以审核不通过.牛逼,我接着发,我看哪来的广告.审核有广告是NT技术写的逻辑,还是没脑子的审核员审核的. 因为在做项目的时候,会有很多项目要进行热更测试,可有些 ...