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. 痞子衡嵌入式:飞思卡尔i.MX RTyyyy系列MCU外设那些事(2)- 善变的FlexRAM

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是飞思卡尔i.MX RTyyyy系列MCU的FlexRAM外设. 本文是外设系列第二篇,上一篇讲的是离内核最近的高速缓存L1 Cache, ...

  2. php thrift TServerSocket实现端口复用

    <?php namespace Message\Controller; use Think\Controller; use Thrift\Exception\TException; use Th ...

  3. 可复用且高度解耦的iOS用户统计实现

    http://www.cocoachina.com/ios/20160421/15912.html 本文为投稿文章,作者:编程小翁(简书) 用户统计 用户行为统计(User Behavior Stat ...

  4. OSGi Capabilities

    OSGi bundle的Capability就是这个bundle所具有的能力. 就像淘宝上的每个店铺一样,它会说明自己都卖哪些东西,也就是Provide-Capability 我们这些剁手党就会根据自 ...

  5. QLabel添加Click信号

    使用自定义label来实现此功能 其他控件可参照此例. #include "customerqlabel.h" CustomerQlabel::CustomerQlabel(QWi ...

  6. 链表经典题Merge Two Sorted Lists

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  7. Directx11教程(55) 建立球形和锥形物体

    原文:Directx11教程(55) 建立球形和锥形物体 本教程中,我们新建2个model class,SphereModelClass以及CylinderModelClass,分别用来表示球形和锥形 ...

  8. jquery鼠标悬停突出显示

    在线演示 本地下载

  9. ListView组件中 onEndReached 方法在滚动到距离列表最底部一半时执行

    初次使用ListView,在写列表滚动到最底部自动加载使用到方法onEndReached, 发现: ListView组件中 onEndReached 方法在滚动到距离列表最底部一半时执行, 于是翻看文 ...

  10. thinkphp php审核后返回信息给html

    1.die("<script>alert('至少选择一个收款方式!');history.back(-1);</script>");