E - Intervals 贪心

Chiaki has n intervals and the i-th of them is [li, ri]. She wants to delete some intervals so that there does not exist three intervals a, b and c such that a intersects with b, b intersects with c and c intersects with a.

Chiaki is interested in the minimum number of intervals which need to be deleted.

Note that interval a intersects with interval b if there exists a real number x such that laxra and lbxrb.

Input

There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:

The first line contains an integer n (1 ≤ n ≤ 50000) -- the number of intervals.

Each of the following n lines contains two integers li and ri (1 ≤ li < ri ≤ 109) denoting the i-th interval. Note that for every 1 ≤ i < jn, lilj or rirj.

It is guaranteed that the sum of all n does not exceed 500000.

<h4< dd="">Output

For each test case, output an integer m denoting the minimum number of deletions. Then in the next line, output m integers in increasing order denoting the index of the intervals to be deleted. If m equals to 0, you should output an empty line in the second line.

<h4< dd="">Sample Input

1
11
2 5
4 7
3 9
6 11
1 12
10 15
8 17
13 18
16 20
14 21
19 22

<h4< dd="">Sample Output

4
3 5 7 10

E - Intervals
题目大意:
就是给你n组数据,在这n组数据里,删除一部分,使得任意三个组数据不相交。
题目思路:
先对它开始的位置进行排序,小的在前,然后取三组数据,进行判断是否两两相交,如果是就先要对它进行排序。
这个排序比较重要,是以结束的数据为标准,数据越大排在越前面。
如果两两相交就删去之后排序在最前面的那组数据,否则就更新一个为最后那组数据。

其实比较想清楚就比较简单了,分成两组数据进行处理,第一组以l进行排序,第二组以r进行排序,如果要删除,就删除r最大的
那组数据,再更新一个数据。值得学习的是,这种处理方法,这样子可以很好的处理表示删除的数据。

不过这里要注意一下,对是不是两两相交判断的标准,就是先选两个例如x,y,先满足x与y相交,再加入z,z与x,z与y相交

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn=50050;
struct node
{
	int l,r,id;
}ex[maxn];
int ans[maxn];

bool cmp1(node a,node b)
{
	if(a.l==b.l) return a.r<b.r;
	return a.l<b.l;
}
bool cmp2(node a,node b)
{
	if(a.r==b.r) return a.l<b.l;
	return a.r>b.r;
}
int isinterval(node x,node y,node z)
{
	int f1=0,f2=0;
	if(x.r>=y.l) f1=1;//x&y
	if(x.r>=z.l&&y.r>=z.l) f2=1;//z&x,z&y
    if(f1&&f2) return 1;
    return 0;
}

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n,pos=0;
		scanf("%d",&n);
		for(int i=0;i<n;i++)
		{
			scanf("%d%d",&ex[i].l,&ex[i].r);
			ex[i].id=i+1;
		}
		sort(ex,ex+n,cmp1);
		node x[5];
		x[0]=ex[0];
		x[1]=ex[1];
		for(int i=2;i<n;i++)
		{
			x[2]=ex[i];
		//	sort(x,x+3,cmp1);
			int f=isinterval(x[0],x[1],x[2]);
			sort(x,x+3,cmp2);
			if(f)
			{
				ans[pos++]=x[0].id;
				swap(x[0],x[2]);
			}
		}
		sort(ans,ans+pos);
		printf("%d\n",pos);
		if(pos==0) printf("\n");
		else
		{
			for(int i=0;i<pos-1;i++) printf("%d ",ans[i]);
		    printf("%d\n",ans[pos-1]);
		}
	}
	return 0;
}

  

原文地址:https://www.cnblogs.com/EchoZQN/p/10372040.html

时间: 2024-10-16 10:17:04

E - Intervals 贪心的相关文章

POJ 1716 Integer Intervals#贪心

(- ̄▽ ̄)-* //求一个集合,这个集合与任意一个区间的交集,需至少有两个数字 //贪心过程:按n个区间的最右值从小到大对区间进行排列, //集合首先取第一个区间的最右两个数字, //到第二个区间,判断集合里的数有没有在区间里 //没有的话,就从第二个区间的最右开始往左取(cnt=0取最后两个数,cnt=1取最后一个数) #include<iostream> #include<cstdio> #include<cstring> #include<algorith

poj1716 Integer Intervals 贪心

#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; struct node { int left,right; }c[10005]; bool cmp(node x,node y)//按照右端点排序 { if(x.right<y.right) return true; if(x.right==y.right&&x.left<y.lef

{POJ}{动态规划}

动态规划与贪心相关: {POJ}{2479}{Maximum Sum} (DP) 摘要: 题意:给定n个数,求两段连续子列的最大和.思路:先从左向右dp,求出一段连续子列的最大和,再从右向左dp,求出两段连续子列的最大和,方法还是挺经典的. {POJ}{1036}{Gansters} (DP) 摘要: 题意:有个伸缩门,门的宽度0~K,每个时间可以伸长或缩短1个单位,有N个流氓,他们在T时刻到达,如果这时门的宽度正好与他们的stoutness相等时,便可获得一定的收入,问最大的收入是多少. 思路

【POJ1716】Integer Intervals——差分约束||贪心

题目大意:给出n个区间,现在要你找出一个点集,使得这n个区间都至少有2个元素在这个点集里面,问这个点集最少有几个点. 解法一:差分约束系统 分析:其实这道题应该说是POJ1201的简化版,不过要注意的一点是,如果你用的是SPFA,那么你的差分约束系统应该为: s[b+1]-s[a]>=2; s[b+1]-s[b]>=0; s[b]-s[b+1]>=1. 为什么下标要全部加上1呢?因为这里的a和b有可能为0,如果按照原来s[a-1]的写法会出现是s[-1]这类数组越界的问题. 代码: #i

POJ 1089 Intervals【合并n个区间/贪心】

There is given the series of n closed intervals [ai; bi], where i=1,2,...,n. The sum of those intervals may be represented as a sum of closed pairwise non−intersecting intervals. The task is to find such representation with the minimal number of inte

Codeforces 788A Functions again - 贪心

Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about

POJ1089 Intervals

Description There is given the series of n closed intervals [ai; bi], where i=1,2,...,n. The sum of those intervals may be represented as a sum of closed pairwise non−intersecting intervals. The task is to find such representation with the minimal nu

POJ 1042 Gone Fishing (贪心)(刘汝佳黑书)

Gone Fishing Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 30281   Accepted: 9124 Description John is going on a fishing trip. He has h hours available (1 <= h <= 16), and there are n lakes in the area (2 <= n <= 25) all reachab

LeetCode 435. Non-overlapping Intervals

435. Non-overlapping Intervals Description Submission Solutions Add to List Total Accepted: 7406 Total Submissions: 18525 Difficulty: Medium Contributors: love_FDU_llp Given a collection of intervals, find the minimum number of intervals you need to