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

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

Sample Output

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)$的前缀和
同时,这个图一定是联通的,因此不用新建超级源点
 
#include<cstdio>
#include<queue>
#include<cstring>
#define INF 1e8+10
using namespace std;
const int MAXN=1e6+;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,MAXN,stdin),p1==p2)?EOF:*p1++)
char buf[MAXN],*p1=buf,*p2=buf;
inline int read()
{
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
struct node
{
int u,v,w,nxt;
}edge[MAXN];
int head[MAXN],num=;
int maxx=-INF,minn=INF;
int dis[MAXN],vis[MAXN];
inline void AddEdge(int x,int y,int z)
{
edge[num].u=x;
edge[num].v=y;
edge[num].w=z;
edge[num].nxt=head[x];
head[x]=num++;
}
int SPFA()
{
queue<int>q;
memset(dis,-0xf,sizeof(dis));
dis[minn]=;q.push(minn);
while(q.size()!=)
{
int p=q.front();q.pop();
vis[p]=;
for(int i=head[p];i!=-;i=edge[i].nxt)
{
if(dis[edge[i].v]<dis[p]+edge[i].w)
{
dis[edge[i].v]=dis[p]+edge[i].w;
if(vis[edge[i].v]==)
vis[edge[i].v]=,q.push(edge[i].v);
}
}
}
printf("%d",dis[maxx]);
}
int main()
{
#ifdef WIN32
freopen("a.in","r",stdin);
#else
#endif
memset(head,-,sizeof(head));
int N=read();
for(int i=;i<=N;i++)
{
int x=read(),y=read(),z=read();
AddEdge(x,y+,z);
maxx=max(y+,maxx);
minn=min(x,minn);
}
for(int i=minn;i<=maxx-;i++)
{
AddEdge(i+,i,-);
AddEdge(i,i+,);
}
SPFA();
return ;
}

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. 如何把web.xml中的context-param、Servlet、Listener和Filter定义添加到SpringBoot中

    把传统的web项目迁移到SpringBoot中,少不了web.xml中的context-param.Servlet.Filter和Listener等定义的迁移. 对于Servlet.Filter和Li ...

  2. iTerm2 使用笔记

    iTerm2 使用了1年多了,一些功能其实还没有主动去发现,这次接着项目忙完的空闲时间整理一下tips,提高工作效率,方便以后查阅. 一.几个术语 从小到大:session > pane > ...

  3. 页面css样式找不到问题

    出现了一个页面没有样式的问题: 问题: 1.路径不对, 可以打开页面f12看样式是否找到 检查路径是否正确. 2.样式没引全或者没引对. 查看引入的样式是否正确或缺少样式. 3.路径明明写对了却404 ...

  4. pickle模块及其API

    模块:pickle 所包含API列表: pickle.dumps : 把任意对象序列化成一个bytes pickle.dump : 直接把对象序列化后写入一个file-like Object pick ...

  5. 性能瓶颈之Target

    最常见的性能问题都发生在向目标数据库写数据的时候   常见的与目标数据库性能有关的问题有: 1) 数据库的checkpoint间隔太小 2) 数据库网络包太小 3) 在进行大批量数据加载时的问题   ...

  6. Jenkins 忘记admin密码拯救方法

    突然有一日发现自己忘记了jenkins的管理员密码,因为我一直登录的是另外一个非管理员账户.如果出现必须要使用管理员账户操作的,比如用户管理那里的,必须要管理员账号吧,这就尴尬了. 很方的我打开安装j ...

  7. shiro源码篇 - shiro的session创建,你值得拥有

    前言 开心一刻 开学了,表弟和同学因为打架,老师让他回去叫家长.表弟硬气的说:不用,我打得过他.老师板着脸对他说:和你打架的那位同学已经回去叫家长了.表弟犹豫了一会依然硬气的说:可以,两个我也打得过. ...

  8. git在工作中的用法总结-使用篇

    上一篇介绍了git的环境安装配置,本篇对git在工作中常用的用法进行总结,已满足大部分的日常工作需求,对于其他的一些git命令用法在今后使用到时我也会更新上来,文中如有错误,欢迎大家指出来,谢谢~ 一 ...

  9. Deep learning with Python 学习笔记(11)

    总结 机器学习(machine learning)是人工智能的一个特殊子领域,其目标是仅靠观察训练数据来自动开发程序[即模型(model)].将数据转换为程序的这个过程叫作学习(learning) 深 ...

  10. Perl的比较操作符

    比较操作符 perl的比较操作符和bash完全相反.数值比较采用符号,字符串比较采用字母. 数值 字符串 意义 ----------------------------- == eq 相等 != ne ...