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中的zipfile

    zipfile - Work with ZIP archives ZipFile.namelist() Return a list of archive members by name. 返回压缩成员 ...

  2. ionic 命令cordova

    安装android platform : ionic platform add android 安装一维码cordova插件 :cordova plugin add https://github.co ...

  3. 1816: [Cqoi2010]扑克牌

    Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 2737  Solved: 1082[Submit][Status][Discuss] Descripti ...

  4. 【学时总结】◆学时·VIII◆ 树形DP

    ◆学时·VIII◆ 树形DP DP像猴子一样爬上了树……QwQ ◇ 算法概述 基于树的模型,由于树上没有环,满足DP的无后效性,可以充分发挥其强大统计以及计算答案的能力. 一般来说树形DP的状态定义有 ...

  5. CodeForces_864_bus

    C. Bus time limit per test 2 seconds memory limit per test 256 megabytes input standard input output ...

  6. 基于centos7实现的ftp

    前言 FTP(File transfer Protocl),文件传输协议,用于在网络上进行文件传输的一套标准协议,使用客户/服务器模式,属于网络传输协议的应用层.FTP服务运行在TCP/21和20端口 ...

  7. C#基础-hashtable,泛型和字典集合

    hashtable 的存储方式 使用方法: 1.引入包含Hashtable的命名空间 using System.Collections; // 引入Hash所在的命名空间 2.往hash表里面添加数据 ...

  8. PHP常用180函数总结

    数学函数 1.abs(): 求绝对值 <span style="font-size: 14px;">$abs = abs(-4.2); //4.2<br>& ...

  9. IntelliJ IDEA 12详细开发教程(一)思想的转变与新手入门【转】

    转载地址:http://bangqu.com/alicas/blog/433 从事软件开发工作以来,提高自己的开发效率,提高自己编码的规范,提高编码深度层次,这三样一直都是自己努力去追求的事情. 最近 ...

  10. 【PHP】Maximum execution time of 30 seconds exceeded解决办法

    Maximum execution time of 30 seconds exceeded,今天把这个错误的解决方案总结一下: 简单总结一下解决办法: 报错一:内存超限,具体报错语句忘了,简单说一下解 ...