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

正解:SPFA+差分约束系统

解题报告:

  大致题意是给定一些要求,比如说,1到7之间至少有5个数,然后条件需要全部满足,问

  这几天刷了几道差分约束系统+SPFA的题目,现在向总给的题目清单里面就只剩下一道半平面交没做了。夏令营回来有时间再切了吧。

  这道题也没什么好说的,只不过是根据大于号建图,跑最长路就可以了。

  唯一要注意的是因为是前缀和建图,需要把这个序列依次连边,比如说1连2连3这么一直连接下去。

  1. //It is made by jump~
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <cstdio>
  6. #include <cmath>
  7. #include <algorithm>
  8. #include <ctime>
  9. #include <vector>
  10. #include <queue>
  11. #include <map>
  12. #ifdef WIN32
  13. #define OT "%I64d"
  14. #else
  15. #define OT "%lld"
  16. #endif
  17. using namespace std;
  18. typedef long long LL;
  19. const int inf = (<<);
  20. const int MAXN = ;
  21. const int MAXM = ;
  22. int n;
  23. int first[MAXN],next[MAXM],to[MAXM],w[MAXM];
  24. int dis[MAXN];
  25. bool pd[MAXN];
  26. int ecnt;
  27. int Min,Max;
  28. int ans;
  29. queue<int>Q;
  30.  
  31. inline int getint()
  32. {
  33. int w=,q=;
  34. char c=getchar();
  35. while((c<'' || c>'') && c!='-') c=getchar();
  36. if (c=='-') q=, c=getchar();
  37. while (c>='' && c<='') w=w*+c-'', c=getchar();
  38. return q ? -w : w;
  39. }
  40.  
  41. inline void Init(){
  42. ecnt=; memset(first,,sizeof(first));
  43. Max=; Min=inf;
  44. while(!Q.empty()) Q.pop();
  45. ans=;
  46. }
  47.  
  48. inline void link(int x,int y,int z){
  49. next[++ecnt]=first[x]; first[x]=ecnt; to[ecnt]=y; w[ecnt]=z;
  50. }
  51.  
  52. inline void spfa(){
  53. for(int i=;i<=n;i++) dis[i]=-inf;
  54. Q.push(Min); pd[Min]=; dis[Min]=;
  55. while(!Q.empty()) {
  56. int u=Q.front(); Q.pop(); pd[u]=;
  57. for(int i=first[u];i;i=next[i]) { //最长路
  58. int v=to[i];
  59. if(dis[v]<dis[u]+w[i]) {
  60. dis[v]=dis[u]+w[i];
  61. if(!pd[v]){
  62. Q.push(v); pd[v]=;
  63. }
  64. }
  65. }
  66. }
  67. ans=dis[Max];
  68. }
  69.  
  70. inline void solve(){
  71. while(scanf("%d",&n)!=EOF){
  72. Init();
  73. int x,y,z;
  74. for(int i=;i<=n;i++) {
  75. x=getint()-;y=getint();z=getint();
  76. link(x,y,z);
  77. Min=min(Min,x); Max=max(Max,y);
  78. }
  79. for(int i=Min;i<Max;i++){//以前缀和为结点
  80. link(i+,i,-); link(i,i+,);//要使所有点满足s[i+1]-s[i] <= 1 && s[i]-s[i+1] <= 0
  81. }
  82. spfa();
  83. printf("%d",ans);
  84. }
  85. }
  86.  
  87. int main()
  88. {
  89. solve();
  90. return ;
  91. }

POJ1201 Intervals的更多相关文章

  1. POJ1201 Intervals[差分约束系统]

    Intervals Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26028   Accepted: 9952 Descri ...

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

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

  3. POJ1201 Intervals(差分约束)

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

  4. POJ1201 Intervals 【差分约束】

    题目链接 POJ1201 题解 差分约束 令\(a[i]\)表示是否选择\(i\),\(s[i]\)表示\(a[i]\)的前缀和 对\(s[i] \quad i \in [-1,50000]\)分别建 ...

  5. POJ1201 Intervals【差分约束系统】

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

  6. POJ1201 Intervals (差分约束)

    You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn. Write a program that: ...

  7. POJ1201 Intervals(差分约束系统)

    与ZOJ2770一个建模方式,前缀和当作点. 对于每个区间[a,b]有这么个条件,Sa-Sb-1>=c,然后我就那样连边WA了好几次. 后来偷看数据才想到这题还有两个隐藏的约束条件. 这题前缀和 ...

  8. poj1201 Intervals【差分约束+SPFA】

    转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4303365.html   ---by 墨染之樱花 题目链接:http://poj.org/pr ...

  9. POJ1201:Intervals(差分约束)

    差分约束经典题.设s[i]为前缀和,则有 s[i]-s[i-1]<=1 (i往i-1连-1的边) s[i]>=s[i-1] (i-1往i连0的边) s[b]-s[a-1]>=c (a ...

随机推荐

  1. 优化mysql主从下的slave延迟问题

    一般而言,slave相对master延迟较大,其根本原因就是slave上的复制线程没办法真正做到并发.简单说,在master上是并发模式(以InnoDB引擎为主)完成事务提交的,而在slave上,复制 ...

  2. 24Mybatis_延迟加载——用association来实现

    resultMap可以实现高级映射(使用association.collection实现一对一及一对多映射),association.collection具备延迟加载功能. 需求: 如果查询订单并且关 ...

  3. 【WPF】WPF中调用Winform

    1.添加两个引用:WindowsFormsIntegration.dll(负责整合WPF和Windows).System.Windows.Forms.2.在 XAML文件中添加两个引用(粗体部分): ...

  4. C语言 读取文件中特定数据

    //读取文件数据 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> struct jia ...

  5. Linux非root用户安装jdk和tomcat

    转载自:http://blog.csdn.net/wuyigong111/article/details/17410661,进行部分修改 创建一个用户 sgmm,并在其用户目录里面安装 jdk和tom ...

  6. php基础19:文件

    <?php //1.打开文件的更好的方法是通过 fopen() 函数.此函数为您提供比 readfile() 函数更多的选项. //fopen() 的第一个参数包含被打开的文件名,第二个参数规定 ...

  7. 关联规则算法(The Apriori algorithm)详解

    一.前言 在学习The Apriori algorithm算法时,参考了多篇博客和一篇论文,尽管这些都是很优秀的文章,但是并没有一篇文章详解了算法的整个流程,故整理多篇文章,并加入自己的一些注解,有了 ...

  8. Opencv step by step - 图像变换

    这里举出三个案例: #include <cv.h> #include <highgui.h> void image_smooth(IplImage * image) { cvN ...

  9. MVC4/5+jquery+bootstrap样式+dataTables+linq+WCF+EF6后台和前台的框架集合!好蛋疼哦!数据库支持MYSQL 和MSSQL,oracle。集成腾讯企业邮箱收邮件同步用户SSO登陆等功能。

    花费了我好多心血,才做出来,下个项目准备用这个框架! 大家有没有做这方面的可以交流一下! 花费了我好多心血,才做出来,下个项目准备用这个框架! 大家有没有做这方面的可以交流一下! 花费了我好多心血,才 ...

  10. linux上svn版本库创建小记

    [新建svn仓库] 先创建一个文件夹mkdir /opt/svn/wechat;   然后创建svn版本库    svnadmin create /opt/svn/wechat;   [创建用户组权限 ...