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

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

差分约束系统用来解一个不等式组,只要这个不等式组里的不等式形如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. zabbix3.0 安装方法,一键实现短信、电话、微信、APP 告警

    引言 免费开源监控工具 Zabbix 因其强大的监控功能得到各大互联网公司的广泛认可,具体功能不再详细介绍,在之前发布的 Zabbix 2.4.1 安装及微信短信提醒已经做了详细介绍,本篇主要对 Za ...

  2. POJ 1850

    #include <iostream> #include <string> using namespace std; int fac(int num); int C(int n ...

  3. Struts2从版本2.2.1升级至2.3.15.1出现的问题

    问题一 原版本Struts2.2.1中的JSP代码如下: <a class="buttonSelect" href="/manage/machine/uploadF ...

  4. YUM详解

    用YUM升级软件打开终端,切换到root用户,yum的操作大都须有超级用户的权限.首 先,yum update,这一步是必须的,yum会从服务器的header目录下载rpm的header,放在本地的缓 ...

  5. *[codility]MinAvgTwoSlice

    https://codility.com/demo/take-sample-test/min_avg_two_slice 此题要求一个数组子段的最小的平均数(返回第一个数字的index).刚开始想记录 ...

  6. JavaWeb项目开发案例精粹-第2章投票系统-006view层

    1.index.jsp <%@ page language="java" import="java.util.*" pageEncoding=" ...

  7. Winsock完成端口模型-Delphi代码

    原文出处 <Windows网络编程技术>第8章 完成端口模型 由于原书附的是C代码,我把其翻译成Delphi代码. 其中winsock2.pas在delphi中不带,要另外下载http:/ ...

  8. 深度分析Java的ClassLoader机制(源码级别)

    写在前面:Java中的所有类,必须被装载到jvm中才能运行,这个装载工作是由jvm中的类装载器完成的,类装载器所做的工作实质是把类文件从硬盘读取到内存中,JVM在加载类的时候,都是通过ClassLoa ...

  9. HDU5087——Revenge of LIS II(BestCoder Round #16)

    Revenge of LIS II Problem DescriptionIn computer science, the longest increasing subsequence problem ...

  10. 转TerreyLee AJAX入门系列2——ScriptManager的理解总结

    ScriptManager的功能之一就是处理页面上局部更新,对于这点,我想大家都知道.但是他工作的原理到底是什么呢,这个暂且不从正面来回答. 我们这样想一下,目前能够真正实现局部刷新的就是js+xml ...