题意

给你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. http头部解释

    If-Modified-Since,If-Node-Match,ETag,Last-Modified 1 属于 Request Headers的是:If-Modified-Since,If-Node- ...

  2. 洛谷【P1104】生日(插入排序版)

    题目传送门:https://www.luogu.org/problemnew/show/P1104 题目很简单,我主要是来讲插入排序的. 所谓插入排序,就是从待排序数组不断将数据插入答案数组里. 假设 ...

  3. pip3 更改安装源

    经常在使用Python的时候需要安装各种模块,而pip是很强大的模块安装工具,但是由于国外官方pypi经常被墙,导致不可用,所以我们最好是将自己使用的pip源更换一下,这样就能解决被墙导致的装不上库的 ...

  4. HDOJ1022(模拟栈)

    Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  5. HDOJ1171(多重背包)

    #include<iostream> #include<cstdio> using namespace std; #define MAX(a,b) (a>b)?a:b + ...

  6. 【转】 Pro Android学习笔记(六十):Preferences(4):MultiSelect List Preference

    目录(?)[-] XML文件 在设备中保存 读出信息 ListPreference提供单选列表,我们可以通过CheckBoxPreference提供多选列表.此外,Android在3.0后提供Mult ...

  7. 通过pip3安装ipython

    操作系统:Centos7.4:ipython可以用来测试python语句,故需要安装. 首先需要安装epelde的扩展库: easy_install是由PEAK(Python Enterprise A ...

  8. springMVC绑定json参数之二(2.1.1)

    二.springmvc 接收不同格式的json字符串 1.首先扫盲几个知识点: 例子如下: 前台传递json对象(这里uu[0]的名字uu要和Test对象中的属性List<User>名称对 ...

  9. 关于surf显示立体图,可视化分析数据

    如果想判断一个点(x,y)对应的ZV值是否在平面上方.平面上.平面下方,只要将(x,y)带入方程,得到z. 如果ZV大于>Z,则在平面上方:如果ZV<Z,则在方面下方:若ZV=Z,则在平面 ...

  10. shell入门-特殊符号

    特殊符号:* 说明:通配符,多个字符.所有后缀为”.txt“的文件列出来 [root@wangshaojun 111]# ls *.txt11.txt 1.txt 22.txt 2.txt 33.tx ...