poj1201/zoj1508/hdu1384 Intervals(差分约束)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud
Intervals
Time Limit: 10 Seconds Memory Limit: 32768 KB
You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn.
Write a program that:
> reads the number of intervals, their endpoints and integers c1, ..., cn
from the standard input,
> computes the minimal size of a set Z of integers which has at least ci
common elements with interval [ai, bi], for each i = 1, 2, ..., n,
> writes the answer to the standard output.
Input
The first line of the input contains an integer n (1 <= n <= 50 000) - the
number of intervals. The following n lines describe the intervals. The i+1-th
line of the input contains three integers ai, bi and ci separated by single
spaces and such that 0 <= ai <= bi <= 50 000 and 1 <= ci <= bi
- ai + 1.
Process to the end of file.
Output
The output contains exactly one integer equal to the minimal size of set Z sharing
at least ci elements with interval [ai, bi], for each i = 1, 2, ..., n.
Sample Input
5
3 7 3
8 10 3
6 8 1
1 3 1
10 11 1
Sample Output
6
题意:
对于一个序列,有n个描述,[ai,bi,ci]分别表示在区间[ai,bi]上,至少有ci个数属于该序列。输出满足这n个条件的最短的序列(即包含的数字个数最少)
分析:
设s[i]表示从0到i中有s[i]个数属于这个序列。
那么,对于每个描述,都可以得到一个方程s[bi]-s[ai-1]>=ci
同时由于每个数至多取一次,那么有s[i]-s[i-1]<=1,即s[i-1]-s[i]>=-1;
另外还有s[i]-s[i-1]>=0;
由这三个方程组建图,利用最长路即可求出。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
#define MAXN 50010
#define REP(A,X) for(int A=0;A<X;A++)
#define INF 0x7fffffff
#define CLR(A,X) memset(A,X,sizeof(A))
struct node {
int v,d,next;
}edge[*MAXN];
int head[MAXN];
int e=;
void init()
{
REP(i,MAXN)head[i]=-;
}
void add_edge(int u,int v,int d)
{
edge[e].v=v;
edge[e].d=d;
edge[e].next=head[u];
head[u]=e;
e++;
}
bool vis[MAXN];
int dis[MAXN];
void spfa(int s){
CLR(vis,);
REP(i,MAXN)dis[i]=i==s?:INF;
queue<int>q;
q.push(s);
vis[s]=;
while(!q.empty())
{
int x=q.front();
q.pop();
int t=head[x];
while(t!=-)
{
int y=edge[t].v;
int d=edge[t].d;
t=edge[t].next;
if(dis[y]>dis[x]+d)
{
dis[y]=dis[x]+d;
if(vis[y])continue;
vis[y]=;
q.push(y);
}
}
vis[x]=;
}
}
int main()
{
ios::sync_with_stdio(false);
int n;
int u,v,d;
int ans=;
int maxn=;
int minn=MAXN;
while(scanf("%d",&n)!= EOF&&n)
{
e=;
init();
REP(i,n)
{
scanf("%d%d%d",&u,&v,&d);
add_edge(u,v+,-d);
maxn=max(maxn,v+);
minn=min(u,minn);
}
for(int i=minn;i<maxn;i++){
add_edge(i+,i,);
add_edge(i,i+,);
}
spfa(minn);
cout<<-dis[maxn]<<endl;
}
return ;
}
代码君
poj1201/zoj1508/hdu1384 Intervals(差分约束)的更多相关文章
- POJ1201 Intervals(差分约束)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 28416 Accepted: 10966 Description You ...
- poj1201 Intervals——差分约束
题目:http://poj.org/problem?id=1201 差分约束裸题: 设 s[i] 表示到 i 选了数的个数前缀和: 根据题意,可以建立以下三个限制关系: s[bi] >= s[a ...
- hdu 1384 Intervals (差分约束)
Intervals Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- poj 1716 Integer Intervals (差分约束 或 贪心)
Integer Intervals Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12192 Accepted: 514 ...
- zoj 1508 Intervals (差分约束)
Intervals Time Limit: 10 Seconds Memory Limit: 32768 KB You are given n closed, integer interva ...
- poj 1201 Intervals(差分约束)
题目:http://poj.org/problem?id=1201 题意:给定n组数据,每组有ai,bi,ci,要求在区间[ai,bi]内至少找ci个数, 并使得找的数字组成的数组Z的长度最小. #i ...
- poj 1201 Intervals——差分约束裸题
题目:http://poj.org/problem?id=1201 差分约束裸套路:前缀和 本题可以不把源点向每个点连一条0的边,可以直接把0点作为源点.这样会快许多! 可能是因为 i-1 向 i 都 ...
- POJ 2101 Intervals 差分约束
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 27746 Accepted: 10687 Description You ...
- hdu 1384 Intervals (差分约束)
/* 给你 n 个区间 [Ai, Bi],要求从每一个区间中至少选出 Ci 个数出来组成一个序列 问:满足上面条件的序列的最短长度是多少? 则对于 不等式 f(b)-f(a)>=c,建立 一条 ...
随机推荐
- 判别linux机器字节序为大端还是小端
代码如下: #include <iostream> #include <arpa/inet.h> #include <cstdio> using namespace ...
- html label 标签的 for 属性
如果您在 label 元素内点击文本,就会触发此控件.就是说,当用户选择该标签时,浏览器就会自动将焦点转到和标签相关的表单控件上. 有两种使用方法: 方法1 使用for属性 <label for ...
- CSS3选择器小结
CSS3 选择器小结 一 通用选择器 1 *{}通配选择符(CSS2):适合所有元素对象. 2 E类型(HTML)选择符(CSS1):以文档语言对象类型DOM作为选择符. 3 E#myid是id ...
- Hadoop学习笔记-HDFS命令
进入 $HADOOP/bin 一.文件操作 文件操作 类似于正常的linux操作前面加上“hdfs dfs -” 前缀也可以写成hadoop而不用hdfs,但终端中显示 Use of this scr ...
- Effective Java2读书笔记-类和接口(五)
第21条:用函数对象表示策略 这一条其实也没说啥,就是策略模式.碰到这种场景时,定义一个策略接口,然后不同策略子类实现它,主类包含这个接口的引用就可以了. 第22条:优先考虑静态成员类 嵌套类是指被定 ...
- hdu 1811 Rank of Tetris
http://acm.hdu.edu.cn/showproblem.php?pid=1811 拓扑排序和并差集 #include <cstdio> #include <queue&g ...
- 【剑指offer】面试题32:从1到n整数中1出现的次数
题目: 求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1.10.11.12.13因此共出现6次,但是对于后面问题他就没辙了.A ...
- 炫酷吊炸天的nodeppt
由于要做一个关于node的分享,要准备写一个ppt方便就行交流.之前用的比较多的是slides(http://www.slides.com),最近知道了一个node写的工具,可以生成ppt,号称很强大 ...
- poj 3616 Milking Time DP
题意:在给予的N个时间里,奶牛Bessie在M个时间段里进行产奶,但是每次产奶后都要休息R个时间 M个时间段里,分别有开始时间start和结束时间end,和该时间段里产奶的效率efficiency 求 ...
- 《Java程序员面试笔试宝典》之为什么Java中有些接口没有任何方法
由于Java不支持多重继承,即一个类只能有一个父类,为了克服单继承的缺点,Java语言引入了接口这一概念.接口是抽象方法定义的集合(接口中也可以定义一些常量值),是一种特殊的抽象类.接口中只包含方法的 ...