Integer Intervals
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 12192   Accepted: 5145

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

 
参考: http://blog.csdn.net/lyy289065406/article/details/6648679
 

贪心算法的思想我没证明,是参考别人的 ,不过不知道代码为什么没有过..所以就不贴出来了

差分约束的算法我自己也想了一下,其实并不难,不知道为什么老是错..可能poj的数据比较大吧

所以代码我也是参考别人的,自己的改了一段时间还没改好就没改了,贴一下:

 //Accepted    324K    297MS    C++    1088B    2013-12-01 22:11:55
/* 题意:
给出n个区间[ai,bi],求在每个区间内最少有两个数的一组数 差分约束:
S[ai - 1] <= S[bi] - 2
S[i] <= S[i - 1] + 1
S[i - 1] <= S[i] 自己看着建吧.. */
#include<iostream>
#include<queue>
#include<string.h>
#define N 10005
#define inf 0x7ffffff
using namespace std;
struct node{
int s,e;
}edge[N];
int d[N];
int n,up,down;
void bellman_ford()
{
memset(d,,sizeof(d));
int flag=;
while(flag){
flag=;
for(int i=;i<n;i++)
if(d[edge[i].s]>d[edge[i].e]-){
d[edge[i].s]=d[edge[i].e]-;
flag=;
}
for(int i=down;i<up;i++)
if(d[i+]>d[i]+){
d[i+]=d[i]+;
flag=;
}
for(int i=up-;i>=down;i--)
if(d[i]>d[i+]){
d[i]=d[i+];
flag=;
}
}
}
int main(void)
{
while(cin>>n)
{
up=;
down=n;
for(int i=;i<n;i++){
int a,b;
cin>>a>>b;
edge[i].s=a;
edge[i].e=b+;
if(down>a) down=a;
if(up<b+) up=b+;
}
bellman_ford();
cout<<d[up]-d[down]<<endl;
}
return ;
}

poj 1716 Integer Intervals (差分约束 或 贪心)的更多相关文章

  1. POJ 1201 Intervals || POJ 1716 Integer Intervals 差分约束

    POJ 1201 http://poj.org/problem?id=1201 题目大意: 有一个序列,题目用n个整数组合 [ai,bi,ci]来描述它,[ai,bi,ci]表示在该序列中处于[ai, ...

  2. POJ 1716 Integer Intervals 差分约束

    题目:http://poj.org/problem?id=1716 #include <stdio.h> #include <string.h> #include <ve ...

  3. poj 1716 Integer Intervals(差分约束)

    1716 -- Integer Intervals 跟之前个人赛的一道二分加差分约束差不多,也是求满足条件的最小值. 题意是,给出若干区间,需要找出最少的元素个数,使得每个区间至少包含两个这里的元素. ...

  4. POJ 1716 Integer Intervals

    题意:给出一些区间,求一个集合的长度要求每个区间里都至少有两个集合里的数. 解法:贪心或者差分约束.贪心的思路很简单,只要将区间按右边界排序,如果集合里最后两个元素都不在当前区间内,就把这个区间内的最 ...

  5. POJ 1716 Integer Intervals#贪心

    (- ̄▽ ̄)-* //求一个集合,这个集合与任意一个区间的交集,需至少有两个数字 //贪心过程:按n个区间的最右值从小到大对区间进行排列, //集合首先取第一个区间的最右两个数字, //到第二个区间, ...

  6. poj 1201 Intervals(差分约束)

    题目:http://poj.org/problem?id=1201 题意:给定n组数据,每组有ai,bi,ci,要求在区间[ai,bi]内至少找ci个数, 并使得找的数字组成的数组Z的长度最小. #i ...

  7. poj 1201 Intervals——差分约束裸题

    题目:http://poj.org/problem?id=1201 差分约束裸套路:前缀和 本题可以不把源点向每个点连一条0的边,可以直接把0点作为源点.这样会快许多! 可能是因为 i-1 向 i 都 ...

  8. POJ 2101 Intervals 差分约束

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 27746   Accepted: 10687 Description You ...

  9. POJ 3159 Candies(差分约束,最短路)

    Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 20067   Accepted: 5293 Descrip ...

随机推荐

  1. SAP库存历史库存表更新逻辑 (转)

    根据库存类型的不同,库存信息保存在不同的表中,具体而言见下表 库存类型 当前库存 历史库存 库存金额 历史库存金额 工厂级别库存 MARC MARCH MBEW MBEWH MBEW 库存地点库存 M ...

  2. Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: Pointcut is not well-formed: expecting 'name pattern' at character position 36

    例: <aop:config> <aop:pointcut expression="execution(* com.zsn.Service.Impl.*.*(..))&qu ...

  3. java后台输入数据的2种方式

    java后台输入数据的2种方式 (1) import java.io.BufferedReader; import java.io.InputStreamReader; public class 输入 ...

  4. 利用python在windows环境下爬取赶集网工作信息。

    主要用到了多进程和多线程的知识,最后结果保存成csv文件格式,如有需要可改成数据库版本. 对用到的库做下简要介绍,具体请参考官方文档: xpinyin.Pinyin:将输入的中文转成拼音 concur ...

  5. AWS CentOS7 实例修改主机名

    问题描述: AWS EC2 实例在升级到CentOS7以后,我们发现主机名的修改不再像之前的版本(CentOS 5/6)一样简单. 每次新建实例之后,修改好主机名,重启或者克隆之后的机器,主机名还是会 ...

  6. vue数据绑定html

    html标签的纯文本显示/被当做html标签处理: 1)使用两个大括号时,假如字符串内容是html标签,那么不会被转义: 2)使用三个大括号时,字符串内的html标签会被直接转义 a.两个大括号: & ...

  7. JSON后台处理特殊字符方法,在JSONArray.fromObject转换时处理

    /** * 替换一个字符串中的某些指定字符 * @param strData String 原始字符串 * @param regex String 要替换的字符串 * @param replaceme ...

  8. Delphi中DLL的创建和使用(转)

    Delphi中DLL的创建和使用     1.DLL简介:   2.调用DLL:   3.创建DLL:   4.两个技巧:   5.初始化:   6.例外处理.            1.DLL简介  ...

  9. 01,jupyter环境安装

    jupyter notebook环境安装 一.什么是Jupyter Notebook? 1. 简介 Jupyter Notebook是基于网页的用于交互计算的应用程序.其可被应用于全过程计算:开发.文 ...

  10. Android Stadio 导入moudle 不显示

    Android Stadio 导入moudle 不显示,moudle 里面的java类也没有识别,只当是普通的txt文件. 后来,我发现,每个moudle 都有一个.iml 文件~ 然后我就随便翻翻配 ...