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.left) return true;
	return false;
}
int main()
{
	int n,sum,l,r;
	while(scanf("%d",&n)!=EOF)
	{
		memset(&c,0,sizeof(&c));
		for(int i=0;i<n;i++)
		scanf("%d %d",&c[i].left,&c[i].right);
		sort(c,c+n,cmp);
		l=c[0].right-1,r=c[0].right;
		sum=2;
		for(int i=1;i<n;i++)//贪心策略 每次总是和集合的倒数第二个数比较就ok了。。第一次比较傻 总是和倒数第一个数-1比较。。。很明显错了
		{
			if(c[i].left<=l)//如果小于等于 不加
			continue;
			else if(c[i].left<=r)//如果大于倒数第二 小于倒数第一+1
			l=r,r=c[i].right,sum++;
			else//如果大于倒数第一
			l=c[i].right-1,r=c[i].right,sum+=2;
		}
		printf("%d\n",sum);
	}
	return 0;
}

时间: 2024-10-12 12:28:27

poj1716 Integer Intervals 贪心的相关文章

poj1716 Integer Intervals(差分约束)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Integer Intervals Time Limit: 1000MS   Memory Limit: 10000K Description An integer interval [a,b], a < b, is a set of all consecutive integers beginning with a and ending with b. Write a prog

POJ1716 Integer Intervals

Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13984   Accepted: 5943 Description An integer interval [a,b], a < b, is a set of all consecutive integers beginning with a and ending with b. Write a program that: finds the minimal number

POJ 1716 Integer Intervals#贪心

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

poj 1716 Integer Intervals

 Integer Intervals http://poj.org/problem?id=1716 Time Limit: 1000MS   Memory Limit: 10000K       Description An integer interval [a,b], a < b, is a set of all consecutive integers beginning with a and ending with b. Write a program that: finds the m

【POJ 1716】Integer Intervals(差分约束系统)

[POJ 1716]Integer Intervals(差分约束系统) Integer Intervals Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13425   Accepted: 5703 Description An integer interval [a,b], a < b, is a set of all consecutive integers beginning with a and ending w

【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

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 th

POJ - 1716 Integer Intervals(差分约束系统)

题目大意:给出N个区间,要求你找出M个数,这M个数满足在每个区间都至少有两个不同的数 解题思路:还是不太懂差分约束系统,数学不太好 借鉴了别人的思路,感觉有点DP思想 设d[i]表示[0,i-1]这个区间有d[i]个数满足要求 则给定一个区间[a,b],就有d[b + 1] - d[a] >= 2(b + 1是因为b也算在区间内) 将其转换为d[a] - d[b + 1] <= -2,这是第一个式子 接着在区间[i,i+1]中满足 d[i + 1] - d[i] >= 0 转换为 d[i

poj分类解题报告索引

图论 图论解题报告索引 DFS poj1321 - 棋盘问题 poj1416 - Shredding Company poj2676 - Sudoku poj2488 - A Knight's Journey poj1724 - ROADS(邻接表+DFS) BFS poj3278 - Catch That Cow(空间BFS) poj2251 - Dungeon Master(空间BFS) poj3414 - Pots poj1915 - Knight Moves poj3126 - Prim