Intervals

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

Total Submission(s): 3559    Accepted Submission(s): 1303
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
 
Author
1384
 
Recommend
Eddy   |   We have carefully selected several similar problems for you:  1529 1531 1548 1534 1317 

题意:给出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>
#include<iostream>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f
int head[100100],vis[1000100],n,cnt;
int dis[100100];
int l,r;
struct node
{
int u,v,val;
int next;
}edge[1001000];
void init()
{
cnt=0;
memset(head,-1,sizeof(head));
l=INF;
r=-1;
}
void add(int u,int v,int val)
{
node E={u,v,val,head[u]};
edge[cnt]=E;
head[u]=cnt++;
}
void getmap()
{
int a,b,c;
for(int i=0;i<n;i++)
{
cin>>a>>b>>c;
l=min(a,l);
r=max(r,b);
add(b,a-1,-c);
}
for(int i=l;i<=r;i++)
{
add(i,i-1,0);
add(i-1,i,1);
}
}
void SPFA()
{
for(int i=l-1;i<=r;i++)
dis[i]=i==r?0:INF;
memset(vis,0,sizeof(vis));
queue<int>q;
q.push(r);
vis[r]=1;
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=0;
for(int i=head[u];i!=-1;i=edge[i].next)
{
node E=edge[i];
if(dis[E.v]>dis[E.u]+edge[i].val)
{
dis[E.v]=dis[E.u]+edge[i].val;
if(!vis[E.v])
{
vis[E.v]=1;
q.push(E.v);
}
}
}
}
cout<<-dis[l-1]<<endl;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
init();
getmap();
SPFA();
}
return 0;
}

hdoj--1384--Intervals(差分约束)的更多相关文章

  1. hdu 1384 Intervals (差分约束)

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

  2. hdu 1384 Intervals (差分约束)

    /* 给你 n 个区间 [Ai, Bi],要求从每一个区间中至少选出 Ci 个数出来组成一个序列 问:满足上面条件的序列的最短长度是多少? 则对于 不等式 f(b)-f(a)>=c,建立 一条 ...

  3. hdoj 1384 Intervals

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

  4. POJ1201 Intervals(差分约束)

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

  5. poj 1716 Integer Intervals (差分约束 或 贪心)

    Integer Intervals Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12192   Accepted: 514 ...

  6. zoj 1508 Intervals (差分约束)

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

  7. poj 1201 Intervals(差分约束)

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

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

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

  9. poj1201 Intervals——差分约束

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

  10. Hdu 1384(差分约束)

    题目链接 Intervals Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

随机推荐

  1. yarn架构——本质上是在做解耦 将资源分配和应用程序状态监控两个功能职责分离为RM和AM

    Hadoop YARN架构解读 原Mapreduce架构 原理架构图如下: 图 1.Hadoop 原 MapReduce 架构 原 MapReduce 程序的流程:首先用户程序 (JobClient) ...

  2. 一个操作oracle的c#类 含分页

    有别于以前的一个OracleHelper,这个版各有所长,MARK下. using System; using System.Data; using System.Data.OracleClient; ...

  3. Thread.setDaemon设置说明

    转载地址:http://blog.csdn.net/m13666368773/article/details/7245570 Thread.setDaemon的用法,经过学习以后了解: 1. setD ...

  4. POJ 3233 矩阵快速幂&二分

    题意: 给你一个n*n的矩阵 让你求S: 思路: 只知道矩阵快速幂 然后nlogn递推是会TLE的. 所以呢 要把那个n换成log 那这个怎么搞呢 二分! 当k为偶数时: 当k为奇数时: 就按照这么搞 ...

  5. Java io 操作

    package tlistpackage; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFou ...

  6. Django后台创建

    1.首先创建Django工程 创建Django有两种方法我用的是pycharm的创建 2.查看url.py 如下 from django.contrib import admin from djang ...

  7. windows phone LongListSelector加载下一页

    LongListSelector利用ListHeader.ListFooter加载上一页和下一页XAML代码: <phone:LongListSelector> <phone:Lon ...

  8. Five Invaluable Techniques to Improve Regex Performance

    Regular expressions are powerful, but with great power comes great responsibility. Because of the wa ...

  9. Android设计模式——单例模式

    1.单例模式就是确保一个类,只有一个实例化对象,而且自行实例化并向整个系统提供这个实例. 2.使用场景: 确保某个类,有且只有一个对象,避免产生对个对象,消耗过多的资源. 2.实现单例模式的重要点: ...

  10. JEE Spring-boot 简单的ioc写法。

    什么是ioc,就是你可能会有一些生活必需品,这些东西你必须要用才能存活.但是你不是每天都回去买,去哪一家点去买.而这些用品会一直放在哪里,每一个商店就是一个容器,包裹着这些物品. 创建ioc项目,首先 ...