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 ...
随机推荐
- Mybatis generator配置文件及说明
项目采用sring mvc + mybatis 组合,这里简单介绍下mybatis的应用: 我的IDE是STS(Spring + Tool + Suite), 安装Mybatis Generator插 ...
- 【LeetCode】019. Remove Nth Node From End of List
Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...
- HDOJ1025(最长上升子序列)
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
- Python:删除字符串中的字符
一.删除字符串两端的一种或多种字符 #strip().lstrip().rstrip()方法:(默认删除空格符) A.list.strip(字符):删除字符串两端的一种或多种字符: #例:删除字符串s ...
- node包管理工具nvm
去NVM官网下载NVM压缩包,下载nvm-setup.zip,直接傻瓜式安装 安装成功后运行命令: nvm -v 常用命令: nvm install <version> ## 安装指定版本 ...
- ML 徒手系列 SVM
在Lagrange乘子法中,介绍了如何构建及如何使用对偶函数,对目标问题进行求解. 这一章利用L乘子法对SVM进行推导. SVM 即支持向量机,是通过求解超平面进行分类的一种算法.所谓的支持向量,是在 ...
- qextserialport打不开com10及以上的串口
需要在portname前添加\\\\.\\这样就可以了!! 例如 QString portname; portname.append("\\\\.\\").append(ui-&g ...
- 17_点击事件第四种写法_布局文件添加onclick属性
尽量不要用第四种点击事件的写法.在一万多行代码中发现了一个没被调用的代码 public void call(View v){//第四种写法参数一定是View v //public void call( ...
- socket消息发送
expressClient.html <html><head><meta http-equiv="Content-Type" content=&quo ...
- 2、misa统计SRR结果
参考: https://www.sogou.com/link?url=hedJjaC291NYNxVe4xgB4c3bUxXRMqZrT93cntTAgYfyBbRAdP9kIA.. https:// ...