1716 -- Integer Intervals

  跟之前个人赛的一道二分加差分约束差不多,也是求满足条件的最小值。

  题意是,给出若干区间,需要找出最少的元素个数,使得每个区间至少包含两个这里的元素。

  做法就是建立(b)->(a)=-2,(i)->(i+1)=1,(i+1)->(i)=0的边,然后跑一次spfa即可。

  做完的时候,因为队列开太小,所以re了一次。

代码如下:

 #include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring> using namespace std; const int N = ;
struct Edge {
int t, nx, c;
Edge() {}
Edge(int t, int nx, int c) : t(t), nx(nx), c(c) {}
} edge[N << ];
int eh[N], ec; void init() {
memset(eh, -, sizeof(eh));
ec = ;
} void addedge(int s, int t, int c) {
edge[ec] = Edge(t, eh[s], c);
eh[s] = ec++;
} int q[N << ], dis[N];
bool vis[N]; int spfa(int s, int t) {
memset(vis, , sizeof(vis));
memset(dis, , sizeof(dis));
int qh, qt, cur;
qh = qt = ;
q[qt++] = s;
vis[s] = true;
dis[s] = ;
while (qh < qt) {
//cout << cur << endl;
cur = q[qh++];
vis[cur] = false;
for (int i = eh[cur]; ~i; i = edge[i].nx) {
//cout << i << endl;
Edge &e = edge[i];
if (dis[e.t] > dis[cur] + e.c) {
dis[e.t] = dis[cur] + e.c;
q[qt++] = e.t;
vis[e.t] = true;
}
}
}
return dis[t];
} int main() {
int n, x, y;
while (~scanf("%d", &n)) {
init();
int mn = , mx = -;
for (int i = ; i < n; i++) {
scanf("%d%d", &x, &y);
addedge(y + , x, -);
mn = min(mn, x);
mx = max(mx, y + );
}
for (int i = ; i <= ; i++) {
addedge(i, i + , );
addedge(i + , i, );
}
printf("%d\n", -spfa(mx, mn));
}
return ;
}

——written by Lyon

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

    题意:给出一些区间,求一个集合的长度要求每个区间里都至少有两个集合里的数. 解法:贪心或者差分约束.贪心的思路很简单,只要将区间按右边界排序,如果集合里最后两个元素都不在当前区间内,就把这个区间内的最 ...

  5. POJ 1716 Integer Intervals#贪心

    (- ̄▽ ̄)-* //求一个集合,这个集合与任意一个区间的交集,需至少有两个数字 //贪心过程:按n个区间的最右值从小到大对区间进行排列, //集合首先取第一个区间的最右两个数字, //到第二个区间, ...

  6. poj 1201 Intervals(差分约束)

    题目:http://poj.org/problem?id=1201 题意:给定n组数据,每组有ai,bi,ci,要求在区间[ai,bi]内至少找ci个数, 并使得找的数字组成的数组Z的长度最小. #i ...

  7. poj 1201 Intervals——差分约束裸题

    题目:http://poj.org/problem?id=1201 差分约束裸套路:前缀和 本题可以不把源点向每个点连一条0的边,可以直接把0点作为源点.这样会快许多! 可能是因为 i-1 向 i 都 ...

  8. POJ 2101 Intervals 差分约束

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 27746   Accepted: 10687 Description You ...

  9. POJ 3159 Candies(差分约束,最短路)

    Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 20067   Accepted: 5293 Descrip ...

随机推荐

  1. Orleans 整体介绍

    背景 Orleans 是微软开源的Actor模型开发框架. Actor模型 此模型解决了并发编程时对资源竞争使用的问题,将对同一个业务数据的访问从并行变为串行执行,降低了多线程编程的难度,使普通编程人 ...

  2. Leetcode598.Range Addition II范围求和2

    给定一个初始元素全部为 0,大小为 m*n 的矩阵 M 以及在 M 上的一系列更新操作. 操作用二维数组表示,其中的每个操作用一个含有两个正整数 a 和 b 的数组表示,含义是将所有符合 0 < ...

  3. git操作github指令

    常用git命令: $ git clone  //本地如果无远程代码,先做这步,不然就忽略 $ cd //定位到你blog的目录下 $ git status //查看本地自己修改了多少文件 $ git ...

  4. sublime3安装javascript控制台环境 方法1

    Sublime Text 3    http://www.sublimetext.com/3 node.js        http://nodejs.cn/download 1.安装完成后. 2.打 ...

  5. webstorm/phpstorm破解版教程网址

    http://idea.lanyus.com/ http://www.php.cn/tool/phpstorm/408348.html 如果正版到期了,重新安装不能再次免费试用的话,之后我就用老版的w ...

  6. 【JZOJ3636】【BOI2012】Mobile(mobile)

    Mission 著名的手机网络运营商Totalphone 修建了若干基站收发台,以用于把信号网络覆盖一条新建的高速公路.因为Totalphone 的程序员总是很马虎的,所以,基站的传功功率不能独立设置 ...

  7. Web编译器Visual Studio扩展

    原文地址:https://marketplace.visualstudio.com/items?itemName=MadsKristensen.WebCompiler 一个Visual Studio扩 ...

  8. DirectX11笔记(四)--渲染管线

    原文:DirectX11笔记(四)--渲染管线 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u010333737/article/details/ ...

  9. 电影的微信小程序

    最近,工作没有那么忙,学习了一下小程序开发,感觉上手比较简单. 在项目中学习是最好的方式,于是就自己模仿豆瓣电影开发一款微信小程序版的豆瓣电影 准备工作: 数据来源:豆瓣电影API 功能: 电影榜单列 ...

  10. typora 使用

    菜单 输入+换行键,产生标题,自动更新 [toc] [TOC] 段落 按换行键建立新的一行可在行尾插入打断线,禁止向后插入 按换行键建立新的一行<br/> 标题 开头#的个数表示,空格+文 ...