题意:给了n个区间,要求你删去最少的区间,使任意三个区间 a,b,c 不存在 a与b相交,b与c相交,c与a相交 的情况。
分析:比赛时候看到这题,还以为要用什么高深的数据结构,出来听他们说贪心后马上就想到了思路,,还是太菜了。。首先,我们先将n个区间按x从小到大,再y从小到大排序,然后遍历,如果三个区间满足 上述的情况,那么我们将y最大的删去,y相等将x最小的删去。过程中维护两个区间tmp1,tmp2,表示前两个相交的区间是什么,然后就是一些分类,具体看代码。
以下是代码。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<cstring>
#include<string>
#include<vector>
#include<iomanip>
#include<unordered_set>
#include<unordered_map>
#include<cmath>
#include<list>
#include<bitset>
using namespace std;
#define ull unsigned long long
#define ll long long
#define lson l,mid,id<<1
#define rson mid+1,r,id<<1|1
typedef pair<int, int>pii;
typedef pair<ll, ll>pll;
typedef pair<double, double>pdd;
const double eps = 1e-6;
const int MAXN = 100005;
const int MAXM = 5005;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int INF = 0x3f3f3f3f;
const double FINF = 1e18;
const ll MOD = 1000000007;
struct lx
{
lx(){}
lx(int _x, int _y, int _id)
{
x = _x, y = _y, id = _id;
}
int x, y, id;
bool operator<(const lx &a)const
{
if (x == a.x)return y < a.y;
else return x < a.x;
}
}tmp[50005];
lx getid(lx a, lx b, lx c)
{
int id, x, y;
if (a.y > b.y)
{
y = a.y;
x = a.x;
id = a.id;
}
else if (a.y == b.y)
{
if (a.x < b.x)
{
y = a.y; x = a.x;
id = a.id;
}
else
{
y = b.y; x = b.x;
id = b.id;
}
}
else
{
y = b.y; x = b.x; id = b.id;
}
if (c.y > y)x = c.x, y = c.y, id = c.id;
else if (c.y == y)
{
if (c.x < x)
{
x = c.x, y = c.y, id = c.id;
}
}
return lx(x, y, id);
}
int main()
{
int t, n, cnt, num;
scanf("%d", &t);
lx tmp1, tmp2;
vector<int>ans;
while (t--)
{
num = 0;
cnt = 0;
ans.clear();
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
{
scanf("%d%d", &tmp[i].x, &tmp[i].y);
tmp[i].id = i;
}
sort(tmp + 1, tmp + n + 1);
//for (int i = 1; i <= n; ++i)cout << tmp[i].x << " " << tmp[i].y << " " << tmp[i].id << endl;
if (tmp[2].x >= tmp[1].x && tmp[2].x <= tmp[1].y)
{
tmp1 = tmp[1], tmp2 = tmp[2];
num = 2;
}
else
{
tmp1 = tmp[2], num = 1;
}
for (int i = 3; i <= n; ++i)
{
if (num == 2)
{
if (tmp[i].x >= tmp2.x && tmp[i].x <= tmp2.y)
{
if (tmp[i].x >= tmp1.x && tmp[i].x <= tmp1.y)
{
cnt++;
lx uu = getid(tmp[i], tmp1, tmp2);
ans.push_back(uu.id);
if (uu.id == tmp1.id)
{
tmp1 = tmp2; tmp2 = tmp[i];
}
else if (uu.id == tmp2.id)tmp2 = tmp[i];
}
else
{
tmp1 = tmp2;
tmp2 = tmp[i];
}
}
else
{
if (tmp[i].x >= tmp1.x && tmp[i].x <= tmp1.y)
{
tmp2 = tmp[i];
}
else
{
tmp1 = tmp[i]; num = 1;
}
}
}
else
{
if (tmp[i].x >= tmp1.x && tmp[i].x <= tmp1.y)
{
tmp2 = tmp[i]; num = 2;
}
else
{
tmp1 = tmp[i];
}
}
}
sort(ans.begin(), ans.end());
printf("%d\n", cnt);
if (cnt == 0)printf("\n");
for (int i = 0; i < ans.size(); ++i)
{
if (i == ans.size() - 1)printf("%d\n", ans[i]);
else printf("%d ", ans[i]);
}
}
}