Intervals
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 20779   Accepted: 7863

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

题目大意:

n行。每行a,b,c,表示在区间a,b内要找c个数,问你总共至少要找多少个数?

解题思路:

差分约束系统。

在本题中。假设[a,b]中要找c个元素。那么:s[b]-s[a-1]>=c。我们能够推得:s[a-1] - s[b] <= -c



同一时候。因为每个值上最多仅仅能含有一个元素。那么:s[i] - s[i-1]<=1 ,又因为s[i] - s[i-1]>=0 推得:s[i-1] - s[i] <=0



这样:我们有了三个约束不等式:

s[a-1] - s[b] <= -c

s[i] - s[i-1]<=1 

s[i-1] - s[i] <=0

于是:假设起点为from,终点为to。我们仅仅要求出:s[to] -s[from-1] >= M就能够了,

因此求出 s[from-1]-s[to]<=-M,即求to 到 from-1 的最短路径,

注意:因为i<0,所以i-1可能小于0,因此所有向右平移1位。

解题代码:

#include <iostream>
#include <queue>
#include <cstdio>
using namespace std; const int maxn=50000;
const int inf=0x3f3f3f3f; struct edge{
int u,v,w,next;
}e[maxn*10]; int head[maxn*2+10],dist[maxn*2+10],cnt;
int n,from,to; void initial(){
cnt=0;
from=inf,to=0;
for(int i=0;i<=maxn;i++) head[i]=-1;
} void adde(int u,int v,int w){
u++;v++;
e[cnt].u=u,e[cnt].v=v,e[cnt].w=w,e[cnt].next=head[u],head[u]=cnt++;
} void input(){
int u,v,w;
//[v]-[u-1]>=w [u-1]-[v]<=-w
for(int i=0;i<n;i++){
scanf("%d%d%d",&u,&v,&w);
adde(v,u-1,-w);
if(u<from) from=u;
if(v>to) to=v;
}
//0<=[i]-[i-1]<=1
for(int i=from;i<=to;i++){
adde(i-1,i,1);
adde(i,i-1,0);
}
} bool spfa(int from){
int s=from,num[maxn];
bool visited[maxn];
for(int i=0;i<=maxn;i++){
num[i]=0;
dist[i]=inf;
visited[i]=false;
}
queue <int> q;
q.push(s);
visited[s]=true;
dist[s]=0;
while(!q.empty()){
s=q.front();
q.pop();
for(int i=head[s];i!=-1;i=e[i].next){
int d=e[i].v;
if(dist[d]>dist[s]+e[i].w){
dist[d]=dist[s]+e[i].w;
if(!visited[d]){
visited[d]=true;
q.push(d);
num[d]++;
if(num[d]>n) return false;
}
}
}
visited[s]=false;
}
return true;
} void solve(){
//get [to]-[from-1]>=M; [from-1]-[to]<=-M
spfa(to+1);
cout<<-dist[from]<<endl;
} int main(){
while(scanf("%d",&n)!=EOF){
initial();
input();
solve();
}
return 0;
}

POJ 1201 Intervals(图论-差分约束)的更多相关文章

  1. POJ 1201 Intervals【差分约束】

    传送门:http://poj.org/problem?id=1201 题意: 有n个如下形式的条件:,表示在区间[, ]内至少要选择个整数点.问你满足以上所有条件,最少需要选多少个点? 思路:第一道差 ...

  2. poj 1201 Intervals(差分约束)

    做的第一道差分约束的题目,思考了一天,终于把差分约束弄懂了O(∩_∩)O哈哈~ 题意(略坑):三元组{ai,bi,ci},表示区间[ai,bi]上至少要有ci个数字相同,其实就是说,在区间[0,500 ...

  3. POJ 1201 Intervals (经典) (差分约束)

    <题目链接> 题目大意:给你$n$段区间,$a_i,b_i,c_i$ 表示在 $[a_i,b_i]$ 区间内至少要选择$c_i$个点.现在问你在满足这n个条件的情况下,最少要选多少个点? ...

  4. 【题解】 POJ 1201 Intervals(差分约束)

    懒得复制,戳我戳我 Solution: 这道题就是一个板子题 抽象成第\(a\)至第\(b\)间选择数的个数为\(c\),我们就可以用前缀和来表示,这样就可以得到不等式\(s[b]-s[a-1]> ...

  5. POJ 1201 Intervals(差分约束 区间约束模版)

    关于差分约束详情可阅读:http://www.cppblog.com/menjitianya/archive/2015/11/19/212292.html 题意: 给定n个区间[L,R], 每个区间至 ...

  6. poj 1201 Intervals【差分约束+spfa】

    设s为前缀和,首先显然的条件是\[ s_{bi}-s_{ai-1}>=c \],然后隐含的是\[ s_i-s_{i-1}>=0 s_i-s_{i-1}<=1 \] 然后根据差分约束, ...

  7. POJ 1201 Intervals (差分约束,最短路)

    题意: 有一个集合Z,其元素都是整整数,但是数量未知.现有n个约束,形如 [a,b]=c 表示整数区间[a,b]中有c个元素在Z中出现.问集合Z最小可能含多少个元素? 思路: 对于所给的区间 cnt[ ...

  8. poj 1201 Intervals(差分约束)

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

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

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

  10. POJ 1201 Intervals || POJ 1716 Integer Intervals 差分约束

    POJ 1201 http://poj.org/problem?id=1201 题目大意: 有一个序列,题目用n个整数组合 [ai,bi,ci]来描述它,[ai,bi,ci]表示在该序列中处于[ai, ...

随机推荐

  1. OpenSSL 与 SSL 数字证书概念贴

    SSL/TLS 介绍见文章 SSL/TLS原理详解(http://seanlook.com/2015/01/07/tls-ssl). 如果你想快速自建CA然后签发数字证书,请移步 基于OpenSSL自 ...

  2. Http Get Post put delete

    HTTP POST GET 本质区别详解一 原理区别 一般在浏览器中输入网址访问资源都是通过GET方式:在FORM提交中,可以通过Method指定提交方式为GET或者POST,默认为GET提交 Htt ...

  3. Android 常用UI控件之TabHost(1)TabHost的两种布局方式

    TabHost是Android中的tab组件. TabHost布局文件的基本结构 TabHost下有个layout,这个layout中有TabWidget与FrameLayout.TabWidget是 ...

  4. BMS 项目过程中遇到的问题

    环境搭建的问题 Git的ssh私人密钥问题, 路劲不正确的话使用ssh方式连接github进行远程push或clone会出现需要输入密码而怎么输入都不正确的情况,这个时候使用下面的办法: http方面 ...

  5. C#技术------垃圾回收机制(GC)

    GC的前世与今生 虽然本文是以.NET作为目标来讲述GC,但是GC的概念并非才诞生不久.早在1958年,由鼎鼎大名的图林奖得主John McCarthy所实现的Lisp语言就已经提供了GC的功能,这是 ...

  6. usaco4.12Fence Rails(迭代加深)

    为了这题还去学了下迭代加深 回来还是不会写 只好参考各大神的代码及题解了 二分枚举最大可以切的块数 然后就是各种分析及优化 USACO题解里写了7个优化.. 问题分析 抽象一下就可以发现,算法的本质是 ...

  7. eclipse 批量 查询 替换

    对于 要对多个jsp 或 java文件中的 某个字符串 方法 参数 之类的进行统一更改,一直很头疼,直到昨天用了 urlrewriter 的 xml 配置文件 才恍然大悟,可以用 regular ex ...

  8. 理解SVG坐标系统和变换: 建立新视窗

    在SVG绘制的任何一个时刻,你可以通过嵌套<svg>或者使用例如<symbol>的元素来建立新的viewport和用户坐标系.在这篇文章中,我们将看一下我们如何这样做,以及这样 ...

  9. zookeeper实现分布锁

    分布式锁服务在大家的项目中或许用的不多,因为大家都把排他放在数据库那一层来挡.当大量的行锁.表锁.事务充斥着数据库的时候.一般web应用很多的瓶颈都在数据库上,这里给大家介绍的是减轻数据库锁负担的一种 ...

  10. 找出Java进程中大量消耗CPU

    原文:https://github.com/oldratlee/useful-shells useful-shells 把平时有用的手动操作做成脚本,这样可以便捷的使用. show-busy-java ...