您的位置:首页 > 其它

ZOJ 3953-贪心

2017-08-16 10:54 344 查看

题目链接

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

#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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  贪心