poj 1716 Integer Intervals (差分约束)
来源:互联网 发布:网络维护人员工作总结 编辑:程序博客网 时间:2024/06/05 15:59
Description
An integer interval [a,b], a < b, is a set of all consecutive integers beginning with a and ending with b.
Write a program that: finds the minimal number of elements in a set containing at least two different integers from each interval.
Input
The first line of the input contains the number of intervals n, 1 <= n <= 10000. Each of the following n lines contains two integers a, b separated by a single space, 0 <= a < b <= 10000. They are the beginning and the end of an interval.
Output
Output the minimal number of elements in a set containing at least two different integers from each interval.
Sample Input
4
3 6
2 4
0 2
4 7
Sample Output
4
题解
这题比考试题简单。。。轻松混过!跟着大佬的步伐水水水(不大佬几乎不做水题,只有这几道我能勉强做出来。。)
代码
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;struct Edge { int v, w, next;} e[200010];int n = 0, t = 0x3fffff, m, num = 0, a[100010], h[200010];void add(int u, int v, int w) { num ++; e[num].v = v; e[num].w = w; e[num].next = h[u]; h[u] = num;}int head = 0, tail = 1, vis[100010], dis[100010], queue[500010];void spfa(int s) { memset(dis, -63, sizeof(dis)); queue[1] = s; vis[s] = 1; dis[s] = 0; while(head < tail) { int u = queue[++ head]; vis[u] = false; for(int i = h[u]; i; i = e[i].next) { int v = e[i].v; if(dis[v] < dis[u] + e[i].w) { dis[v] = dis[u] + e[i].w; if(! vis[v]) { vis[v] = true; queue[++ tail] = v; } } } }}int main() { scanf("%d", &m); for(int i = 1; i <= m; i ++) { int l, r; scanf("%d %d", &l, &r); n = max(n, r + 1); t = min(t, l); add(l, r + 1, 2); } for(int i = t; i < n; i ++) add(i + 1, i, -1), add(i, i + 1, 0); spfa(t); printf("%d", dis[n]); return 0;}
阅读全文
0 0
- POJ--1716[Integer Intervals] 差分约束
- Poj 1716 Integer Intervals(差分约束)
- 【POJ 1716 Integer Intervals】+ 差分约束
- POJ 1716 Integer Intervals 差分约束
- poj 1716 -- Integer Intervals ( 差分约束 )
- POJ 1716 Integer Intervals 差分约束(入门)
- POJ - 1716 Integer Intervals(差分约束系统)
- 【POJ 1716】Integer Intervals(差分约束系统)
- POJ 1716 Integer Intervals(差分约束)
- POJ 1716 Integer Intervals(差分约束)
- poj 1716 Integer Intervals (差分约束)
- POJ 1716 Integer Intervals(差分约束)
- POJ 1201 Intervals || POJ 1716 Integer Intervals 差分约束
- poj 1716 Integer Intervals(差分约束系统)
- POJ 1716 Integer Intervals SPFA 差分约束
- poj 1716 Integer intervals (差分约束#3 /贪心)
- POJ 1716 Integer Intervals 差分约束系统
- poj 1716 Integer Intervals(差分约束||贪心)(中等)
- No bean named 'springSecurityFilterChain' is defined
- 深度学习 loss
- 选择排序
- hdu 2066 一个人的旅行 Dijkstra+优先队列优化
- JAVA笔记集合1
- poj 1716 Integer Intervals (差分约束)
- Rxjava学习(一)
- MySQL悲观锁总结和实践
- C/C++:面试题 int *ptr=(int *)(&a+1);
- Oracle学习之case when的用法
- 237. Delete Node in a Linked List
- NIO基础(1)-缓冲区用法
- BAT等大厂已开源的70个实用工具盘点(附下载地址)
- 2017.10.22