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 ...
随机推荐
- 虚假新闻检测(CADM)《Unsupervised Domain Adaptation for COVID-19 Information Service with Contrastive Adversarial Domain Mixup》
论文信息 论文标题:Unsupervised Domain Adaptation for COVID-19 Information Service with Contrastive Adversari ...
- [OpenCV实战]31 使用OpenCV将一个三角形仿射变换到另一个三角形
目录 1 什么是仿射变换? 2 使用OpenCV进行三角形仿射变换 2.1 定义输入和输出 2.2 计算边界框 2.3 裁剪图像和更改坐标 2.4 计算仿射变换矩形 2.5 应用仿射变换到三角形 2. ...
- VSCode运行C/C++配置
将MinGw安装目录下的 1.安装 VSCode 2.安装 MinGW 链接:点击跳转 3.MinGW 内安装两个模块 1.右键 Mark for Installation 勾选 (此处已安装好,所以 ...
- Siri Shortcut
AppDelegate.m //#pragma mark - INUIAddVoiceShortcutButtonDelegate //新添加 - (void)presentAdd ...
- Linux C 打印变量数组内容到一个文件中的方法。
思路: 1.以追加的方式新建打开一个文件 2.将buf 按字节,先将整型转换为字符串,之后写入到文件中. memset(tmp_buf, 0, sizeof(tmp_buf)); sprintf(tm ...
- 使用阿里云产品搭建PHPWIND
2.4 安装PhpWind论坛网站 1. 本小节主要是在ECS云主机中快速安装PhpWind论坛网站的基础环境. 2. 远程登录到 PhpWind 主机服务器 ECS实例.请使用本实验 ...
- doc指令
## 打开doc指令方法 1. 开始菜单 --所有应用--Windows系统--命令提示符 2. win+r 输入cmd 3. 桌面文件夹下面按住shift+右键选择在此处打开窗口 4. 文件导航栏路 ...
- 【高并发】AQS中的CountDownLatch、Semaphore与CyclicBarrier用法总结
CountDownLatch 概述 同步辅助类,通过它可以阻塞当前线程.也就是说,能够实现一个线程或者多个线程一直等待,直到其他线程执行的操作完成.使用一个给定的计数器进行初始化,该计数器的操作是原子 ...
- 一幅图像为f=[1 4 7;2 5 8;3 6 9],设kx=1.8,ky=1.3,试采用最邻近插值对其进行放大,写出新图像矩阵。
一幅图像为f=[1 4 7;2 5 8;3 6 9],设kx=1.8,ky=1.3,试采用最邻近插值对其进行放大,写出新图像矩阵.(请写出解题步骤,并编写程序进行结果验证) 参考 matlab代码如下 ...
- UBUNTU18.04安装使用ORB-SLAM2
1.下载: git clone https://github.com/raulmur/ORB_SLAM2.git ORB_SLAM2 2.依赖项: sudo apt install autotools ...