D2. Too Many Segments (hard version)

D2. Too Many Segments (hard version)

题目链接

这是一道贪心题目,每次枚举左端点,然后删除的话是离左端点最远的那个点,可以用set维护。
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <vector>
#define ll long long int
#define ms(a, b) memset(a, b, sizeof(a))
#define lowbit(x) (x & -x)
#define fi first
#define se second
#define Size(a) int((a).size())
#define ull unsigned long long
#define lson (rt << 1)
#define rson (rt << 1 | 1)
#define endl "\n"
#define bug cout << "----acac----" << endl;
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
using namespace std;
ll ksm(ll a, ll b, ll mod)
{
    ll ans = 1;
    while (b)
    {
        if (b & 1)
        {
            ans = (a % mod * ans % mod) % mod;
        }
        b >>= 1;
        a = (a % mod * a % mod) % mod;
    }
    return ans;
}
ll gcd(ll a, ll b)
{
    return b == 0 ? a : gcd(b, a % b);
}
ll read()
{
    ll x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9')
    {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
    {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return x * f;
}
const ll mod = 10007;
const int maxn = 3e5 + 10;
const int maxm = 2e3 + 50;
const double eps = 1e-8;
const ll inf = 0x3f3f3f3f;
const ll lnf = 0x3f3f3f3f3f3f3f3f;
const double pi = acos(-1);

vector<pair<int, int>> ve[maxn];
vector<int> ans;
set<pair<int, int>> s;

int main()
{
    IOS;
    int n, k;
    cin >> n >> k;
    for (int i = 1; i <= n; i++)
    {
        int l, r;
        cin >> l >> r;
        ve[l].push_back({r, i});
    }
    for (int i = 1; i <= 200000; i++)
    {
        while(Size(s)&&s.begin()->fi<i)
        {
            s.erase(s.begin());
        }
        for(auto u:ve[i])
        {
            s.insert(u);
        }
        while(Size(s)>k)
        {
            ans.push_back(prev(s.end())->se);//因为迭代器是[s.begin(),s.end()),所以这里需要返回迭代器的下一位
            s.erase(prev(s.end()));
        }
    }
    cout << Size(ans) << endl;
    for (auto u : ans)
    {
        cout << u << " ";
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值