CF C. Maximal Intersection(贪心 || STL)
题意
给你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)的更多相关文章
- C. Maximal Intersection(STL)
这道题,关键在于怎么求多个区间的交集,使用multiset就可以 分别将 r , l 存在不同的mutiset中. 然后,我们来看一下 是不是 交集的 l 是最大的, 交集的 r 是最小的 #incl ...
- 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 ...
- CF 990B. Micro-World【数组操作/贪心/STL/二分搜索】
[链接]:CF [题意]:对任意一个数a[i] ,可以对任意 满足 i != j 且 a[i] > a[j] && a[i] <= a[j] +k 的 a[j] 可以被删掉 ...
- 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 ...
- HDU 4268 Alice and Bob 贪心STL O(nlogn)
B - Alice and Bob Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u D ...
- CodeForces C. Maximal Intersection
http://codeforces.com/contest/1029/problem/C You are given nn segments on a number line; each endpoi ...
- Codeforces Round #595 (Div. 3)D1D2 贪心 STL
一道用STL的贪心,正好可以用来学习使用STL库 题目大意:给出n条可以内含,相交,分离的线段,如果重叠条数超过k次则为坏点,n,k<2e5 所以我们贪心的想我们从左往右遍历,如果重合部分条数超 ...
- F - Maximal Intersection --------暴力求解题
You are given n segments on a number line; each endpoint of every segment has integer coordinates. S ...
随机推荐
- selenium webdriver frame操作,跳进跳出
如果有两个平级的frame,跳进一个以后操作完成再操作第二个,这种情况要先跳出来,再跳进另外一个frame 跳出语句:browser.switch_to_default_content() #codi ...
- 2008上交:Day of Week
题目描述: We now use the Gregorian style of dating in Russia. The leap years are years with number divis ...
- Java中继承的规则
一.子类中对父类构造函数的调用规则 1.如果不显式指定,子类中的构造函数会默认调用父类中的无参构造 测试代码 package day07; import java.sql.Date; import j ...
- AlteraFPGA使用通用SPIFlash - 张亚群的技术专栏 - 博客频道 - CSDN.NET
AlteraFPGA使用通用SPIFlash - 张亚群的技术专栏 - 博客频道 - CSDN.NET Altera器件有EPCS系列配置器件,其实,这些配置器件就是我们平时通用的SPIFlash,据 ...
- maven可用镜像
<mirrors> <mirror> <id>alimaven</id> <name>aliyun maven</name> & ...
- ASPX 关闭子窗口后自动更新父窗口
Response.Write("<script language:javascript>javascript:window.close();</script>&quo ...
- keras安装windows版
按照官网成功了.下面没有成功,貌似是 Anacode的问题 http://blog.csdn.net/hweiyi/article/details/70018317 http://blog.csdn. ...
- Struts2学习第七课 ActionSupport
com.opensymphony.xwork2.ActionSupport类是默认的Action类,如果某个Action节点没有配置class属性,则ActionSupport即为待执行的Action ...
- 使用IDEA编译netty4
本来计划一天一篇博文的,结果周末加班加的不亦乐乎,对整个项目重构添加国际化的功能,不停的使用native2ascii工具 使用IDEA编译原有的netty4要报错,我修改了下pom.xml,编译成功了 ...
- 常用转义字符例如&的含义
&中的amp就是英文ampersand的缩写,该词的意思是&这个符号& 是 HTML 中 & 的表示方法.即在html中用&表示&符号