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. Python 文件访问模式

    f = open('xxx文件', '访问模式') r    以只读方式打开文件(read).文件的指针将会放在文件的开头.默认模式. w   打开一个文件只用于写入(write).如果该文件已存在则 ...

  2. JavaScript的变量命名规则和关键字的介绍

    变量的名字 就像 人的名字一样,不能乱起.          你的代码不是只有你一个人看,变量既然是名字,那就这个名字就要有特殊的意义:     举个栗子:翠花,我们能从这个名字中得到什么信息?(这个 ...

  3. poj_3696_The Luckiest number

    Chinese people think of '8' as the lucky digit. Bob also likes digit '8'. Moreover, Bob has his own ...

  4. DB设计工具——dbschema

      Preface       I've got a db design job about meeting room booking system last week.There're many s ...

  5. Linux关闭开启防火墙命令

    在外部访问CentOS中部署应用时,需要关闭防火墙. 关闭防火墙命令:systemctl stop firewalld.service 开启防火墙:systemctl start firewalld. ...

  6. Centos7-Mysql-5.6.41一主两从的搭建

    01.准备工作 首先的前提条件你必须安装了mysql,而且知道你安装mysql配置文件的位置,接下来的事情就好办了. 我的搭建环境: 服务器1: 10.233.17.20 mysql-master(主 ...

  7. html页面 加载完成后再刷新 一次

    主要用于第一次加载页面有部分加载bug,再刷新一次即可正常运行. 简单粗暴直接上代码,不带参数,0影响 <Script>function refresh(){ url = location ...

  8. flask项目实战--论坛

    项目结构搭建 1:用pycharm创建flask bbs项目 2:分别创建config.py.exts.py.models.py.manage.py文件 创建一个apps包存放前台,后台,公共的模块 ...

  9. CF797E. Array Queries

    a is an array of n positive integers, all of which are not greater than n. You have to process q que ...

  10. 1,Python常用库之一:Numpy

    Numpy支持大量的维度数组和矩阵运算,对数组运算提供了大量的数学函数库! Numpy比Python列表更具优势,其中一个优势便是速度.在对大型数组执行操作时,Numpy的速度比Python列表的速度 ...