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. 17,时间模块 time,random模块

    表示时间的三种方式 在python中,通常有着三种方式来表示时间:时间戳,元祖,格式化的时间字符串: 1,时间戳(timestamp):通常来说时间戳表示的是从1970年1月1日00:00:00开始按 ...

  2. server 08 R2 NBL 报错:RPC 服务器在指定计算机上不可用

    排查步骤如下: 1.检查并确保 Remote Procedure Call (RPC) 和 Remote Procedure Call (RPC) Locator这两项服务是否都已经启动 2.确认此2 ...

  3. Leetcode 400.第n个数

    第n个数 在无限的整数序列 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...中找到第 n 个数字. 为整形范围内 ( n < 231). 示例 1: 输入: 3 输出 ...

  4. C# 方法冒号this的用法

    public Class1(string host, int port, string password = null):this() { this.Host=host; this.Port=port ...

  5. [Kubernetes]容器健康检查和恢复机制

    在Kubernetes中,可以为Pod里的容器定义一个健康检查探针(Probe),这样Kubernetes会根据这个Probe的返回值决定这个容器的状态,而不是直接以容器是否允许(来自Docker返回 ...

  6. HDU——1195Open the Lock(双向BFS)

    Open the Lock Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  7. BZOJ 3230 相似子串 ——后缀数组

    题目的Source好有趣. 我们求出SA,然后求出每一个后缀中与前面本质不同的字符串的个数. 然后二分求出当前的字符串. 然后就是正反两次后缀数组求LCP的裸题了. 要注意,这时两个串的起点可能会相同 ...

  8. BZOJ 2140 稳定婚姻 ——二分图

    论二分图的可行边与必须边. 考虑用dinic增广之后的图,一些是必要的割边,一些是可行的割边. 我们首先求出一组可行的最大匹配,那么这些变都是可行的. 然后我们求一遍强连通分量. 如果 scc[u]! ...

  9. 算法复习——虚树(消耗战bzoj2286)

    题目: Description 在一场战争中,战场由n个岛屿和n-1个桥梁组成,保证每两个岛屿间有且仅有一条路径可达.现在,我军已经侦查到敌军的总部在编号为1的岛屿,而且他们已经没有足够多的能源维系战 ...

  10. Snmp的学习总结(二)

    一.SNMP简介 SNMP指的是简单网络管理协议.它属于TCP/IP五层协议中的应用层协议.它提供了一种简单和方便的模式来管理网络中的各个元素.这里的元素就是各个被管理的对象,可以是因特网中的某个硬件 ...