ZOJ 3953-贪心

题目链接

对区间的左端点进行排序,然后每三个点进行一次判断,如果出现两两相交的情况就删除右端点值最大的那个区间

#include <iostream>  
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <string>
#include <string.h>
#include <cmath>
#include <sstream>
#include <set>
#include <map>
#include <functional>
#include <queue>
#include <vector>
using namespace std;

const int maxn = 50010;
int t, n;
struct N
{
    int l, r, id;
    bool operator<(const N& a) const
    {
            return l < a.l;
    }
}Num[maxn], temp[3];

bool cmp_r(N a, N b)
{
    return a.r > b.r;
}

bool cmp_r2(N a, N b)
{
    return a.r < b.r;
}

int main()
{
    cin >> t;
    while (t--)
    {
        cin >> n;
        for (int i = 1; i <= n; i++)
        {
            cin >> Num[i].l >> Num[i].r;
            Num[i].id = i;
        }
        sort(Num + 1, Num + 1 + n);
        vector<int> res;
        res.clear();        
        for (int i = 1, num = 0; i <= n; i++)
        {
            sort(temp, temp + num, cmp_r);
            if (num&&temp[num-1].r < Num[i].l)//如果不相交
                num--;
            temp[num++] = Num[i];
            if (num == 3)
            {
                sort(temp, temp + 3, cmp_r2);
                res.push_back(temp[2].id);
                num--;
            }
        }   
        sort(res.begin(), res.end());
        cout << res.size() << endl;
        for (int i = 0; i < res.size(); i++)
        {
            if (i == res.size() - 1)
                cout << res[i] << endl;
            else
                cout << res[i] << " ";
        }           
        cout << endl;
    }
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值