Intervals

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3332    Accepted Submission(s):
1227

Problem 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 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个区间的左右端点,和这个区间内至少存在的在集合s中的点的个数,让你求集合s中最少有多少个点
题解:重在找到差分约束的约束条件,将约束条件转化为xj-xi<=k的形式,然后建立一条从i到j权值为k的边;
设maxl为区间的左端点,maxr为区间的右端点,S[i] 表示集合Z里面的元素在区间[0, i ]的个数,Maxl,Maxr分别表示所有区间里面的最左端和最右端,dist[]数组存储源点到某点的最短路。则由题意得限制条件

一 S[right] -  S[left-1] >= least 即[left, right]区间个数不小于least,转换得S[left-1] - S[right] <= least;
二 0 <= S[i] - S[i-1] <= 1转换得 S[i-1] - S[i] <= 0 && S[i] - S[i-1] <= 1。
第二个条件题中并没有给出,需要自己推导,因为仅仅靠题中的条件无法构建一个连通图,也就无法求最短路,因为s[i]表示的是集合Z里面的元素在区间[0, i ]的个数所以s[i]至多比s[i-1]大一也可能相等
然后根据限制条件建图
转化问题:题目需要求的是S[Maxr] - S[Maxl-1] >= ans 即S[Maxl-1] - S[Maxr] <= -ans。 若以Maxr为源点 ,而-ans就为Maxr到Maxl-1的最短路径的相反数,即-dist[Maxl-1]。
 
#include<stdio.h>
#include<string.h>
#include<queue>
#define INF 0x3f3f3f
#define MAX 200000
#include<algorithm>
using namespace std;
int n,ans;
int maxl,maxr;
int vis[MAX],dis[MAX];
int head[MAX];
struct node
{
int u,v,w;
int next;
}edge[MAX];
void add(int u,int v,int w)
{
edge[ans].u=u;
edge[ans].v=v;
edge[ans].w=w;
edge[ans].next=head[u];
head[u]=ans++;
}
void init()
{
ans=0;
maxl=INF;
maxr=0;
memset(head,-1,sizeof(head));
}
void getmap()
{
int i,j,a,b,c;
while(n--)
{
scanf("%d%d%d",&a,&b,&c);
maxl=min(maxl,a);
maxr=max(maxr,b);
add(b,a-1,-c);
}
for(i=maxl;i<=maxr;i++)
{
add(i,i-1,0);
add(i-1,i,1);
}
}
void spfa()
{
int i,j;
queue<int>q;
memset(vis,0,sizeof(vis));
for(i=maxl-1;i<=maxr;i++)//以maxr为源点,也可以以maxl为源点,不过要对建图稍作修改
dis[i]=INF;
dis[maxr]=0;
vis[maxr]=1;
q.push(maxr);
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=0;
for(i=head[u];i!=-1;i=edge[i].next)
{
int top=edge[i].v;
if(dis[top]>dis[u]+edge[i].w)
{
dis[top]=dis[u]+edge[i].w;
if(!vis[top])
{
vis[top]=1;
q.push(top);
}
}
}
}
printf("%d\n",-dis[maxl-1]);
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
init();
getmap();
spfa();
}
return 0;
}

  

 
 

hdoj 1384 Intervals的更多相关文章

  1. POJ 1384 Intervals (区间差分约束,根据不等式建图,然后跑spfa)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1384 Intervals Time Limit: 10000/5000 MS (Java/Others ...

  2. hdu 1384 Intervals (差分约束)

    Intervals Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  3. HDU 1384 Intervals(差分约束)

    Intervals Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  4. POJ 1201 &amp;&amp; HDU 1384 Intervals(差动制动系统)

    职务地址:POJ 1201   HDU 1384 依据题目意思.能够列出不等式例如以下: Sj-Si>=c; Si-S(i-1)>=0; S(i-1)-Si>=-1; 然后用最短路s ...

  5. hdu 1384 Intervals (差分约束)

    Problem - 1384 好歹用了一天,也算是看懂了差分约束的原理,做出第一条查分约束了. 题意是告诉你一些区间中最少有多少元素,最少需要多少个元素才能满足所有要求. 构图的方法是,(a)-> ...

  6. HDOJ 1384 差分约束

    结题报告合集请戳:http://972169909-qq-com.iteye.com/blog/1185527 /*题意:求符合题意的最小集合的元素个数 题目要求的是求的最短路, 则对于 不等式 f( ...

  7. hdu 1384 Intervals

    差分约束系统. 求最小值,用最长路来解决. #include<cstdio> #include<cstring> #include<cmath> #include& ...

  8. HDU 1384 Intervals &洛谷[P1250]种树

    差分约束 差分约束的裸题,关键在于如何建图 我们可以把题目中给出的区间端点作为图上的点,此处应注意,由于区间中被标记的点的个数满足区间加法,这里与前缀和类似,对于区间[L..R]来说,我们加入一条从L ...

  9. HDU 1384 Intervals【差分约束-SPFA】

    类型:给出一些形如a−b<=k的不等式(或a−b>=k或a−b<k或a−b>k等),问是否有解[是否有负环]或求差的极值[最短/长路径].例子:b−a<=k1,c−b&l ...

随机推荐

  1. 关于iOS元旦http,https的规定,官方论坛回应

    先贴原文地址:https://forums.developer.apple.com/thread/48979#146140 原文: eskimoAug 2, 2016 4:17 AM(in respo ...

  2. iOS:iOS开发中用户密码保存位置

    原文来自简书:http://www.jianshu.com/p/4af3b8179136/comments/1294203 如果要实现自动登录,不必每次打开应用都去登录,我们势必要把密码保存到本地.一 ...

  3. POJ 1631 Bridging signals(LIS O(nlogn)算法)

    Bridging signals Description 'Oh no, they've done it again', cries the chief designer at the Waferla ...

  4. Windows phone 之独立存储

    独立存储命名空间的说明:

  5. WinForm控件复杂数据绑定常用数据源(对Combobox,DataGridView等控件DataSource赋值的多种方法)

    开始以前,先认识一下WinForm控件数据绑定的两种形式,简单数据绑定和复杂数据绑定. 1) 简单数据绑定 简单的数据绑定是将用户控件的某一个属性绑定至某一个类型实例上的某一属性.采用如下形式进行绑定 ...

  6. html5时间选择器

    HTML5日期输入类型(date)   分享   分享   分享   分享   分享 在很多页面和web应用中都有输入日期和时间的地方,最典型的是订飞机票,火车票,酒店,批萨等网站. 在HTML5之前 ...

  7. u-boot子目录Makefile分析

    一.概述 u-boot的子目录Makefile是整个Makefile体系的重要组成部分,决定了对应子目录的编译过程. 二.分析 以cpu/arm920t/Makefile为例进行说明 (1)首先,调用 ...

  8. JSP三大指令 /9大内置对象 /Javabean / EL

    一个jsp页面中,可以有0~N个指令的定义! 1. page --> 最复杂:<%@page language="java" info="xxx". ...

  9. 转:靠谱的代码和DRY

    http://www.cppblog.com/vczh/archive/2014/07/15/207658.html 靠谱的代码和DRY 上次有人来要求我写一篇文章谈谈什么代码才是好代码,是谁我已经忘 ...

  10. iOSシステム構成の纏め

    iOSのアーキテクチャ a)       iOSのアーキテクチャは階層化されている最上位レベルでは.iOSはそれが稼働しているハードウェアとアプリケーションの間の仲介役を果たします.アプリケーションが ...