POJ 1716 Integer Intervals
题意:给出一些区间,求一个集合的长度要求每个区间里都至少有两个集合里的数。
解法:贪心或者差分约束。贪心的思路很简单,只要将区间按右边界排序,如果集合里最后两个元素都不在当前区间内,就把这个区间内的最后两个数加入集合,如果只有一个元素在区间里就加一个,如果两个元素都在区间里就不加。
差分约束系统用来解一个不等式组,只要这个不等式组里的不等式形如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的更多相关文章
- poj 1716 Integer Intervals(差分约束)
1716 -- Integer Intervals 跟之前个人赛的一道二分加差分约束差不多,也是求满足条件的最小值. 题意是,给出若干区间,需要找出最少的元素个数,使得每个区间至少包含两个这里的元素. ...
- poj 1716 Integer Intervals (差分约束 或 贪心)
Integer Intervals Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12192 Accepted: 514 ...
- POJ 1201 Intervals || POJ 1716 Integer Intervals 差分约束
POJ 1201 http://poj.org/problem?id=1201 题目大意: 有一个序列,题目用n个整数组合 [ai,bi,ci]来描述它,[ai,bi,ci]表示在该序列中处于[ai, ...
- POJ 1716 Integer Intervals 差分约束
题目:http://poj.org/problem?id=1716 #include <stdio.h> #include <string.h> #include <ve ...
- POJ 1716 Integer Intervals#贪心
(- ̄▽ ̄)-* //求一个集合,这个集合与任意一个区间的交集,需至少有两个数字 //贪心过程:按n个区间的最右值从小到大对区间进行排列, //集合首先取第一个区间的最右两个数字, //到第二个区间, ...
- 【POJ 1716】Integer Intervals(差分约束系统)
id=1716">[POJ 1716]Integer Intervals(差分约束系统) Integer Intervals Time Limit: 1000MS Memory L ...
- 【POJ 1201】 Intervals(差分约束系统)
[POJ 1201] Intervals(差分约束系统) 11 1716的升级版 把原本固定的边权改为不固定. Intervals Time Limit: 2000MS Memory Limit: ...
- 【38.24%】【POJ 1201】Intervals
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25902 Accepted: 9905 Description You are ...
- POJ No.3680 Intervals
2016-06-01 22:01:39 题目链接: POJ No.3680 Intervals 题目大意: 给定N个带权区间,最多可以重复选一个点M次,求出一种选法使得所得权最大 解法: 费用流 建模 ...
随机推荐
- Ubuntu12.04 + 虚拟机VMware 9 + Secure CRT + EditPlus 本地C++开发环境搭建
1.1 软件准备 虚拟机VMware 9 Ubuntu 12.04 Secure CRT EditPlus 1.2 安装VMware 9与Ubuntu 12.04 这两个软件安装,按部就班,这里就 ...
- moto xt800 刷机到2.2.2
老机器啊,原来2.1的系统大多数软件都不能装sbf刷机包+工具+教程下载地址:http://u.115.com/file/bhdlwl2x 刷完之后如果RSD Lite显示刷机结果为失败,不要担心,手 ...
- lintcode:Remove Nth Node From End of Lis 删除链表中倒数第n个节点
题目: 删除链表中倒数第n个节点 给定一个链表,删除链表中倒数第n个节点,返回链表的头节点. 样例 给出链表1->2->3->4->5->null和 n = 2. 删除 ...
- java @param参数注解
注解,@param是参数的解释.如/***@param s 这里表示对s的文字说明,描述 */ public void aa(String s){}一般java中@表示注解,解释一个方法,类,属性的作 ...
- eclipse怎么集成配置JDK和Tomcat
eclipse怎么集成配置JDK和Tomcat_电脑软件_百度经验http://jingyan.baidu.com/album/db55b609ab6a1f4ba30a2f2c.html?picind ...
- 汇编debug 截图
- 浏览器 怪异模式(Quirks Mode) 与 标准模式(Standards Mode)
浏览器 怪异模式(Quirks Mode) 与 标准模式(Standards Mode) 怪异模式,浏览器使用自己的方式解析渲染页面,在不同的浏览器就会显示不同的样式.标准模式,浏览器使用W3C的标准 ...
- 【转载】String、StringBuffer与StringBuilder之间区别
文章来源:http://www.cnblogs.com/A_ming/archive/2010/04/13/1711395.html 这两天在看Java编程的书,看到String的时候将之前没有弄懂的 ...
- JVM学习笔记(二)------Java代码编译和执行的整个过程
Java代码编译是由Java源码编译器来完成,流程图如下所示: Java字节码的执行是由JVM执行引擎来完成,流程图如下所示: Java代码编译和执行的整个过程包含了以下三个重要的机制: Java源码 ...
- java开发之匿名内部类,接口的使用
下面的例子是Java.JDK7学习笔记上的 打算开发多人联机程序,对每个联机客户端,都会建立Client对象封装相关信息 1.Client.java public class Client { pri ...