【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 ...
随机推荐
- java 21 - 13 IO流之 合并流
SequenceInputStream :表示其他输入流的逻辑串联. 构造方法摘要 SequenceInputStream(Enumeration<? extends InputStream&g ...
- Java虚拟机工作原理详解 ( 二 )
首先这里澄清两个概念:JVM实例和JVM执行引擎实例,JVM实例对应了一个独立运行的Java程序,而JVM执行引擎实例则对应了属于用户运行程序的线程:也就是JVM实例是进程级别,而执行引擎是线程级别的 ...
- 判断变量是否为json对象
var m ={a:'A'}; if(typeof m == 'object' && JSON.stringify(m).indexOf('{') == 0){//判断变量m是不是js ...
- 本地ip被劫持,初始化hosts文件,及其作用与说明
# Copyright (c) 1993-2009 Microsoft Corp.## This is a sample HOSTS file used by Microsoft TCP/IP for ...
- 层叠上下文(The stacking context)
MDNThe stacking context 层叠上下文是HTML元素的三维概念,这些HTML元素在一条假想的相对于面向(电脑屏幕的)视窗或者网页的用户的z轴上延伸,HTML元素依据其自身属性按照优 ...
- f2fs解析(二)f2fs写checkpoint时如何冻住整个文件系统
函数write_checkpoint中,会调用block_operations,函数中有这样一段代码: retry_flush_dents: f2fs_lock_all(sbi); /* write ...
- 继续Wcf记录点滴
之前说wcf以tcp协议作为通信方式的话会出现很多奇怪的bug,今天我把自己遇到的比较特殊的一个exception和解决方案列出来.主要是自己记录一下,顺便方便遇到这个问题的有缘人吧!废话不多说直接上 ...
- 挂多个class还是新建class —— 多用组合,少用继承
用css实现下面的效果图. 方案一 <style type="text/css"> .myList1 { border: 1px solid #333; padding ...
- benchmark
redis benchmark How many requests per second can I get out of Redis? Using New Relic to Understand R ...
- 使用WITH AS提高性能简化嵌套SQL(转)
http://www.cnblogs.com/fygh/archive/2011/08/31/2160266.html 一.WITH AS的含义 WITH AS短语,也叫做子查询部分(subquery ...