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

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

差分约束系统用来解一个不等式组,只要这个不等式组里的不等式形如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. PHP WAMP关闭notice等提示

    这是xdebug的的错误报告.在开发环境下,可以考虑将其开启,但是在部署到真实应用环境下应该将其关掉. 找到你的php.ini 在最后几行注释掉所有关于xdebug的东西,重启apache即可!

  2. 【转】Wireshark:“There are no interfaces on which a capture can be done ”

    linux环境下 两种解决方案:    第一种方法:使用root用户登陆        xiaoshancun@xiaoshancun-VM500:~$ sudo wireshark    第二种方法 ...

  3. Project Euler 97 :Large non-Mersenne prime 非梅森大素数

    Large non-Mersenne prime The first known prime found to exceed one million digits was discovered in ...

  4. python学习[二]

    继续学习 http://www.cnblogs.com/vamei/archive/2012/09/13/2682778.html 词典的每个元素是键值对.元素没有顺序.dic = {'tom':11 ...

  5. 包装类型的比较,如:Integer,Long,Double

    Integer, Long, Double等基本类型的包装类型,比较时两种方法:第一种:equals,  第二种: .intValue(),  .longValue() ,  .doubleValue ...

  6. CSDN代码片排版

    CSDN上发博文,尤其是有代码的,要用好“插入代码”功能(见下图).这是必须的,未来的IT程序员,需要用这种方式,展示出自己的代码.这是尊重读者,树立品牌的需要. 相关方法,参考见视频<发布第一 ...

  7. Python中itertools模块

    itertools模块包含创建有效迭代器的函数,可以用各种方式对数据进行循环操作,此模块中的所有函数返回的迭代器都可以与for循环语句以及其他包含迭代器(如生成器和生成器表达式)的函数联合使用. ch ...

  8. JavaWeb项目开发案例精粹-第3章在线考试系统-006实体层

    1. package com.sanqing.po; /* * 学生表,保存学生编号,系统密码 */ public class Student { private String studentID; ...

  9. python 利用smtp发送邮件,html格式

    def send_mail(to_list, sub, context):#sentmail to the maillist ''' to_list: 发送给谁 sub: 主题 context: 内容 ...

  10. 给360的六条建议(禁止异地登录,普通用户500G足够用了)

    个人觉得,360云盘哪怕做个小改进,都不至于走到现在的地步,最后六条建议! 1.弄个实名制,身份证和手机号码双重绑定,每人限制申请一个账户. 2.禁止云盘的一切分享功能,采用封闭式,个人云盘资料其他人 ...