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的更多相关文章

  1. 【POJ 1716】Integer Intervals(差分约束系统)

    id=1716">[POJ 1716]Integer Intervals(差分约束系统) Integer Intervals Time Limit: 1000MS   Memory L ...

  2. 【POJ 1716】 Integer Intervals

    [题目链接] 点击打开链接 [算法] 差分约束系统 [代码] #include <algorithm> #include <bitset> #include <cctyp ...

  3. 【leetcode】Merge Intervals

    Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example,Given  ...

  4. 【leetcode】Integer to Roman

    Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within t ...

  5. 【leetcode】Merge Intervals(hard)

    Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...

  6. 【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 ...

  7. 【Leetcode】【Hard】Merge Intervals

    Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...

  8. 【leetcode】 Merge Intervals

    Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example,Given  ...

  9. 【LeetCode】Merge Intervals 题解 利用Comparator进行排序

    题目链接Merge Intervals /** * Definition for an interval. * public class Interval { * int start; * int e ...

随机推荐

  1. java 21 - 1 IO流中的字符流概述

    字节流通过读取一个byte数组的方式可以读取中文的,但是有可能出现小问题,所以,读取中文最好是用字符流. 字符流: 字符流=字节流+编码表. 编码表: 由字符及其对应的数值组成的一张表 编码表介绍: ...

  2. lcx转发 【解决内网没法链接3389 的问题】

    要求我自己在外网 然后监听1111端口,将1111端口数据流量转发至2222端口 被入侵主机上 将本地的2222端[愿3389 主机修改了远程连接的端口]口流量转发至外网ip的1111端口 2222为 ...

  3. RecyclerView (一) 基础知识

    RecyclerView是什么? RecyclerView是一种新的视图组,目标是为任何基于适配器的视图提供相似的渲染方式.它被作为ListView和GridView控件的继承者,在最新的suppor ...

  4. 常用的adb命令

    在平时的工作中,会经常用到adb命令,在这里稍微整理了一下. 一.概要 1.什么是adb? adb全称为Android Debug Bridge,就是起到调试桥的作用.顾名思义,adb就是一个debu ...

  5. Entity Framework4.0 (一)概述(EF4 的Database First方法)

    转自:http://www.cnblogs.com/marksun/archive/2011/12/15/2289582.html Entity Framework4.0(以后简称:EF4),是Mic ...

  6. Sublime 将 Tab 转为空格

    最近在使用 vue-cli 搭建项目,但每次用 Hbuilder 编写 vue 文件的时候,如果存在<script>部分就会报错,错误信息大意是说空格有问题.仔细研究了之后才知道,这是因为 ...

  7. 待整理-coredump

    Linux下如何产生coredump(gdb调试用) 任务发生异常,需要记录遗言信息,利用gdb调试,因此需要记录coredump文件.设置查看:在root用户下执行sysctl -a | grep ...

  8. JAVA NIO是什么(zz)

    JAVA NIO是什么? 1.   基本 概念 IO 是主存和外部设备 ( 硬盘.终端和网络等 ) 拷贝数据的过程. IO 是操作系统的底层功能实现,底层通过 I/O 指令进行完成. 所有语言运行时系 ...

  9. Linux第三次学习笔记

    #信息的表示和处理 三种重要的数字表示 1. 无符号数编码: 基于传统的二进制表示法,表示大于或者等于零的数字. 2. 补码编码: 表示有符号数整数的最常见的方式,有符号数就是只可 以为正或者为负的数 ...

  10. Linux 读书笔记 二

    一.实验说明 1. 环境登录 无需密码自动登录,系统用户名shiyanlou,密码shiyanlou 若不小心登出后,直接刷新页面即可 2. 环境使用 完成实验后可以点击桌面上方的“实验截图”保存并分 ...