【POJ 1716】Integer Intervals(差分约束系统)
id=1716">【POJ 1716】Integer Intervals(差分约束系统)
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 13425 | Accepted: 5703 |
Description
Write a program that: finds the minimal number of elements in a set containing at least two different integers from each interval.
Input
interval.
Output
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(差分约束系统)的更多相关文章
- poj 1716 Integer Intervals (差分约束 或 贪心)
Integer Intervals Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12192 Accepted: 514 ...
- POJ 1201 Intervals || POJ 1716 Integer Intervals 差分约束
POJ 1201 http://poj.org/problem?id=1201 题目大意: 有一个序列,题目用n个整数组合 [ai,bi,ci]来描述它,[ai,bi,ci]表示在该序列中处于[ai, ...
- POJ 1716 Integer Intervals 差分约束
题目:http://poj.org/problem?id=1716 #include <stdio.h> #include <string.h> #include <ve ...
- poj 1716 Integer Intervals(差分约束)
1716 -- Integer Intervals 跟之前个人赛的一道二分加差分约束差不多,也是求满足条件的最小值. 题意是,给出若干区间,需要找出最少的元素个数,使得每个区间至少包含两个这里的元素. ...
- POJ 1716 Integer Intervals
题意:给出一些区间,求一个集合的长度要求每个区间里都至少有两个集合里的数. 解法:贪心或者差分约束.贪心的思路很简单,只要将区间按右边界排序,如果集合里最后两个元素都不在当前区间内,就把这个区间内的最 ...
- POJ 1716 Integer Intervals#贪心
(- ̄▽ ̄)-* //求一个集合,这个集合与任意一个区间的交集,需至少有两个数字 //贪心过程:按n个区间的最右值从小到大对区间进行排列, //集合首先取第一个区间的最右两个数字, //到第二个区间, ...
- POJ1201 Intervals差分约束系统(最短路)
Description You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn. Write a p ...
- POJ 3169 Layout (差分约束系统)
Layout 题目链接: Rhttp://acm.hust.edu.cn/vjudge/contest/122685#problem/S Description Like everyone else, ...
- Intervals(差分约束系统)
http://poj.org/problem?id=1201 题意:给定n个整数闭区间[a,b]和n个整数c,求一个最小的整数集合Z,满足Z里边的数中范围在闭区间[a,b]的个数不小于c个. 思路:根 ...
- POJ1201 Intervals[差分约束系统]
Intervals Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 26028 Accepted: 9952 Descri ...
随机推荐
- POJ 2486 树形背包DP Apple Tree
设d(u, j, 0)表示在以u为根的子树中至多走k步并且最终返回u,能吃到的最多的苹果. 则有状态转移方程: #include <iostream> #include <cstdi ...
- xfce-OpenVAS
OpenVAS开源风险评估系统部署方案 OpenVAS,即开放式漏洞评估系统,是一个用于评估目标漏洞的杰出框架.功能十分强大,最重要的是,它是“开源”的——就是免费的意思啦- 它与著名的Nessu ...
- ubuntu14.04:php7+apache2+mysql
apache2: sudo apt-get install apache2 apache2-dev service apache2 start mysql: sudo apt-get install ...
- [android开发篇]项目目录结构
- SPOJ - ADALIST,双端队列入门模板!
ADALIST - Ada and List 这道题时限6.5s,激动人心啊,好多人STL一顿乱写AC,哈哈,如果熟悉双端队列的话这道题其实是很水的. 题意:n个数的数列,然后接下来Q次操作,每次可以 ...
- mysqlbinlog备份和mysqldump备份
-bash : mysqldump: command not found -bash : mysqlbinlog:command not found 首先得知道mysql命令或mysqldump命令的 ...
- (绝对有用)iOS获取UUID,并使用keychain存储
原文链接 http://blog.sina.com.cn/s/blog_5971cdd00102vqgy.html UDID被弃用,使用UUID来作为设备的唯一标识.获取到UUID后,如果用NSUse ...
- BZOJ 4810 [Ynoi2017]由乃的玉米田 ——Bitset 莫队算法
加法和减法的操作都能想到Bitset. 然后发现乘法比较难办,反正复杂度已经是$O(n\log{n})$了 枚举因数也不能更差了,直接枚举就好了. #include <map> #incl ...
- (2015大作业)茹何优雅的手写正则表达式引擎(regular expression engine
貌似刚开学的时候装了个逼,和老师立了个flag说我要写个正则表达式引擎,然后学期末估计老师早就忘了这茬了,在历时3个月的懒癌发作下,终于在这学期末deadline的时候花了一个下午加晚上在没有网的房间 ...
- 数字串(codevs 1394)
题目描述 Description 给你一个长度为n的数字串,数字串里会包含1-m这些数字.如果连续的一段数字子串包含了1-m这些数字,则称这个数字字串为NUM串.你的任务是求出长度最短的NUM串是什么 ...