【poj1716】 Integer Intervals
http://poj.org/problem?id=1716 (题目链接)
题意
给出n个区间,要求取出最少数量的不同的自然数,使每个区间中至少包含2个取出的数。
Solution
差分约束。
运用前缀和,将问题转化为了一些不等式,然后建图连边跑SPFA最长路(因为是>=)即可,因为有负权所以用不了dijistra。就是poj1201的简化版。
代码
// poj1716
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#define LL long long
#define inf 2147483640
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std;
inline LL getint() {
int f,x=0;char ch=getchar();
while (ch<='0' || ch>'9') {if (ch=='-') f=-1;else f=1;ch=getchar();}
while (ch>='0' && ch<='9') {x=x*10+ch-'0';ch=getchar();}
return x*f;
} const int maxn=10010;
struct edge {int next,to,w;}e[maxn<<2];
int head[maxn],vis[maxn],dis[maxn],cnt,s,t,n; void link(int u,int v,int w) {
e[++cnt].to=v;e[cnt].next=head[u];head[u]=cnt;e[cnt].w=w;
}
void SPFA() {
queue<int> q;
while (q.size()) q.pop();
for (int i=s;i<=t;i++) {
vis[i]=1;
dis[i]=0;
q.push(i);
}
while (q.size()) {
int x=q.front();
q.pop();
vis[x]=0;
for (int i=head[x];i;i=e[i].next)
if (dis[e[i].to]<dis[x]+e[i].w) {
dis[e[i].to]=dis[x]+e[i].w;
if (!vis[e[i].to]) {
q.push(e[i].to);
vis[e[i].to]=1;
}
}
}
}
int main() {
scanf("%d",&n);
s=inf,t=0;
for (int x,y,i=1;i<=n;i++) {
scanf("%d%d",&x,&y);
link(x,y+1,2);
s=min(s,x);
t=max(t,y+1);
}
for (int i=s;i<=t;i++) link(i-1,i,0),link(i,i-1,-1);
SPFA();
printf("%d",dis[t]);
return 0;
}
【poj1716】 Integer Intervals的更多相关文章
- 【POJ 1716】Integer Intervals(差分约束系统)
		id=1716">[POJ 1716]Integer Intervals(差分约束系统) Integer Intervals Time Limit: 1000MS Memory L ... 
- 【POJ 1716】 Integer Intervals
		[题目链接] 点击打开链接 [算法] 差分约束系统 [代码] #include <algorithm> #include <bitset> #include <cctyp ... 
- 【leetcode】Merge Intervals
		Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example,Given ... 
- 【leetcode】Integer to Roman
		Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within t ... 
- 【leetcode】Merge Intervals(hard)
		Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ... 
- 【leetcode】Integer to Roman  & Roman to Integer(easy)
		Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ... 
- 【Leetcode】【Hard】Merge Intervals
		Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ... 
- 【leetcode】 Merge Intervals
		Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example,Given ... 
- 【LeetCode】Merge Intervals 题解 利用Comparator进行排序
		题目链接Merge Intervals /** * Definition for an interval. * public class Interval { * int start; * int e ... 
随机推荐
- Android配置----Eclipse+BlueStacks调试Android应用
			[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/3 ... 
- 第16章 Windows线程栈
			16.1 线程栈及工作原理 (1)线程栈简介 ①系统在创建线程时,会为线程预订一块地址空间(即每个线程私有的栈空间),并调拨一些物理存储器.默认情况下,预订1MB的地址空间并调拨两个页面的存储器. ② ... 
- Android应用程序签名详解 简介
			转自: http://blog.csdn.net/lyq8479/article/details/6401093 本文主要讲解Android应用程序签名相关的理论知识,包括:什么是签名.为什么要给应用 ... 
- Android 动态加载 (二) 态加载机制 案例二
			探秘腾讯Android手机游戏平台之不安装游戏APK直接启动法 重要说明 在实践的过程中大家都会发现资源引用的问题,这里重点声明两点: 1. 资源文件是不能直接inflate的,如果简单的话直接在程序 ... 
- python-基础案例
			范例一: 练习:元素分类 有如下值集合 [11,22,33,44,55,66,77,88,99,90...],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值 ... 
- gridControl控件动态绑定列
			DataTable dt = =Query.GetCustome=(ref customColumnCount); //绑定列 gridView.Columns.Add(}); gridView.Co ... 
- 理解SQL Server中的权限体系(下)----安全对象和权限
			原文:http://www.cnblogs.com/CareySon/archive/2012/04/12/SQL-Security-SecurableAndPermission.html 在开始阅读 ... 
- VS2010引用App_Code下的类文件问题解决方法
			原文连接:http://blog.csdn.net/zjlovety/article/details/7658528 VS2020中“添加ASP.NET文件夹”里没有App_Code,添加普通文件夹然 ... 
- yslow性能优化的35条黄金守则
			参考Best Practices for Speeding Up Your Web Site Exceptional Performance 团队总结了一系列优化网站性能的方法,分成了7个大类35条, ... 
- 【转】【PNG压缩工具】PNG 图像的优化及压缩工具介绍
			图像格式有许多种不同类型,在互联网上最常见的有JPEG.GIF.BMP.TIFF和PNG.每一种图像格式都有它自己的用途,比如GIF是用于动画的,JPEG是用于高清图片的,这种图片在保存或者调整大小后 ... 
