AcWing803区间合并-解释 if(st!=INT_MIN) res.push_back({st,ed});的必要性

在这里插入图片描述
sort定义在<algorithm>头文件
INT_MIN,定义在<climits>头文件
res.push_back({st,ed});的逻辑是seg的这组区间和上一组的出现了区间断层!
比如上组是[1,4],下一组是[5,6],st=1,ed=4,seg=[5,6],则 e d < s e g . f i r s t ed \lt seg.first ed<seg.first是可以直接res.push_back({st,ed});的!因为出现了区间断层!直接保存res即可!

#include<iostream>
#include<vector>
#include<algorithm>
#include<climits>
using namespace std;
typedef pair<int,int> PII;
vector<PII> res,segs;
void merage(vector<PII>& segs){
    int st=INT_MIN,ed=INT_MIN;
    sort(segs.begin(),segs.end());
    for(auto seg:segs){
        if(ed<seg.first){
            if(st!=INT_MIN){
                res.push_back({st,ed});
            }
            st=seg.first,ed=seg.second;
        }else{
            ed=max(ed,seg.second);
        }
    }
    if(st!=INT_MIN) res.push_back({st,ed});//这一行是必要的
    
}
int main(){
    int n;
    cin>>n;
    for(int i=1;i<=n;++i){
        int l,r;
        cin>>l>>r;
        segs.push_back({l,r});
    }
    merage(segs);
    cout<<res.size()<<endl;
    return 0;
}

if(st!=INT_MIN) res.push_back({st,ed});代码的缺失,会导致如下用例不通过!这个用例的情况是排序完了之后,进行区间合并时直接合并到了Lmin和Rmax,但是还没有放到res结果数组里面,然后一直 e d ≥ s e g . f i r s t ed \ge seg.first edseg.first,而不是ed<seg.first,导致一直无法保存res数组。

10
-15 -8
-20 23
-2 11
2 22
18 23
11 27
-8 21
-18 14
-17 -12
-23 8
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
这段代码是用来加载一个名为 Octree_python_lib.so 的动态链接库,并定义了一些函数接口和函数参数的类型。下面是对每个定义的函数接口和参数类型的解释- c_double_p: 定义了一个指向 double 类型的指针。 - c_uint16_p: 定义了一个指向 uint16 类型的指针。 - lib = cdll.LoadLibrary(os.path.dirname(os.path.abspath(__file__))+'/Octree_python_lib.so'): 加载 Octree_python_lib.so 动态链接库,并将其赋值给 lib 变量。 - lib.new_vector: 函数接口,返回类型为 c_void_p,无参数。 - lib.delete_vector: 函数接口,返回类型为 None,参数为 c_void_p。 - lib.vector_size: 函数接口,返回类型为 c_int,参数为 c_void_p。 - lib.vector_get: 函数接口,返回类型为 c_void_p,参数为 c_void_p 和 c_int。 - lib.vector_push_back: 函数接口,返回类型为 None,参数为 c_void_p 和 c_int。 - lib.genOctreeInterface: 函数接口,返回类型为 c_void_p,参数为 c_void_p、c_double_p 和 c_int。 - lib.Nodes_get: 函数接口,返回类型为指向 Node 类型的指针 (POINTER(Node)),参数为 c_void_p 和 c_int。 - lib.Nodes_size: 函数接口,返回类型为 c_int,参数为 c_void_p。 - lib.int_size: 函数接口,返回类型为 c_int,参数为 c_void_p。 - lib.int_get: 函数接口,返回类型为 c_int,参数为 c_void_p 和 c_int。 这些函数接口的具体功能需要根据实际的动态链接库 Octree_python_lib.so 来确定。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿维的博客日记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值