转载请注明出处: 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;

由这三个方程组建图,利用最长路即可求出。

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <cstdlib>
 5 #include <algorithm>
 6 #include <queue>
 7 using namespace std;
 8 typedef long long ll;
 9 typedef pair<int,int> PII;
10 #define MAXN 50010
11 #define REP(A,X) for(int A=0;A<X;A++)
12 #define INF  0x7fffffff
13 #define CLR(A,X) memset(A,X,sizeof(A))
14 struct node {
15     int v,d,next;
16 }edge[3*MAXN];
17 int head[MAXN];
18 int e=0;
19 void init()
20 {
21     REP(i,MAXN)head[i]=-1;
22 }
23 void add_edge(int u,int v,int d)
24 {
25     edge[e].v=v;
26     edge[e].d=d;
27     edge[e].next=head[u];
28     head[u]=e;
29     e++;
30 }
31 bool vis[MAXN];
32 int dis[MAXN];
33 void spfa(int s){
34     CLR(vis,0);
35     REP(i,MAXN)dis[i]=i==s?0:INF;
36     queue<int>q;
37     q.push(s);
38     vis[s]=1;
39     while(!q.empty())
40     {
41         int x=q.front();
42         q.pop();
43         int t=head[x];
44         while(t!=-1)
45         {
46             int y=edge[t].v;
47             int d=edge[t].d;
48             t=edge[t].next;
49             if(dis[y]>dis[x]+d)
50             {
51                 dis[y]=dis[x]+d;
52                 if(vis[y])continue;
53                 vis[y]=1;
54                 q.push(y);
55             }
56         }
57         vis[x]=0;
58     }
59 }
60 int main()
61 {
62     ios::sync_with_stdio(false);
63     int n;
64     int u,v,d;
65     int ans=0;
66     int maxn=0;
67     int minn=MAXN;
68     while(scanf("%d",&n)!= EOF&&n)
69     {
70         e=0;
71         init();
72         REP(i,n)
73         {
74             scanf("%d%d%d",&u,&v,&d);
75             add_edge(u,v+1,-d);
76             maxn=max(maxn,v+1);
77             minn=min(u,minn);
78         }
79         for(int i=minn;i<maxn;i++){
80             add_edge(i+1,i,1);
81             add_edge(i,i+1,0);
82         }
83         spfa(minn);
84         cout<<-dis[maxn]<<endl;
85     }
86     return 0;
87 }

代码君

转载于:https://www.cnblogs.com/fraud/p/4304383.html

poj1201/zoj1508/hdu1384 Intervals(差分约束)相关推荐

  1. POJ1201/ZOJ1508/HDU1384 Intervals(spfa解差分约束问题)

    题意是说给出一些闭区间,这些区间上整点可以选择放一个元素或者不放,但是每个区间都有一个下限,就是说你在这个区间里面的元素个数不能低于这个下限值. 最后要求出最少需要几个元素才能满足每个区间的要求. 建 ...

  2. poj 1201 Intervals 差分约束

    真 .读题杀,英文题一脸懵逼,看来以后还要多读读英文题,不过读完了就能发现这其实是一道很裸的差分约束,按照题意建边即可,但还要注意的就是后一个要大于等于前一个,并且每个位置不能超过一个元素.求一边最大 ...

  3. poj-1201 Intervals(差分约束)

    题目链接: Intervals Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 24379   Accepted: 9274 ...

  4. POJ - 1201 Intervals(差分约束+最短路)

    题目链接:点击查看 题目大意:给定n个闭区间[ai,bi]和n个整数ci,你需要构造一个整数集合Z,使得Z中满足所有的ai<=x<=bi的整数不少于ci个,求出这样的整数集合Z最少包含多少 ...

  5. POJ 1716 Integer Intervals 差分约束

    题目:http://poj.org/problem?id=1716 1 #include <stdio.h> 2 #include <string.h> 3 #include ...

  6. POJ1201 Intervals 【差分约束】

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

  7. poj1201(差分约束+SPFA)

    看到这道题,其实就是和poj1716是差不多的 题意:给出n个闭整数区间[ai,bi]和n个整数C1,.,cn.计算具有区间[ai,bi]的至少ci公共元素的整数集Z的最小大小,对于每一个i=1,2, ...

  8. POJ1201基础差分约束

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

  9. 差分约束 1:pku 1201 Intervals 2:pku 1364 King 3:hdu 1534

    一个很好的差分约束总结:http://972169909-qq-com.iteye.com/blog/1185527 第一:  感觉难点在于建图  第二:  ①:对于差分不等式,a - b <= ...

最新文章

  1. Vertica 分区表设计(续)
  2. [Nginx]用Nginx实现与应用结合的訪问控制 - 防盗链
  3. Java——集合的基本功能测试
  4. [转载] 百科全说——潘怀宗:“认识”食品添加剂(10-10-20)
  5. 鸿蒙os2.0怎么报名,我想问一下各位,怎么报名鸿蒙os2.0
  6. POJ 1014 Dividing
  7. editor does not contain a main type的解决方案
  8. 窗口潜水面罩行业调研报告 - 市场现状分析与发展前景预测(2021-2027年)
  9. Microsoft Dynamics CRM MVP
  10. 八.nginx网站服务实践应用
  11. 计算机系统基础lab2(二进制炸弹实验)
  12. 冰点还原精灵软件功能及优势
  13. linux make menuconfig快速查找配置项
  14. iOS 16描述文件升级方法 iOS 16升级描述文件下载
  15. MySQL 5.7新特性:并行复制原理(MTS)
  16. 关于刷微信投票的js代码
  17. PDF怎么转图片?快把这些方法收好
  18. 安卓玩机搞机技巧综合资源-----修改rom 制作rom 解包rom的一些问题解析【二十一】
  19. NeXT,NEXTSTEP,OPENSTEP,Cocoa,Cocoa Touch,GNUstep,xcode
  20. 使用java获取硬盘序列号

热门文章

  1. 多线程 java 实例_Java多线程实例学习
  2. python读取math_怎么使用python安装math库?怎么用?
  3. 一定要会的synchronized关键字的用法
  4. qt 一个线程接收数据 主线程更新界面 会造成界面退出 怎么解决_打造一个好产品...
  5. postgresql的特点_PG:PostgreSQL的一些简单操作
  6. vs2013怎么清理解决方案_厕所漏水怎么办?厕所免拆砖防水维修方案
  7. 数据库开发技术java方向_Java开发工程师(Web方向) - 03.数据库开发 - 第5章.MyBatis...
  8. 西安理工大学 计算机考研不分专硕学硕吗,2021年西安理工大学计算机科学与工程学院考研专业目录_研究生考试范围 - 学途吧...
  9. 2万人同时访问 nodejs_面向前端工程师的Nodejs入门手册(一)
  10. HDLBits答案(12)_Verilog移位寄存器