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

5
3 7 3
8 10 3
6 8 1
1 3 1
10 11 1

Sample Output

6

题意:给定X轴上一些区间[ai,bi],其间至少有ci个点,求X轴上至少多少个点。

思路:记得高中是用bellman-ford优化到了NKOJ的第一名。。。。但是差不多搞忘差分约束了。所以现在再来整理总结一下。

此处求最小,显然需要构造T>=S+dist,然后用最长路求。 由于是点段,处理区间最好半开半闭。

#include<queue>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
const int inf=0x7fffffff;
int cnt,n,Max,Min;
int Laxt[maxn],Next[maxn],To[maxn],Len[maxn];
int times[maxn],st[maxn],vis[maxn],inq[maxn];
void update()
{
cnt=;Min=inf;Max=-inf;
memset(Laxt,,sizeof(Laxt));
memset(vis,,sizeof(vis));
memset(inq,,sizeof(inq));
}
void add(int u,int v,int d)
{
Next[++cnt]=Laxt[u];
Laxt[u]=cnt;
To[cnt]=v;
Len[cnt]=d;
}
bool spfa()
{
for(int i=Min-;i<=Max+;i++) st[i]=-inf;
queue<int>q;
q.push(Min); st[Min]=; inq[Min]=;
while(!q.empty()){
int u=q.front(); q.pop(); inq[u]=;
for(int i=Laxt[u];i;i=Next[i]){
int v=To[i];
if(st[v]<st[u]+Len[i]){
st[v]=st[u]+Len[i];
if(!inq[v]){
inq[v]=; vis[v]++; q.push(v);
//if(vis[v]>Max-Min) return false;
}
}
}
} return true;
}
int main()
{
int a,b,c;
while(~scanf("%d",&n)){
update();
for(int i=;i<=n;i++) {
scanf("%d%d%d",&a,&b,&c);
a++;b++;
if(b>Max) Max=b;
if(a-<Min) Min=a-1;
add(a-,b,c);
}
for(int i=Min;i<Max;i++) add(i+,i,-),add(i,i+,);
spfa();
printf("%d\n",st[Max]);
} return ;
}

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

  1. POJ1201 Intervals(差分约束)

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

  2. poj1201 Intervals——差分约束

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

  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. zoj 1508 Intervals (差分约束)

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

  6. poj 1201 Intervals(差分约束)

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

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

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

  8. POJ1201基础差分约束

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

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

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

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

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

随机推荐

  1. android状态栏总结

    针对状态栏的操作,只针对4.4kitKat(含)以上的机型,部分国产rom会失效,目前发现的有华为的EMUI Activity必须是noActionbar主题 本文基于StatusBarUtils略作 ...

  2. Linux文件内容查阅

    直接查阅一个文件的内容:cat/tac/nl命令 cat (concatenate) # cat [-AbEnTv] 选项与參数: -A  :相当於 -vET 的整合选项.可列出一些特殊字符而不是空白 ...

  3. 仰视源代码,实现strcpy

    编程实现字符串的拷贝,不能用库函数. 一般的刚開始学习的人也许能写出来.可是要写的非常完美那就须要基本功了. char* strcpy(char* strDest, const char* strSr ...

  4. Solaris作业管理

    凌晨2点重启数据库 每周晚23点后都需要做备份工作 所有这些都需要作业.这时候数据库压力没那么大. 作业就像是闹钟,可以有一次性计划任务,也有重复性计划任务. 计划任务管理 ----at At [-m ...

  5. struts1.3中使用DispatchAction的一个问题

    近期做项目发现我们公司的项目是用struts1写的,在多方百度下,总有理解了struts1.3的DispatchAction的使用方法 一:struts.xml文件的配置 <?xml versi ...

  6. 【Sprint2 每日Scrum】 第一天(4.22)Sprint2计划会议成果

    Sprint2计划会议成果 从今天起我们就开始正式的Sprint2之旅了,经过上一次Sprint1的冲刺计划和几天的调整,我们已经大致了解了敏捷开发的流程和思想,并将我们的TD学生助手做出了大致的框架 ...

  7. MOS简介

    功率半导体器件机能 MOS管(击穿原因),它采用“超级结”(Super-Junction)结构,故又称超结功率MOSFET.全数字控制是发展趋势,已经在很多功率变换设备中得到应用.既管理了对电网的谐波 ...

  8. hdu5387(2015多校8)--Clock(模拟)

    题目链接:点击打开链接 题目大意:给出一个时间,问在钟表上这个时间的时候.时针和分针的角度,时针和秒针的角度.分针和秒针的角度.假设不是整数以分数的形式输出. 假设依照最小的格来算,那么: 1s对于秒 ...

  9. C递归算法与栈的分析,非全然二叉树遍历分析---ShinePans

                 对于递归,这里面的分析最好当然是用图形的方式来分析了.这里来总结一下 1.首先对于栈的理解: 先进后出,后进先出 先进后出 2.在进行非全然二叉树的存储之后,我们要做的是对其 ...

  10. JSON和JSONP 傻傻分不清楚?

    什么是JSON  前面简单说了一下,JSON是一种基于文本的数据交换方式,或者叫做数据描述格式,你是否该选用他首先肯定要关注它所拥有的优点. JSON的优点: 1.基于纯文本,跨平台传递极其简单: 2 ...