id=1716">【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 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

实训回来后的一血~~

差分约束系统,走前看了点,没搞透,做完这题略微有点明确了。

这题是差分约束系统入门题,关于差分约束系统。百度各种大牛博客讲的都非常具体。简单说就是通过不等关系建立约束系统图,然后跑最短路(大于关系则跑最长路)

回到此题,题目要求找出一个最小集合S,满足对于n个范围[ai,bi],S中存在两个及两个以上不同的点在范围内

令Zi表示满足条件的情况下。0~i点至少有多少点在集合内

则Zb-Za >= 2

仅仅有这一个条件构造出来的图可能不是全然连通的,所以须要找一些“隐含条件”

不难发现 对于相邻的点 0 <= Zi-Z(i-1) <= 1 保证关系符同样 转化为

Zi-Z(i-1) >= 0

Z(i-1)-Zi >= -1

用这三个关系,就可以构造差分约束系统,然后SPFA或者Bellman跑一趟最长路(满足全部条件)

代码例如以下:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue> using namespace std;
const int INF = 0x3f3f3f3f;
const int msz = 1e5;
const int mod = 1e9+7;
const double eps = 1e-8; struct Edge
{
int v,w,next;
}; Edge eg[233333];
int head[50050];
bool vis[50050];
int dis[50050];
int tp,st,en; void Add(int u,int v,int w)
{
eg[tp].v = v;
eg[tp].w = w;
eg[tp].next = head[u];
head[u] = tp++;
} int SPFA()
{
memset(vis,0,sizeof(vis));
memset(dis,-INF,sizeof(dis));
queue <int> q;
dis[st] = 0;
vis[st] = 1;
int u,v,w; q.push(st); while(!q.empty())
{
u = q.front();
q.pop();
vis[u] = 0;
for(int i = head[u]; i != -1; i = eg[i].next)
{
v = eg[i].v;
w = eg[i].w;
if(dis[v] < dis[u]+w)
{
dis[v] = dis[u]+w;
if(!vis[v])
{
q.push(v);
vis[v] = 1;
}
}
}
}
return dis[en];
} int main(int argc,char **argv)
{
int n;
int u,v; while(~scanf("%d",&n))
{
tp = 0;
memset(head,-1,sizeof(head)); en = 0,st = INF; while(n--)
{
scanf("%d%d",&u,&v);
Add(u,v+1,2);
//最小点做起点 最大点做终点
en = max(en,v+1);
st = min(st,u);
} for(int i = st; i < en; ++i)
{
Add(i,i+1,0);
Add(i+1,i,-1);
}
printf("%d\n",SPFA());
}
return 0;
}

【POJ 1716】Integer Intervals(差分约束系统)的更多相关文章

  1. poj 1716 Integer Intervals (差分约束 或 贪心)

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

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

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

  3. POJ 1716 Integer Intervals 差分约束

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

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

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

  5. POJ 1716 Integer Intervals

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

  6. POJ 1716 Integer Intervals#贪心

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

  7. POJ1201 Intervals差分约束系统(最短路)

    Description You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn. Write a p ...

  8. POJ 3169 Layout (差分约束系统)

    Layout 题目链接: Rhttp://acm.hust.edu.cn/vjudge/contest/122685#problem/S Description Like everyone else, ...

  9. Intervals(差分约束系统)

    http://poj.org/problem?id=1201 题意:给定n个整数闭区间[a,b]和n个整数c,求一个最小的整数集合Z,满足Z里边的数中范围在闭区间[a,b]的个数不小于c个. 思路:根 ...

  10. POJ1201 Intervals[差分约束系统]

    Intervals Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26028   Accepted: 9952 Descri ...

随机推荐

  1. Android开发——ThreadLocal功能介绍

    个静态的监听器对象,显然是无法接受的. 2.  使用实例 //首先定义一个ThreadLocal对象,选择泛型为Boolean类型 private ThreadLocal<Boolean> ...

  2. poj2217 Secretary 后缀数组

    #include <iostream> #include <cstring> #include <string> #include <cstdio> u ...

  3. 【JavaScript 1—基础知识点】:宏观概述

    导读:JavaScript是一门新的(也可以说是旧的或者半新语言),里面有很多的知识点都能和已有的知识产生共鸣.但是,虽然简单,相同点也有很多,也有不同点.我脑袋也不好使,所以对于我来说,还是有必要再 ...

  4. nginx的简介和配置文件实例(一)

    此文章配合 nginx配置文件解答    共同分享,了解. 一.nginx服务简介Nginx是一个高性能的HTTP和反向代理服务器 使用 Nginx 前必须了解的事项: 1)Nginx 本身只是一个 ...

  5. LightOJ——1066Gathering Food(BFS)

    1066 - Gathering Food   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB W ...

  6. POJ 2411 Mondriaan's Dream ——状压DP 插头DP

    [题目分析] 用1*2的牌铺满n*m的格子. 刚开始用到动规想写一个n*m*2^m,写了半天才知道会有重复的情况. So Sad. 然后想到数据范围这么小,爆搜好了.于是把每一种状态对应的转移都搜了出 ...

  7. Java中NIO、BIO、AIO相关概念及应用场景

    1.同步阻塞IO(JAVA BIO):同步并阻塞,服务器实现模式为一个连接一个线程,即客户端有连接请求时,服务器端就需要启动一个线程进行处理,如果这个连接不做任何事情会造成不必要的线程开销,当然可以通 ...

  8. 【译】NCCloud: Applying Network Coding for the Storage Repair in a Cloud-of-Clouds

    NCCloud:多云存储设备下存储修复的网络编码 Yuchong Hu, Henry C. H. Chen, Patrick P. C. Lee, Yang Tang  摘要:近年来的研究提出通过条带 ...

  9. 洛谷P2676 超级书架

    题目描述 Farmer John最近为奶牛们的图书馆添置了一个巨大的书架,尽管它是如此的大,但它还是几乎瞬间就被各种各样的书塞满了.现在,只有书架的顶上还留有一点空间. 所有N(1 <= N & ...

  10. NOJ 1116 哈罗哈的大披萨 【淡蓝】 [状压dp+各种优化]

    我只能说,珍爱生命,远离卡常数的题...感谢陈老师和蔡神,没有他们,,,我调一个星期都弄不出来,,,, 哈罗哈的大披萨 [淡蓝] 时间限制(普通/Java) : 1000 MS/ 3000 MS   ...