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 of elements in a set
containing at least two different integers from each interval.

Input

The
first line of the input contains the number of intervals n, 1 <= n
<= 10000. Each of the following n lines contains two integers a, b
separated by a single space, 0 <= a < b <= 10000. They are the
beginning and the end of an interval.

Output

Output the minimal number of elements in a set containing at least two different integers from each interval.

Sample Input

4
3 6
2 4
0 2
4 7

Sample Output

4

Source

CEOI 1997

题目和POJ1201相同。

由于这次区间内的数的个数固定为2,所以可以使用贪心解法。将区间按右端点从小到大排序,每次能不加点就不加,必须加的话就加在区间最右面,并累计答案数。

 1 /**/
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cmath>
 5 #include<cstring>
 6 #include<algorithm>
 7 using namespace std;
 8 const int mxn=50000;
 9 struct area{
10     int l,r;
11 }a[mxn];
12 int n;
13 int ans,f,s;
14 int cmp(area a,area b){
15     if(a.r!=b.r)return a.r<b.r;
16     return a.l<b.l;
17 }
18 int main(){
19     scanf("%d",&n);
20     int i,j;
21     for(i=1;i<=n;i++)scanf("%d%d",&a[i].l,&a[i].r);
22     sort(a+1,a+n+1,cmp);
23     ans=2;f=a[1].r;s=a[1].r-1;
24     for(i=2;i<=n;i++){
25         if(f<a[i].l){
26             ans+=2;
27             f=a[i].r;
28             s=a[i].r-1;
29             continue;
30         }
31         if(s<a[i].l){
32             ans++;
33             if(f>a[i].r-1)    s=a[i].r-1;
34             else s=f;
35             f=a[i].r;
36             continue;
37         }
38         if(f>a[i].r)f=a[i].r;
39     }
40     printf("%d\n",ans);
41     return 0;
42 }
时间: 2024-10-11 23:53:32

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 贪心

#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 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

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

【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 - 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 1716 Integer Intervals#贪心

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

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

图论专题整理

poj1251 Jungle Roads 思路:最小生成树          解题报告Here CodeForces 472D Design Tutorial: Inverse the Problem 思路:最小生成树          解题报告Here poj1789 Truck History 思路:最小生成树          解题报告Here poj1639 Picnic Planning 思路:顶点度数限制的MST         解题报告Here poj1062 昂贵的聘礼 思路:S