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. onselectstart属性解决双击出现的蓝色区域

    小伙伴们对 onselect  这个事件应该不陌生吧(在元素中的文本被选中时触发),但当同事问我onselectstart 这个事件的时候,我是一脸蒙蔽,心想,这难道不是随便起的一个方法名吗2333 ...

  2. [Postman]Postman导航(3)

    Postman提供了一个多窗口和多标签界面,供您使用API​​. 此界面设计为您提供尽可能多的API空间. 侧边栏 邮差侧边栏可让您查找和管理请求和集合.侧边栏有两个主要选项卡:  历史记录  和   ...

  3. 【Spark调优】大表join大表,少数key导致数据倾斜解决方案

    [使用场景] 两个RDD进行join的时候,如果数据量都比较大,那么此时可以sample看下两个RDD中的key分布情况.如果出现数据倾斜,是因为其中某一个RDD中的少数几个key的数据量过大,而另一 ...

  4. Vue的基本使用

    VUE vue挂载点 el:"标签id" vue绑定属性 :v-model:"属性值" vue绑定事件 @click:"事件名" vue基本 ...

  5. [原创]K8Cscan插件之Weblogic漏洞扫描&通用GetShell Exploit

    [原创]K8 Cscan 大型内网渗透自定义扫描器 https://www.cnblogs.com/k8gege/p/10519321.html Cscan简介:何为自定义扫描器?其实也是插件化,但C ...

  6. session的一些笔记

    HttpSession hs = request.getSession();//以键值对方式存储数据在session中hs.setAttribute("code", code);/ ...

  7. [译]聊聊C#中的泛型的使用(新手勿入)

    写在前面 今天忙里偷闲在浏览外文的时候看到一篇讲C#中泛型的使用的文章,因此加上本人的理解以及四级没过的英语水平斗胆给大伙进行了翻译,当然在翻译的过程中发现了一些问题,因此也进行了纠正,当然,原文的地 ...

  8. skywalking部署

    官方文档:Setup java agent Backend and UI 下载地址:http://skywalking.apache.org/downloads/ 解压后目录 部署UI和收集器 进入w ...

  9. 上传文件报错--Unable to find 'struts.multipart.saveDir' property setting.

    struts2 上传文件时,有时候会报这个错误. Unable to find 'struts.multipart.saveDir' property setting. Defaulting to j ...

  10. CSS从零开始(1)--CSS基础语法

    1.CSS语法 CSS规则有两个主要部分构成:选择器,以及一条或多条说明. 例如:selector{declaration1;declaration2;declaration3;......;} 注: ...