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;

}

相关推荐
_不会dp不改名_32 分钟前
leetcode_42 接雨水
算法·leetcode·职场和发展
Swaggy T32 分钟前
自动驾驶轨迹规划算法——Apollo EM Planner
人工智能·算法·自动驾驶
野生的编程萌新1 小时前
从冒泡到快速排序:探索经典排序算法的奥秘(二)
c语言·开发语言·数据结构·c++·算法·排序算法
iLoyalty1 小时前
防御保护15
算法·哈希算法
weixin_307779131 小时前
VS Code配置MinGW64编译backward库
开发语言·c++·vscode·算法
Crazy_eater1 小时前
C++继承(1)
c++
花开富贵ii3 小时前
代码随想录算法训练营四十三天|图论part01
java·数据结构·算法·深度优先·图论
weixin_307779133 小时前
AWS Lambda解压缩S3 ZIP文件流程
python·算法·云计算·aws
破刺不会编程4 小时前
socket编程UDP
linux·运维·服务器·网络·c++·网络协议·udp
code小毛孩4 小时前
leetcode hot100数组:缺失的第一个正数
数据结构·算法·leetcode