区间选点(重载函数 贪心 *基础运用)

题目大意:

此题为贪心的基础运用 ,求得需求的最小

解题思路:

先以右端点排序(利用重载函数),从大到小,然后从头开始遍历 ,如果当前线段的左端点小于上个线段的右端点即可用一个点来表达(在处理的时候就可以忽略掉该线段. 否则就要用另一个点来表达ans ++

并且更换左端点。

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector> 
#include <map>
#include <stack>
#include <queue>
#include <set>
using namespace std;
typedef long long ll;
const int maxx = 1000050;
int n, m, t, now, l, r, c, b , s;
const int INF = 0x3f3f3f3f;
const double pi = acos(-1.0);
const ll mod = 1e9 + 7;
int ans[225];
struct node//函数重载 // 为了使用sort
{
    int r, l;//r为头 ,l为尾
    bool operator< (const node &w)const//结构体内部排序函数
    {//这是固定格式
        return r < w.r;//从小到大排序 //按右端点排序
    }
}rang[10000010];//储存线段点的数组
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> rang[i].l >> rang[i].r;
    }//依次储存
    sort(rang, rang + n);//按右端点排序
    int ans = 0;//记录答案
    int minn = -2e9;//用最小值求大
    for (int i = 0; i < n; i++)//从0开始遍历
    {
        if (minn < rang[i].l)//如果当前区间的头比上个区间的尾所处位置要靠后
        {
            ans++;//那就要另一个点来处理
            minn = rang[i].r;//并且更换为该线段的头//相当于另起炉灶
        }
    }
    cout << ans << endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Tang_7777777

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

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

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

打赏作者

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

抵扣说明:

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

余额充值