两重“线段覆盖”问题

Recording the Moolympics

题目描述 :
Being a fan of all cold-weather sports (especially those involving cows),Farmer John wants to record as much of the upcoming winter Moolympics as possible.
The television schedule for the Moolympics consists of N different programs (1 <= N <= 150), each with a designated starting time and ending time. FJ has a dual-tuner recorder that can record two programs simultaneously.
Please help him determine the maximum number of programs he can record in total.
输入 :

  • Line 1: The integer N.
  • Lines 2…1+N: Each line contains the start and end time of a single program (integers in the range 0…1,000,000,000).
    输出
  • Line 1: The maximum number of programs FJ can record.
    样例输入:
    6
    0 3
    6 7
    3 10
    1 5
    2 8
    1 9
    样例输出:
    4
    与线段覆盖问题一样,要把结束点早的放前面,不同的是,要有两个参数,同时开始记录(要同时开始记录,分开上下两次记录是要WA掉的,具体原因暂不明)。要注意,两次记录的标记前后顺序不能变,要是某一次变了,需要swap回来。
#include<bits/stdc++.h>
using namespace std;
typedef struct
{
    int a,b;
} STU;
bool cmp(STU x,STU y)
{
    if(x.b!=y.b)
        return x.b<y.b;
    else
        return x.a>y.a;
}
int main()
{
    int n;
    cin>>n;
    STU stu[n],stu1[n];
    for(int i=0; i<n; i++)
        cin>>stu[i].a>>stu[i].b;
    sort(stu,stu+n,cmp);
    int r=0,l=0,ans=0;
    for(int i=0; i<n; i++)
    {
        if(stu[i].a>=r)
            r=stu[i].b,ans++;
        else if(stu[i].a>=l)
            l=stu[i].b,ans++;
        if(r<l)
            swap(r,l);
    }
    cout<<ans<<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值