906. 区间分组(贪心)

路径:906. 区间分组 - AcWing题库

思路:

可以想到是求区间最厚的地方。

每次输入l,r区间让l*2,r*2+1存入同一个数组;

注意:当l=r时,要先计算左点。

代码:

#define _CRT_SECURE_NO_WARNINGS

#include<iostream>

#include<string>

#include<cstring>

#include<cmath>

#include<ctime>

#include<algorithm>

#include<utility>

#include<stack>

#include<queue>

#include<vector>

#include<set>

#include<map>

#include<unordered_map>

using namespace std;

#define LL long long

const int N = 2e5+100;

int b[N];

int main() {

int n;

cin >> n;

for (int i = 1; i <= n; i++)

{

int l, r;

cin >> l >> r;

b[2 * i - 1] = l * 2;

b[2 * i] = r * 2+1;

}

n =n*2;

sort(b + 1, b + 1 + n);

int ans = 0,maxx=0;

for (int i = 1; i <= n; i++)

{

if (b[i] % 2 == 0) ans++;

else ans--;

maxx = max(ans, maxx);

}

cout << maxx << endl;

return 0;

}

相关推荐
草莓熊Lotso32 分钟前
Linux 文件描述符与重定向实战:从原理到 minishell 实现
android·linux·运维·服务器·数据库·c++·人工智能
历程里程碑35 分钟前
Linux22 文件系统
linux·运维·c语言·开发语言·数据结构·c++·算法
你撅嘴真丑8 小时前
第九章-数字三角形
算法
在路上看风景8 小时前
19. 成员初始化列表和初始化对象
c++
uesowys8 小时前
Apache Spark算法开发指导-One-vs-Rest classifier
人工智能·算法·spark
zmzb01038 小时前
C++课后习题训练记录Day98
开发语言·c++
ValhallaCoder8 小时前
hot100-二叉树I
数据结构·python·算法·二叉树
董董灿是个攻城狮8 小时前
AI 视觉连载1:像素
算法
念风零壹9 小时前
C++ 内存避坑指南:如何用移动语义和智能指针解决“深拷贝”与“内存泄漏”
c++
智驱力人工智能9 小时前
小区高空抛物AI实时预警方案 筑牢社区头顶安全的实践 高空抛物检测 高空抛物监控安装教程 高空抛物误报率优化方案 高空抛物监控案例分享
人工智能·深度学习·opencv·算法·安全·yolo·边缘计算