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

【题目描述】

You are given $n$ segments on a number line; each endpoint of every segment has integer coordinates. Some segments can degenerate to points. Segments can intersect with each other, be nested in each other or even coincide.

The intersection of a sequence of segments is such a maximal set of points (not necesserily having integer coordinates) that each point lies within every segment from the sequence. If the resulting set isn‘t empty, then it always forms some continuous segment. The length of the intersection is the length of the resulting segment or $0$ in case the intersection is an empty set.

Your task is to remove exactly one segment from the given sequence in such a way that the intersection of the remaining $(n?1)$ segments has the maximal possible length.

【算法】

    预处理前后缀最大左区间和最小右区间,枚举remove的位置,取区间长度的最大值。复杂度O(n)。

【代码】

#include <bits/stdc++.h>
using namespace std;
int n,ans;
int a[300010][2],pre[300010][2],post[300010][2];
inline int read() {
    int x=0,f=1; char c=getchar();
    while(c<‘0‘||c>‘9‘) { if(c==‘-‘)f=-1; c=getchar(); }
    while(c>=‘0‘&&c<=‘9‘) { x=x*10+c-‘0‘; c=getchar(); }
    return x*f;
}
int main() {
    n=read(); post[n+1][1]=pre[0][1]=2e9;
    for(int i=1;i<=n;i++) a[i][0]=read(),a[i][1]=read();
    int max1=a[1][0],min1=a[1][1],max2=a[n][0],min2=a[n][1];
    for(int i=1;i<=n;i++) {
        max1=max(max1,a[i][0]),min1=min(min1,a[i][1]);
        max2=max(max2,a[n-i+1][0]),min2=min(min2,a[n-i+1][1]);
        pre[i][0]=max1,pre[i][1]=min1;
        post[n-i+1][0]=max2,post[n-i+1][1]=min2;
    }
    for(int i=1;i<=n;i++) {
        int l=max(pre[i-1][0],post[i+1][0]),r=min(pre[i-1][1],post[i+1][1]);
        ans=max(ans,r-l);
    }
    printf("%d\n",ans);
    return 0;
}

原文地址:https://www.cnblogs.com/Willendless/p/9538978.html

时间: 2024-07-30 03:42:15

Codeforces Round #506 (Div. 3) 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 standard input output standard output You are given nn segments on a number line; each endpoint of every segment has integer coordinates. Some segments ca

Codeforces Round #506 (Div. 3) 题解

Codeforces Round #506 (Div. 3) 题目总链接:https://codeforces.com/contest/1029 A. Many Equal Substrings 题意: 给出长度为n的字符串,然后要求你添加一些字符,使得有k个这样的字符串. 题解: 直接暴力吧...一个指针从1开始,另一个从2开始,逐一比较看是否相同:如果不同,第一个指针继续回到1,第二个指针从3开始...就这么一直重复.最后如果第二个指针能够顺利到最后一位,那么记录当前的第一个指针,把他后面的

Codeforces Round #506 (Div. 3) D-F

Codeforces Round #506 (Div. 3) (中等难度) 自己的做题速度大概只尝试了D题,不过TLE D. Concatenated Multiples 题意 数组a[],长度n,给一个数k,求满足条件的(i,j)(i!=j) a[i],a[j]连起来就可以整除k 连起来的意思是 20,5连起来时205; 5,20连起来时520 n<=2*1e5,k<=1e9,ai<=1e9 愚蠢的思路是像我一样遍历(i,j)可能性,然后TLE,因为这是O(n^2) 可以先思考一简单问

Codeforces Round #506 (Div. 3) A-C

CF比赛题解(简单题) 简单题是指自己在比赛期间做出来了 A. Many Equal Substrings 题意 给个字符串t,构造一个字符串s,使得s中t出现k次;s的长度最短 如t="cat",k=3, minlen(s)=9,s=catcatcat 1<=len(t),k<=50 题解 稍加思考是个前缀等于后缀的问题,用kmp的next数组模板 假设t[0..l-1]==t[n-l....n-1] 那么应该不断重复t[l...n-1]这样的子串 代码如下(应该好好记住

Codeforces Round #506 (Div. 3)ABCDEF

并没有参加比赛,全是赛后AC A题 题意:现有一个长度为n的字符串s.你需要构建一个长度最小的字符串t,使得t中恰好包含k个s(允许部分重叠),输出这个字符串 1 /* 2 我们可以非常容易的发现我们构造出来的字符串t有着鲜明的特征.即t中有大量重复的子串,可以证明的是t = S + (K-1)*A, A是S串中删去一段字符串(这段字符串满足既是S的前缀又是S的后缀). 3 */ 4 #include<cstdio> 5 #include<iostream> 6 7 using n

Codeforces Round #266 (Div. 2)B(暴力枚举)

很简单的暴力枚举,却卡了我那么长时间,可见我的基本功不够扎实. 两个数相乘等于一个数6*n,那么我枚举其中一个乘数就行了,而且枚举到sqrt(6*n)就行了,这个是暴力法解题中很常用的性质. 这道题找出a和b中最小的那个,然后开始枚举,一直枚举到sqrt(6*n)的向上取整.这样所有可能是答案的情况都有啦.再干别的都是重复的或者肯定不是最小面积的. #include<iostream> #include<cstdio> #include<cstdlib> #includ

Codeforces Round #352 (Div. 2) C. Recycling Bottles(枚举)

思路:分析完这道题后会发现  当两个人捡到第一个瓶子后, 之后走的路的最小值都是不会变的了. 所以只要找出两个走到各自的第一个瓶子是最小值的情况的时候(其中还有一个人不走,一个人走的情况).   如果当有两个人或有一个人到其第一个瓶子的权值大于瓶子到回收点时,选取权值小的那个. 而且计算最小值的时候 只需取出两个权值数组中最小的两个数 即可 不用全部遍历来选取. #include<iostream> #include<cstdio> #include<cmath> #i

Codeforces Round #266 (Div.2) B Wonder Room --枚举

题意:给出一个两边长为a,b的矩形,要求增加a和增加b使a*b>=6*n且a*b最小. 解法:设新的a,b为a1,b1,且设a<b,那么a<=a1<=ceil(sqrt(6*n)),那么我们可以枚举a1,然后算出b1,如果b1<b,那么b1 = b,然后算出面积,取所有面积的最小值不就可以了,然后再枚举一次b1,处理与之相同即可. 复杂度O(sqrt(n)) 代码: #include <iostream> #include <cstdio> #incl

Codeforces Round #315 (Div. 1)

A. Primes or Palindromes? time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Rikhail Mubinchik believes that the current definition of prime numbers is obsolete as they are too complex and un