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.erase(prev(s.end()));
}
}
cout << Size(ans) << endl;
for (auto u : ans)
{
cout << u << " ";
}
return 0;
}