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

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 end points 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 <= 50000) -- 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 <= 50000 and 1 <= ci <= bi - ai+1.

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

  1. 5
  2. 3 7 3
  3. 8 10 3
  4. 6 8 1
  5. 1 3 1
  6. 10 11 1

Sample Output

  1. 6

Source

 
 
题意:每次给出一段区间$[a_i,b_i]$以及一个数$c_i$,使得在这中间至少有$c_i$个数,求一个最小的集合$Z$,使得集合$Z$满足上述所有要求,问集合$Z$的大小
 
 
 
思路:
设$S[i]$表示$0-i$这一段区间的前缀和
那么题目的关系就变成了$S[b_i]-S[a_i]>=c_i$
这是一个很典型的差分约束类问题
题目中要求集合最小,因此转换为最长路,将所有的式子写成$B-A>=C$的形式
同时题目中还有一个条件$0<=S[i]-S[i-1]<=1$
因为数据为整数
于是又得到两个方程
$S\left[ i\right] -S\left[ i-1\right] \geq 0$
$S\left[ i-1\right] -S\left[ i\right] \geq -1$
但是有个细节:$S[i-1]$不能表示,因此我们需要将所有下标$+1$,此时$S[i]$表示$0 to (i-1)$的前缀和
同时,这个图一定是联通的,因此不用新建超级源点
 
  1. #include<cstdio>
  2. #include<queue>
  3. #include<cstring>
  4. #define INF 1e8+10
  5. using namespace std;
  6. const int MAXN=1e6+;
  7. #define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,MAXN,stdin),p1==p2)?EOF:*p1++)
  8. char buf[MAXN],*p1=buf,*p2=buf;
  9. inline int read()
  10. {
  11. char c=getchar();int x=,f=;
  12. while(c<''||c>''){if(c=='-')f=-;c=getchar();}
  13. while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
  14. return x*f;
  15. }
  16. struct node
  17. {
  18. int u,v,w,nxt;
  19. }edge[MAXN];
  20. int head[MAXN],num=;
  21. int maxx=-INF,minn=INF;
  22. int dis[MAXN],vis[MAXN];
  23. inline void AddEdge(int x,int y,int z)
  24. {
  25. edge[num].u=x;
  26. edge[num].v=y;
  27. edge[num].w=z;
  28. edge[num].nxt=head[x];
  29. head[x]=num++;
  30. }
  31. int SPFA()
  32. {
  33. queue<int>q;
  34. memset(dis,-0xf,sizeof(dis));
  35. dis[minn]=;q.push(minn);
  36. while(q.size()!=)
  37. {
  38. int p=q.front();q.pop();
  39. vis[p]=;
  40. for(int i=head[p];i!=-;i=edge[i].nxt)
  41. {
  42. if(dis[edge[i].v]<dis[p]+edge[i].w)
  43. {
  44. dis[edge[i].v]=dis[p]+edge[i].w;
  45. if(vis[edge[i].v]==)
  46. vis[edge[i].v]=,q.push(edge[i].v);
  47. }
  48. }
  49. }
  50. printf("%d",dis[maxx]);
  51. }
  52. int main()
  53. {
  54. #ifdef WIN32
  55. freopen("a.in","r",stdin);
  56. #else
  57. #endif
  58. memset(head,-,sizeof(head));
  59. int N=read();
  60. for(int i=;i<=N;i++)
  61. {
  62. int x=read(),y=read(),z=read();
  63. AddEdge(x,y+,z);
  64. maxx=max(y+,maxx);
  65. minn=min(x,minn);
  66. }
  67. for(int i=minn;i<=maxx-;i++)
  68. {
  69. AddEdge(i+,i,-);
  70. AddEdge(i,i+,);
  71. }
  72. SPFA();
  73. return ;
  74. }

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

  1. poj1201 Intervals——差分约束

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

  2. hdu 1384 Intervals (差分约束)

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

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

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

  4. zoj 1508 Intervals (差分约束)

    Intervals Time Limit: 10 Seconds      Memory Limit: 32768 KB You are given n closed, integer interva ...

  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基础差分约束

    题意:       有一条直线,直线上做多有50000个点,然后给你组关系 a b c表明a-b之间最少有c个点,问直线上最少多少个点. 思路:        a-b最少有c个点可以想象a到b+1的距 ...

  8. poj1201/zoj1508/hdu1384 Intervals(差分约束)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Intervals Time Limit: 10 Seconds      Mem ...

  9. POJ1201 Intervals差分约束系统(最短路)

    Description You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn. Write a p ...

随机推荐

  1. less编译工具——koala使用介绍

    1:使用koala编译软件    官网:http://koala-app.com/index-zh.html (目前官网点击下载没有反应,有人说可能是网络问题,但真正的原因是需要FQ才能下载了) 百度 ...

  2. 315道python面试题(参考答案)

    第一部分 Python基础篇 1:为什么学习Python 家里有在这个IT圈子里面,也想让我接触这个圈子,然后给我建议学的Python, 然后自己通过百度和向有学过Python的同学了解了Python ...

  3. Java异常处理 10 个最佳实践

    异常处理是Java 开发中的一个重要部分.它是关乎每个应用的一个非功能性需求,是为了处理任何错误状况,比如资源不可访问,非法输入,空输入等等.Java提供了几个异常处理特性,以try,catch 和 ...

  4. python元组和字典的简单学习

    元组(tuple) 用圆括号()标识,定义元组后,元组元素不可修改.如果想修改元组只能重新定义元组. 因为元组不可更改,所以也没有增删改等用法,主要语法就是访问元组元素,遍历元组. 访问元组元素: t ...

  5. mysql 开发基础系列12 选择合适的数据类型(上)

    一. char 与varchar比较 在上图的最后一行的值只适用在"非严格模式",关于严格模式后面讲到.在“开发基础系列4“ 中讲到CHAR 列删除了尾部的空格.由于char是固定 ...

  6. RocketMQ事务消息实现分析

    这周RocketMQ发布了4.3.0版本,New Feature中最受关注的一点就是支持了事务消息: 今天花了点时间看了下具体的实现内容,下面是简单的总结. RocketMQ事务消息概要 通过冯嘉发布 ...

  7. Web中的积累:外观模式 Facade

    摘要: 原创出处: http://www.cnblogs.com/Alandre/ 泥沙砖瓦浆木匠 希望转载,保留摘要,谢谢! 壹 前言 目测好久没写文章了,距离上一篇文章也有二十多天.我是怎么了?哈 ...

  8. nohup & expect & netstat学习

    1.nohup 用途:不挂断地运行命令,通常加上‘&’命令,& 放在命令后面表示设置此进程为后台进程.分为两种情况,如下: 在不使用密码的情况下使用nohup,只需按如下形式即可: n ...

  9. Windows编程之模块遍历(C++实现)

    Windows编程之模块遍历 PS: 主要扣代码使用,直接滑动到最下面使用. 遍历模块需要几个API,和一个结构体 1.创建进程快照 2.遍历首次模块 3.继续下次遍历 4.模块信息结构体 API 分 ...

  10. PHP接口的思考

    其中就有一个SPL(标准PHP库)的尝试,SPL中实现一些接口,其中最主要的就是Iterator迭代器接口,通过实现这个接口,就能使对象能够用于foreach结构,从而在使用形式上比较统一.比如SPL ...