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. 一篇RxJava友好的文章(二)

    上一篇文章介绍了rxjava的基本用法,和一些常用的操作符,以及rxjava的链式操作带来的好处.由于rxjava非常的强大,让我如此的痴迷,我打算写五篇文章,专门讲解rxjava 常见的操作符和用法 ...

  2. C# CheckBoxList 实现全选/反选功能怎么写?

    首先我们用RadioButtonList控件,且必须包含OnSelectedIndexChanged事件和AutoPostBack=‘true’属性, <asp:LinkButton ID=&q ...

  3. MySQL - Linux下安装

    本安装方式仅对5.7.21版本负责. 下载地址:wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.21-linux-glibc2 ...

  4. django+xadmin在线教育平台(十二)

    6-4 用form实现登录-1 上面我们的用户登录的方法是基于函数来做的.本节我们做一个基于类方法的版本. 要求对类的继承有了解. 基础教程中基本上都是基于函数来做的,其实更推荐基于类来做.基于类可以 ...

  5. 39条常见的Linux系统简单面试题

    39条常见的Linux系统简单面试题 本文主要分享39条常见的Linux系统简单面试题,其中包括如何看当前Linux系统有几颗物理CPU和每颗CPU的核数.如何实时查看网卡流量为多少等等,希望对你有所 ...

  6. 统计寄存器AX中1 的个数

    ;==================================== ; 统计寄存器AX中1 的个数 DATAS segment DATAS ends CODES segment START: ...

  7. 【文件处理】RandomAccessFile

    一,RandomAccessFile的用途: 使用RandomAccessFile的最大好处在于,一般的InputStream和OutputStream类对于文件都是顺序读取的,不能跳跃读取数据.而R ...

  8. 15,Flask-Script

    Flask-Script 从字面意思上来看就是 Flask 的脚本 是的,熟悉Django的同学是否还记得Django的启动命令呢? python manager.py runserver 大概是这样 ...

  9. Erlang OTP设计原则Gen_Fsm行为[转]

    转自: http://www.cnblogs.com/yourihua/archive/2012/05/13/2497776.html 1. Fsm 称为 有限状态机,举个例子,游戏中的怪物称为NPC ...

  10. 《Cracking the Coding Interview》——第18章:难题——题目12

    2014-04-29 04:36 题目:最大子数组和的二位扩展:最大子矩阵和. 解法:一个维度上进行枚举,复杂度O(n^2):另一个维度执行最大子数组和算法,复杂度O(n).总体时间复杂度为O(n^3 ...