题意:给出一些区间,求一个集合的长度要求每个区间里都至少有两个集合里的数。

解法:贪心或者差分约束。贪心的思路很简单,只要将区间按右边界排序,如果集合里最后两个元素都不在当前区间内,就把这个区间内的最后两个数加入集合,如果只有一个元素在区间里就加一个,如果两个元素都在区间里就不加。

差分约束系统用来解一个不等式组,只要这个不等式组里的不等式形如x1 - x2 <= c,c为常数就可以用差分约束来解不等式,将每个变量看做点,每个不等式看做边,求一个最短路,那么dis[i]就是不等式的一组解,主要利用的原理就是在最短路问题中:dis[v] <= dis[u] + edge[u][v],而将x1 - x2 <= c进行移项就可以得到以上形式,由于有负权,所以一般用bellmanford或者spfa……不过我不会写spfa……如果产生负环说明无解。

对于本题来说,将每个区间端点都看做点,sum[i]表示集合中小于等于i的数的个数,则对于一个区间[l, r]来说有不等式sum[r] - sum[l - 1] >= 2,转化成上述形式就是sum[l - 1] - sum[r] <= -2,可以看做是点r到点l-1的一条权值为-2的边。除了题中给出的条件,还有一个隐含条件为0 <= sum[i] - sum[i - 1] <= 1,用以上不等式建图后跑最短路,dis[n] - dis[0]即为答案。

差分约束比较复杂……但是主要想练差分约束才做的这题= =但是数据范围略大……跑bellmanford有点难……最后1000ms擦边过的……

代码:

贪心:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#include<stdlib.h>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#include<iomanip>
#define LL long long
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1 using namespace std; struct node
{
int l, r;
bool operator < (const node &tmp) const
{
if(r == tmp.r) return l < tmp.l;
return r < tmp.r;
}
}interval[10005];
int main()
{
int n;
while(~scanf("%d", &n))
{
for(int i = 0; i < n; i++)
scanf("%d%d", &interval[i].l, &interval[i].r);
sort(interval, interval + n);
int x = -1, y = -1;
int ans = 0;
for(int i = 0; i < n; i++)
{
if(interval[i].l <= x) continue;
if(interval[i].l <= y)
{
x = y;
y = interval[i].r;
ans++;
continue;
}
x = interval[i].r - 1;
y = interval[i].r;
ans += 2;
}
cout << ans << endl;
}
return 0;
}

  

差分约束系统:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#include<stdlib.h>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#include<iomanip>
#define LL long long
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1 using namespace std; struct node
{
int u, v, value;
node(int u, int v, int value) : u(u), v(v), value(value) {}
node() {}
}edge[30005];
int minn = 10000000, maxn, m, cnt;
int dis[10005];
const int inf = 0x3f3f3f3f;
void BellmanFord()//由于不存在无解的数据,不需要判负环
{
memset(dis, 0, sizeof dis);
dis[minn] = 0;
bool flag = true;
while(flag)//剪枝,如果本次没有进行松弛,则停止松弛
{
flag = false;
for(int j = 0; j < cnt; j++)
if(dis[edge[j].v] > dis[edge[j].u] + edge[j].value)
{
flag = true;
dis[edge[j].v] = dis[edge[j].u] + edge[j].value;
}
}
}
int main()
{
while(~scanf("%d", &m))
{
cnt = 0;
for(int i = 0; i < m; i++)
{
int a, b;
scanf("%d%d", &a, &b);
minn = min(minn, min(a, b + 1));//记录最小点
maxn = max(maxn, max(a, b + 1));//记录最大点
edge[cnt++] = node(b + 1, a, -2);
}
for(int i = minn + 1; i <= maxn; i++)
{
edge[cnt++] = node(i - 1, i, 1);
edge[cnt++] = node(i, i - 1, 0);
}
BellmanFord();
cout << dis[maxn] - dis[minn] << endl;
}
return 0;
}

  

POJ 1716 Integer Intervals的更多相关文章

  1. poj 1716 Integer Intervals(差分约束)

    1716 -- Integer Intervals 跟之前个人赛的一道二分加差分约束差不多,也是求满足条件的最小值. 题意是,给出若干区间,需要找出最少的元素个数,使得每个区间至少包含两个这里的元素. ...

  2. poj 1716 Integer Intervals (差分约束 或 贪心)

    Integer Intervals Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12192   Accepted: 514 ...

  3. POJ 1201 Intervals || POJ 1716 Integer Intervals 差分约束

    POJ 1201 http://poj.org/problem?id=1201 题目大意: 有一个序列,题目用n个整数组合 [ai,bi,ci]来描述它,[ai,bi,ci]表示在该序列中处于[ai, ...

  4. POJ 1716 Integer Intervals 差分约束

    题目:http://poj.org/problem?id=1716 #include <stdio.h> #include <string.h> #include <ve ...

  5. POJ 1716 Integer Intervals#贪心

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

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

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

  7. 【POJ 1201】 Intervals(差分约束系统)

    [POJ 1201] Intervals(差分约束系统) 11 1716的升级版 把原本固定的边权改为不固定. Intervals Time Limit: 2000MS   Memory Limit: ...

  8. 【38.24%】【POJ 1201】Intervals

    Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25902 Accepted: 9905 Description You are ...

  9. POJ No.3680 Intervals

    2016-06-01 22:01:39 题目链接: POJ No.3680 Intervals 题目大意: 给定N个带权区间,最多可以重复选一个点M次,求出一种选法使得所得权最大 解法: 费用流 建模 ...

随机推荐

  1. PHP7 扩展之自动化测试

    在安装 PHP7 及各种扩展的过程中,如果你是用源码安装,会注意到在 make 成功之后总会有一句提示:Don't forget to run 'make test'. 这个 make test 就是 ...

  2. 管理Java垃圾回收的五个建议

    [编者按]本文作者是Niv Steingarten,是Takipi 的联合创始人,热衷于编写优雅简洁的代码.作者通过对垃圾收集器的介绍和梳理,在管理垃圾回收方面提出了五个建议,降低收集器开销,帮助大家 ...

  3. linux sort 命令详解(转 )

    linux sort 命令详解 sort是在Linux里非常常用的一个命令,管排序的,集中精力,五分钟搞定sort,现在开始! 1 sort的工作原理 sort将文件的每一行作为一个单位,相互比较,比 ...

  4. HDOJ-1999 不可摸数

    不可摸数 转自:http://www.cnblogs.com/dongsheng/archive/2012/08/18/2645594.html Time Limit: 2000/1000 MS (J ...

  5. ZOJ1232 Adventure of Super Mario spfa上的dp

    很早之前听说有一种dp是在图上的dp,然后是在跑SPFA的时候进行dp,所以特地找了一题关于在SPFA的时候dp的. 题意:1~a是村庄 a+1~a+b是城堡,存在m条无向边.求由a+b->1的 ...

  6. HDU 1540 / POJ 2892 Tunnel Warfare (单点更新,区间合并,求包含某点的最大连续个数)

    题意:一条线上有n个点,D x是破坏这个点,Q x是表示查询x所在的最长的连续的点的个数,R是恢复上一次破坏的点. 思路:这题的关键是查询. 将被毁的村庄看成空位,当查询某个点的时候,如果我们知道它左 ...

  7. 毕向东JAVA视频讲解笔记(前三课)

    1,定义一个类,因为java程序都定义类中,java程序都是以类的形式存在的,类的形式其实就是一个字节码文件最终体现. 2,定义一个主函数.为了让该类可以独立运行. 3,因为演示hello world ...

  8. 进程内核栈、用户栈及 Linux 进程栈和线程栈的区别

    Linux 进程栈和线程栈的区别 http://www.cnblogs.com/luosongchao/p/3680312.html 总结:线程栈的空间开辟在所属进程的堆区,线程与其所属的进程共享进程 ...

  9. 如何配置svn服务器

    如果你已经安装好了VisualServer服务器,现在让我们一起来配置svn服务器吧. 工具/原料 VisualServer 配置VisualServer 找到VisualServer Manager ...

  10. 网上图书商城项目学习笔记-011Book模块查询(分页)

    一.流程分析 1.图书模块 2.分布分析 二.代码 1.view层 1)list.jsp <%@ page language="java" import="java ...