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


Source: Southwestern Europe 2002

 //2013-11-25 09:45:31     Accepted     1508    C++    2120    2124
/* 题意:
有一个序列,题目用n个整数组合 [ai,bi,ci]来描述它,[ai,bi,ci]表示在该
序列中处于[ai,bi]这个区间的整数至少有ci个。如果存在这样的序列,请求出满足
题目要求的最短的序列长度是多少。如果不存在则输出 -1。 差分约束:
第一题差分约束,感觉还好,基本看过资料的应该都可以做。
难点在于构图部分,构好图后直接求其单源最短路径。
构图就是要满足
1)S(bi) - S(a(i-1))>=Ci
2)Si - S(i-1)>=0
3)S(i-1) - Si>=-1 即 map[bi][a(i-1)]=-ci
map[i][i-1]=0
map[i-1][i]=1 然后在采用最短路算法求点n到点0的最短路,值再取反即为解
存在负权环时不存在 */
#include<stdio.h>
#include<string.h>
#define N 50005
#define inf 0x7ffffff
using namespace std;
struct node{
int u,v,w;
}edge[*N];
int n,edgenum,m;
int d[N];
int Max(int a,int b)
{
return a>b?a:b;
}
void addedge(int u,int v,int w)
{
edge[edgenum].u=u;
edge[edgenum].v=v;
edge[edgenum++].w=w;
}
bool bellman_ford()
{
bool flag;
for(int i=;i<=m;i++) d[i]=inf;
d[m]=;
for(int i=;i<m;i++){
flag=false;
for(int j=;j<edgenum;j++){
if(d[edge[j].u]!=inf && d[edge[j].v]>d[edge[j].u]+edge[j].w){
flag=true;
d[edge[j].v]=d[edge[j].u]+edge[j].w;
}
}
if(!flag) break;
}
for(int i=;i<edgenum;i++)
if(d[edge[i].u]!=inf && d[edge[i].v]>d[edge[i].u]+edge[i].w)
return false;
return true;
}
int main(void)
{
int a,b,c;
while(scanf("%d",&n)!=EOF)
{
edgenum=;
m=;
for(int i=;i<n;i++){
scanf("%d%d%d",&a,&b,&c);
addedge(b,a-,-c);
m=Max(m,Max(a,b));
}
for(int i=;i<m;i++){
addedge(i,i+,);
addedge(i+,i,);
}
if(!bellman_ford()) puts("-1");
else printf("%d\n",-d[]);
}
return ;
}

贴一下SPFA的解法:

 //2013-11-25 22:05:24     Accepted     1508    C++    130    5444
#include<iostream>
#include<vector>
#include<queue>
#include<stdio.h>
#include<string.h>
#define N 50005
#define inf 0x7ffffff
using namespace std;
struct node{
int v,w;
node(int a,int b){
v=a;w=b;
}
};
vector<node>V[N];
int d[N],vis[N],in[N];
int n,m;
int spfa()
{
memset(vis,,sizeof(vis));
memset(in,,sizeof(in));
for(int i=;i<=m;i++) d[i]=-inf;
queue<int>Q;
Q.push(m);
d[m]=;
while(!Q.empty()){
int u=Q.front();
Q.pop();
if(in[u]>m) return ;
vis[u]=;
int n0=V[u].size();
for(int i=;i<n0;i++){
int v=V[u][i].v;
int w=V[u][i].w;
if(d[v]<d[u]+w){
d[v]=d[u]+w;
if(!vis[v]){
in[v]++;
Q.push(v);
vis[v]=;
}
}
}
}
return ;
}
int main(void)
{
int a,b,c;
while(scanf("%d",&n)!=EOF)
{
m=;
for(int i=;i<N;i++) V[i].clear();
for(int i=;i<n;i++){
scanf("%d%d%d",&a,&b,&c);
m=max(m,max(a,b));
V[b].push_back(node(a-,c));
}
for(int i=;i<m;i++){
V[i].push_back(node(i+,-));
V[i+].push_back(node(i,));
}
if(!spfa()) puts("impossible");
else printf("%d\n",d[]);
}
return ;
}

zoj 1508 Intervals (差分约束)的更多相关文章

  1. poj 1201/zoj 1508 intervals 差分约束系统

      // 思路 : // 图建好后 剩下的就和上一篇的 火烧连营那题一样了 求得解都是一样的 // 所以稍微改了就过了 // 最下面还有更快的算法 速度是这个算法的2倍#include <ios ...

  2. POJ1201 Intervals(差分约束)

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 28416   Accepted: 10966 Description You ...

  3. hdu 1384 Intervals (差分约束)

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

  4. poj 1716 Integer Intervals (差分约束 或 贪心)

    Integer Intervals Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12192   Accepted: 514 ...

  5. poj 1201 Intervals(差分约束)

    题目:http://poj.org/problem?id=1201 题意:给定n组数据,每组有ai,bi,ci,要求在区间[ai,bi]内至少找ci个数, 并使得找的数字组成的数组Z的长度最小. #i ...

  6. poj 1201 Intervals——差分约束裸题

    题目:http://poj.org/problem?id=1201 差分约束裸套路:前缀和 本题可以不把源点向每个点连一条0的边,可以直接把0点作为源点.这样会快许多! 可能是因为 i-1 向 i 都 ...

  7. poj1201 Intervals——差分约束

    题目:http://poj.org/problem?id=1201 差分约束裸题: 设 s[i] 表示到 i 选了数的个数前缀和: 根据题意,可以建立以下三个限制关系: s[bi] >= s[a ...

  8. POJ 1201 &amp; HDU1384 &amp; ZOJ 1508 Intervals(差分约束+spfa 求最长路径)

    题目链接: POJ:http://poj.org/problem?id=1201 HDU:http://acm.hdu.edu.cn/showproblem.php? pid=1384 ZOJ:htt ...

  9. POJ 2101 Intervals 差分约束

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 27746   Accepted: 10687 Description You ...

随机推荐

  1. 15、SpringBoot------整合swagger2

    开发工具:STS 前言: 对外提供一个Api,无论是对开发.测试.维护,都有很大的帮助. 下面我们来实现swagger2. 参考实例:https://blog.csdn.net/weixin_3947 ...

  2. MFC项目依赖 BCG框架示例

    1.创建一个简单的MFC工程: 2.将BCG框架项目导入到新建的mfc解决方案中,例如将BCGCBPro\BCGCBPRO140.vcxproj添加到解决方案. 3.修改mfc项目属性,包含BCG框架 ...

  3. Lucene检索提高性能的几个方式

    1.采用最新版本的Lucene 2.索引文件存储采用本地文件系统,如果需要挂载远程系统,请采用 readonly方式. 3.当然采用更好的硬件,更高I/O的磁盘 4.提高OS 缓存,调整参数 5.提高 ...

  4. 深入理解 SVG 系列(一) —— SVG 基础

    来源:https://segmentfault.com/a/1190000015652209 本系列文章分为三个部分: 第一部分是 SVG 基础. 主要讲 SVG 的一些基础知识,包括 SVG 基本元 ...

  5. 继续分享shell 之变量使用

    变量类型 运行shell时,会同时存在三种变量: 1) 局部变量 局部变量在脚本或命令中定义,仅在当前shell实例中有效,其他shell启动的程序不能访问局部变量. 2) 环境变量 所有的程序,包括 ...

  6. ajaxfileupload多文件上传 - 修复只支持单个文件上传的bug

    搜索: jquery ajaxFileUpload AjaxFileUpload同时上传多个文件 原生的AjaxFileUpload插件是不支持多文件上传的,通过修改AjaxFileUpload少量代 ...

  7. xml的应用与dtd约束

    1.xml的应用 *不同的系统之间的传输数据(qq消息传输) *用来表示生活中有关系的数据(省市区的包含关系) *经常用在文件配置 **比如现在连接数据库,肯定知道数据库的名称和密码及用户名.    ...

  8. React路由-基础篇

    React-Router-DOM ReactRouter网址, 安装 -npmjs找到react-router-dom -yarn add react-router-dom 基本使用方法 1.创建一个 ...

  9. HTML5--定义区块

    1.效果图如下: 备注: <article> 1.作用:用来表示文档.页面中独立的.完整的.可以独自被外部引用的内容 2.一般有个header元素,有时还有脚注 <article&g ...

  10. JDK5 新特性

     JDK5新特性目录导航: 自动拆装箱 Foreach 静态导入 可变参数 Var args 枚举 格式化输出 泛型 ProcessBuilder 内省 线程并发库(JUC) 监控和管理虚拟机 元数据 ...