题意

给你N个线段(一条直线上),问删去一个之后,最长公共长度 ;

分析:首先我们得先知道n条线段公共的线段一定是(LMAX,RMIN) ,那我们可以先排序,然后枚举删除边;

#include<stdio.h>
#include<algorithm>
using namespace std;
struct no
{
int l,r;
}a[];
int l[],r[];
int main( )
{
int n;
scanf("%d",&n);
for(int i= ; i<=n ; i++)
{
scanf("%d%d",&a[i].l,&a[i].r);
l[i]=a[i].l;r[i]=a[i].r;
}
sort(l+,l++n);
sort(r+,r++n);
int ans = ;
for(int i= ; i<=n ; i++)
{
int end=l[n];///左端点最大部分;
int st=r[];///右端点最小的部分
if(end==a[i].l)/// 如果删边为最后一个左端点,左移一位
end=l[n-];
if(st==a[i].r)/// 如果删边为最后一个左端点,左移一位
st=r[];
ans = max(ans,st-end);
}
printf("%d\n",ans); }

SLT 做法;

c.begin() 返回一个迭代器,它指向容器c的第一个元素

c.end() 返回一个迭代器,它指向容器c的最后一个元素的下一个位置

c.rbegin() 返回一个逆序迭代器,它指向容器c的最后一个元素

c.rend() 返回一个逆序迭代器,它指向容器c的第一个元素前面的位置

multiset和set不同之处在于multiset可以有重复的元素。

#include<bits/stdc++.h>
#define maxn 300005
using namespace std;
multiset<int>a,b;
int L[maxn],R[maxn];
int main()
{
int n;
cin>>n;
for(int i=;i<n;i++)
{
cin>>L[i]>>R[i];
a.insert(L[i]);
b.insert(R[i]);
}
/// cout<<-1<<endl;
int ans=0;
for(int i=;i<n;i++)
{
a.erase(a.find(L[i]));
b.erase(b.find(R[i]));
ans=max(ans,*(b.begin())-*(a.rbegin()));
a.insert(L[i]);
b.insert(R[i]);
}
cout<<ans<<endl;
return ;
}

CF C. Maximal Intersection(贪心 || STL)的更多相关文章

  1. C. Maximal Intersection(STL)

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

  2. 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 ...

  3. CF1029C Maximal Intersection 暴力枚举

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

  4. CF 990B. Micro-World【数组操作/贪心/STL/二分搜索】

    [链接]:CF [题意]:对任意一个数a[i] ,可以对任意 满足 i != j 且 a[i] > a[j] && a[i] <= a[j] +k 的 a[j] 可以被删掉 ...

  5. CodeForces - 620C Pearls in a Row 贪心 STL

    C. Pearls in a Row time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  6. HDU 4268 Alice and Bob 贪心STL O(nlogn)

    B - Alice and Bob Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u D ...

  7. CodeForces C. Maximal Intersection

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

  8. Codeforces Round #595 (Div. 3)D1D2 贪心 STL

    一道用STL的贪心,正好可以用来学习使用STL库 题目大意:给出n条可以内含,相交,分离的线段,如果重叠条数超过k次则为坏点,n,k<2e5 所以我们贪心的想我们从左往右遍历,如果重合部分条数超 ...

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

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

随机推荐

  1. BZOJ4317: Atm的树+2051+2117

    BZOJ4317: Atm的树+2051+2117 https://lydsy.com/JudgeOnline/problem.php?id=4317 分析: 二分答案之后就变成震波那道题了. 冷静一 ...

  2. JS通过经纬度计算两个地方的距离

    1 主要原理: Lat1 Lung1 表示A点纬度和经度,Lat2 Lung2 表示B点纬度和经度: a=Lat1 – Lat2 为两点纬度之差  b=Lung1 -Lung2 为两点经度之差: 63 ...

  3. Azure PIP (Instance Level Public IP)

    微软的Azure平台已经支持Instance Level Public IP功能.当有复杂协议的情况下,需要开启多个端口的情况下,可以考虑开启PIP功能. 先介绍几个概念: VIP – virtual ...

  4. Oracle中生成UUID

    Oracle中生成跨系统的唯一识别符UUID非常方便,比生成序列还简单,直接用sys_guid()就行, 例如select sys_guid() from dual 会产生一个跟MAC地址.生成时间相 ...

  5. python 基础 列表 字符串转换

    1. 字符串转列表 str1 = "hi hello world" print(str1.split(" "))输出:['hi', 'hello', 'worl ...

  6. free查看内存情况

    free命令可以显示当前系统未使用的和已使用的内存数目,还可以显示被内核使用的内存缓冲区. free [option]     -b:以Byte为单位显示内存使用情况:      -k:以KB为单位显 ...

  7. [poj3311]Hie with the Pie(Floyd+状态压缩DP)

    题意:tsp问题,经过图中所有的点并回到原点的最短距离. 解题关键:floyd+状态压缩dp,注意floyd时k必须在最外层 转移方程:$dp[S][i] = \min (dp[S \wedge (1 ...

  8. pig flatten

    今天通过不断的尝试,终于知道这个flatten的用法了.其实吧,有时候关键是要test,才能充分理解解说.不过,同事给说的有点问题,误导了我.整的我一直没明白怎么回事. 这是官方的解释: The FL ...

  9. JavaScript中函数作为值

    function myfunc() { // .. } 这是个函数,这样理解, myfunc只是外层作用域的一个变量,指向刚刚声明的function. 也就是说,function本身就是一个值, 就像 ...

  10. 4.Windows应急响应:勒索病毒

    0x00 前言 勒索病毒,是一种新型电脑病毒,主要以邮件.程序木马.网页挂马的形式进行传播.该病毒性质恶劣. 危害极大,一旦感染将给用户带来无法估量的损失.这种病毒利用各种加密算法对文件进行加密,被感 ...