传送门:

http://acm.hdu.edu.cn/showproblem.php?pid=1384

Intervals

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4841    Accepted Submission(s): 1815

Problem Description
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
 
Author
1384
 
Recommend
Eddy   |   We have carefully selected several similar problems for you:  1529 1531 1548 1534 1317 
题目意思:
给出 n 个区间,每个区间有个权值 Ci,最终找出一个最少的数字的集合,使得满足每个区间中至少包含 Ci 个数。
给你几组的a,b,c
从区间a到b(闭区间)选择至少c个数放入集合
要求集合中的数字最少,问你最少多少个数字
 
分析:
f(a)表示从0到a有f(a)个数放入集合
那么a,b,c根据不等式建立边
f(b)-f(a-1)>=c
 
这个不等式的意思是:从区间a,b里面选择至少c个数加入集合
隐藏的不等式:0<=f(i)-f(i-1)<=1
 
变形一下:
f(i)-f(i-1)>=0
f(i-1)-f(i)>=-1
 
根据这三个不等式建立边
找到区间在最左端:minn
找到区间的最右端:maxx
所以这样建立边的话,跑最短路的时候
起点应该是max,终点是min-1
f(max)-f(min-1)>=x
x就是我们需要的结果
code:
#include <iostream>
#include <cstdio>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<math.h>
#include<memory>
#include<queue>
#include<vector>
using namespace std;
#define max_v 50010
#define INF 9999999
int tot;
int head[max_v];
int vis[max_v];
int dis[max_v];
int minn,maxx;
struct node
{
int u,v,val;
int next;
}Edge[max_v<<];
void addEdge(int u,int v,int val)
{
Edge[tot].u=u;
Edge[tot].v=v;
Edge[tot].val=val;
Edge[tot].next=head[u];
head[u]=tot++;
}
void spfa()
{
for(int i=minn-;i<=maxx;i++)
dis[i]=-INF;
queue<int> q;
dis[maxx]=;
vis[maxx]=;
q.push(maxx);
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=;
for(int i=head[u];i!=-;i=Edge[i].next)
{
int v=Edge[i].v;
if(dis[v]<dis[u]+Edge[i].val)
{
dis[v]=dis[u]+Edge[i].val;
if(!vis[v])
{
vis[v]=;
q.push(v);
}
}
}
}
printf("%d\n",dis[minn-]);
return ;
}
int main()
{
int n,a,b,c;
while(~scanf("%d",&n))
{
tot=;
memset(head,-,sizeof(head));
memset(vis,,sizeof(vis));
maxx=;
minn=INF;
for(int i=;i<n;i++)
{
scanf("%d %d %d",&a,&b,&c);
a++;
b++;
minn=min(minn,a);
maxx=max(maxx,b);
addEdge(b,a-,c);
}
for(int i=minn;i<=maxx;i++)
{
addEdge(i,i-,);
addEdge(i-,i,-);
}
spfa();
}
return ;
}

今天学差分约束的时候又遇到了这个题,跑最长路初始化的时候搞错了,应该是dis[i]=-INF。。。。,卡了好一会

题目意思:
从一系列区间[a,b]中每个至少取ci个数构成集合s,问你集合s至少需要多少个数
区间约束问题,差分约束解决
分析:
f(x):[0,x]区间内取f(x)个数
则区间[a,b]内至少取c个数可以变形为:f(b)-f(a-1)>=c
隐藏关系:1>=f(i)-f(i-1)>=0,变形一下:
f(i)-f(i-1)>=0
f(i-1)-f(i)>=-1
maxx:所有区间的最大值
minx:所有区间的最小值
i属于[minx,maxx]
总结一下得到这样三个关系:
f(b)-f(a-1)>=c
f(i)-f(i-1)>=0  i属于[minx,maxx]
f(i-1)-f(i)>=-1 i属于[minx,maxx]
这三个表达式的形式都是一样的,xi-xj>=c
按照j->i建边,权值为c
所以:
(a-1)->b 建边 权值c
(i-1)->i 建边 权值0
i->(i-1) 建边 权值-1
我们要求的东西ans可以变形为:f(maxx)-f(minx-1)>=ans
>=是最小值,所以是最长路
所以是问你从minx-1到maxx的最长路
建图完毕之后spfa跑一遍最长路,不能用dj,因为存在负权边
code:
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<set>
#include<map>
#include<list>
#include<math.h>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long LL;
#define INF 9999999999
#define me(a,x) memset(a,x,sizeof(a))
int mon1[]= {,,,,,,,,,,,,};
int mon2[]= {,,,,,,,,,,,,};
int dir[][]= {{,},{,-},{,},{-,}}; int getval()
{
int ret();
char c;
while((c=getchar())==' '||c=='\n'||c=='\r');
ret=c-'';
while((c=getchar())!=' '&&c!='\n'&&c!='\r')
ret=ret*+c-'';
return ret;
}
void out(int a)
{
if(a>)
out(a/);
putchar(a%+'');
} #define max_v 50005
int dis[max_v];
int vis[max_v]; struct node
{
int v,w;
node(int vv=,int ww=):v(vv),w(ww){}
};
vector<node> G[max_v];
int n,m; void init()
{
for(int i=;i<max_v;i++)
G[i].clear();
for(int i=;i<max_v;i++)
{
dis[i]=-INF;
vis[i]=;
}
} void spfa(int s)
{ queue<int> q;
q.push(s);
vis[s]=;
dis[s]=; while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=; for(int j=;j<G[u].size();j++)
{
int v=G[u][j].v;
int w=G[u][j].w;
if(dis[v]<dis[u]+w)//最长路
{
dis[v]=dis[u]+w;
if(vis[v]==)
{
vis[v]=;
q.push(v);
}
}
}
}
} int f(int u,int v)
{
for(int j=;j<G[u].size();j++)
{
if(G[u][j].v==v)
return ;
}
return ;
}
int main()
{
int a;
while(~scanf("%d",&a))
{
int x,y,w;
init();
int maxx=-INF,minx=INF;
for(int i=;i<=a;i++)
{
scanf("%d %d %d",&x,&y,&w);
x++,y++;//防止减1的时候出现负数
maxx=max(maxx,y);
minx=min(minx,x);
if(f(x-,y))//预防重边
G[x-].push_back(node(y,w));
}
for(int i=minx;i<=maxx;i++)
{
if(f(i-,i))//预防重边
G[i-].push_back(node(i,));
if(f(i,i-))//预防重边
G[i].push_back(node(i-,-));
}
spfa(minx-);
printf("%d\n",dis[maxx]);
}
return ;
}
/*
题目意思:
从一系列区间[a,b]中每个至少取ci个数构成集合s,问你集合s至少需要多少个数
区间约束问题,差分约束解决 分析:
f(x):[0,x]区间内取f(x)个数 则区间[a,b]内至少取c个数可以变形为:f(b)-f(a-1)>=c
隐藏关系:1>=f(i)-f(i-1)>=0,变形一下:
f(i)-f(i-1)>=0
f(i-1)-f(i)>=-1
maxx:所有区间的最大值
minx:所有区间的最小值 i属于[minx,maxx] 总结一下得到这样三个关系:
f(b)-f(a-1)>=c
f(i)-f(i-1)>=0 i属于[minx,maxx]
f(i-1)-f(i)>=-1 i属于[minx,maxx] 这三个表达式的形式都是一样的,xi-xj>=c
按照j->i建边,权值为c
所以:
(a-1)->b 建边 权值c
(i-1)->i 建边 权值0
i->(i-1) 建边 权值-1 我们要求的东西ans可以变形为:f(maxx)-f(minx-1)>=ans
>=是最小值,所以是最长路
所以是问你从minx-1到maxx的最长路
建图完毕之后spfa跑一遍最长路,不能用dj,因为存在负权边 */

POJ 1384 Intervals (区间差分约束,根据不等式建图,然后跑spfa)的更多相关文章

  1. POJ 1201 Intervals(差分约束 区间约束模版)

    关于差分约束详情可阅读:http://www.cppblog.com/menjitianya/archive/2015/11/19/212292.html 题意: 给定n个区间[L,R], 每个区间至 ...

  2. poj 1201 Intervals(差分约束)

    做的第一道差分约束的题目,思考了一天,终于把差分约束弄懂了O(∩_∩)O哈哈~ 题意(略坑):三元组{ai,bi,ci},表示区间[ai,bi]上至少要有ci个数字相同,其实就是说,在区间[0,500 ...

  3. POJ 1201 Intervals (经典) (差分约束)

    <题目链接> 题目大意:给你$n$段区间,$a_i,b_i,c_i$ 表示在 $[a_i,b_i]$ 区间内至少要选择$c_i$个点.现在问你在满足这n个条件的情况下,最少要选多少个点? ...

  4. HDU 1384 Intervals【差分约束-SPFA】

    类型:给出一些形如a−b<=k的不等式(或a−b>=k或a−b<k或a−b>k等),问是否有解[是否有负环]或求差的极值[最短/长路径].例子:b−a<=k1,c−b&l ...

  5. hdu 1384 Intervals (差分约束)

    Problem - 1384 好歹用了一天,也算是看懂了差分约束的原理,做出第一条查分约束了. 题意是告诉你一些区间中最少有多少元素,最少需要多少个元素才能满足所有要求. 构图的方法是,(a)-> ...

  6. POJ 1201 Intervals【差分约束】

    传送门:http://poj.org/problem?id=1201 题意: 有n个如下形式的条件:,表示在区间[, ]内至少要选择个整数点.问你满足以上所有条件,最少需要选多少个点? 思路:第一道差 ...

  7. 【题解】 POJ 1201 Intervals(差分约束)

    懒得复制,戳我戳我 Solution: 这道题就是一个板子题 抽象成第\(a\)至第\(b\)间选择数的个数为\(c\),我们就可以用前缀和来表示,这样就可以得到不等式\(s[b]-s[a-1]> ...

  8. POJ 1201 Intervals (差分约束,最短路)

    题意: 有一个集合Z,其元素都是整整数,但是数量未知.现有n个约束,形如 [a,b]=c 表示整数区间[a,b]中有c个元素在Z中出现.问集合Z最小可能含多少个元素? 思路: 对于所给的区间 cnt[ ...

  9. poj 1201 Intervals【差分约束+spfa】

    设s为前缀和,首先显然的条件是\[ s_{bi}-s_{ai-1}>=c \],然后隐含的是\[ s_i-s_{i-1}>=0 s_i-s_{i-1}<=1 \] 然后根据差分约束, ...

  10. HDU 1384 Intervals(差分约束)

    Intervals Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

随机推荐

  1. Ubuntu重启网卡的三种方法

    一.network利用root帐户# service network restart 或者/etc/init.d/networking restart 二.ifdown/ifup# ifdown et ...

  2. php中怎么导入自己写的类

    如果写的类是写在当前php文件内,就直接实例化若你的类写在其他的php文件里,就要先用include或require,将类文件引入<?php include("class.php&qu ...

  3. CodeChef SADPAIRS:Chef and Sad Pairs

    vjudge 首先显然要建立圆方树 对于每一种点建立虚树,考虑这一种点贡献,对于虚树上已经有的点就直接算 否则对虚树上的一条边 \((u, v)\),\(u\) 为父亲,假设上面连通块大小为 \(x\ ...

  4. Luogu4191:[CTSC2010]性能优化

    传送门 题目翻译:给定两个 \(n\) 次多项式 \(A,B\) 和一个整数 \(C\),求 \(A\times B^C\) 在模 \(x^n\) 意义下的卷积 显然就是个循环卷积,所以只要代入 \( ...

  5. [SDOI2014]旅行解题报告

    题目描述 S国有N个城市,编号从1到N.城市间用N-1条双向道路连接,满足从一个城市出发可以到达其它所有城市.每个城市信仰不同的宗教,如飞天面条神教.隐形独角兽教.绝地教都是常见的信仰. 为了方便,我 ...

  6. Liunx一些命令

    1.设置管理员的密码:sudo passwd root2.获取管理员权限su root3.查看IP地址sudo ifconfig -a4.创建一个文件sudo touch test.txt5.创建一个 ...

  7. js中变量声明有var和没有var的区别

    转js中var用与不用的区别 2015年07月13日 16:08:22 阅读数:3627 Javascript声明变量的时候,虽然用var关键字声明和不用关键字声明,很多时候运行并没有问题,但是这两种 ...

  8. css3在页面中插入内容

    A. 使用选择器来插入内容 h2:before{ content:"前缀"; } h2:after{ content:"后缀"; } B. 指定个别的元素不进行 ...

  9. weex 数据绑定,动态控制组件的显示内容及样式

    无论的原生开发还是weex开发,经常会需要我们对一些组件/控件动态赋值,在原生中,我们大家都知道,对控件setText就可以了,那么在weex中呢,我们需要怎么做呢,其实很简单,几行代码就可以搞定!首 ...

  10. PRINCE2考试用什么语言?

    PRINCE2考试可用英语之外的阿拉伯语.中文.日语.马来西亚/印度尼西亚语.泰国语.越南语.菲律宾语.波兰语和盖尔语等9种语言进行. PRINCE2手册目前已有英文.中文.丹麦语和日语,正在翻译成荷 ...