Intervals 【区间差分约束】基础

You are given n closed, integer intervals ai,biai,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,biai,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,biai,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

//别的例题 2、区间约束
【例题2】给定n(n <= 50000)个整点闭区间和这个区间中至少有多少整点需要被选中,每个区间的范围为[ai, bi],并且至少有ci个点需要被选中,其中0 <= ai <= bi <= 50000,问[0, 50000]至少需要有多少点被选中。
例如3 6 2 表示[3, 6]这个区间至少需要选择2个点,可以是3,4也可以是4,6(总情况有 C(4, 2)种 )。

  这类问题就没有线性约束那么明显,需要将问题进行一下转化,考虑到最后需要求的是一个完整区间内至少有多少点被选中,试着用d[i]表示[0, i]这个区间至少有多少点能被选中,根据定义,可以抽象出 d[-1] = 0,对于每个区间描述,可以表示成d[ bi ]  - d[ ai - 1 ] >= ci,而我们的目标要求的是 d[ 50000 ] - d[ -1 ] >= T 这个不等式中的T,将所有区间描述转化成图后求-1到50000的最长路。
  这里忽略了一些要素,因为d[i]描述了一个求和函数,所以对于d[i]和d[i-1]其实是有自身限制的,考虑到每个点有选和不选两种状态,所以d[i]和d[i-1]需要满足以下不等式:  0 <= d[i] - d[i-1] <= 1   (即第i个数选还是不选)
  这样一来,还需要加入 50000*2 = 100000 条边,由于边数和点数都是万级别的,所以不能采用单纯的Bellman-Ford ,需要利用SPFA进行优化,由于-1不能映射到小标,所以可以将所有点都向x轴正方向偏移1个单位(即所有数+1)。

题意:有n个区间,给出每个区间的左端点和右端点以及在该区间至少有多少个数在集合Z里面,现在让你求出集合Z里面最少元素个数。

思路:设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[Maxr] - S[Maxl-1] >= ans 即S[Maxl-1] - S[Maxr] <= -ans。 若以Maxr为源点 ,而-ans就为Maxr到Maxl-1的最短路径的相反数,即-dist[Maxl-1]。

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#define CLR(a,b) memset((a),(b),sizeof(a))
#define inf 0x3f3f3f3f
#define mod 100009
#define LL long long
#define MAXN  50000+10
#define MAXM 200000+100
#define ll o<<1
#define rr o<<1|1
#define lson o<<1,l,mid
#define rson o<<1|1,mid+1,r
using namespace std;
void read(int &x){
    x=0;char c;
    while((c=getchar())<'0');
    do x=x*10+c-'0';while((c=getchar())>='0');
}
struct Edge {
    int from,to,val,next;
}edge[MAXM];
int head[MAXN],top;
int vis[MAXN],dis[MAXN];
int qcnt[MAXN];
int n,m;
int le,ri;

void addedge(int a,int b,int c)
{
    Edge e={a,b,c,head[a]};
    edge[top]=e;head[a]=top++;
}

void init()
{
    memset(head,-1,sizeof(head));
    top=0;
    memset(qcnt,0,sizeof(qcnt));
    le=50005; ri=0;
}

void getmap()   //* 核心 ,根据 差分约束条件 建图 
{
    for(int i=0;i<n;i++)
    {
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        a++;b++;// 将所有的序号都向右移动一位
        addedge(b,a-1,-c);
        le=min(le,a); // 进行了优化,只要找出此过程中最左值和最右值就行了
        ri=max(ri,b);
    }

    for(int i=le;i<=ri;i++)
    {
        addedge(i,i-1,0);
        addedge(i-1,i,1);
    }
}
int  spfa(int st,int ed)
{
    for(int i=le-1;i<=ri;i++)
    dis[i]= i==st ?0:inf;
    memset(vis,0,sizeof(vis));

    vis[st]=1; dis[st]=0;
    queue<int>Q; Q.push(st);
    while(!Q.empty()) {
        int now=Q.front();Q.pop();
        if(qcnt[now]++>n) return 0; //有环 
        vis[now]=0;
        for(int i=head[now];i!=-1;i=edge[i].next)
        {
            Edge e=edge[i];
            if(dis[e.to]>dis[now]+e.val)
            {
                dis[e.to]=dis[now]+e.val;
                if(!vis[e.to])
                {
                vis[e.to]=1;
                Q.push(e.to);
                }
            }
        }
    }

    if(dis[ed]!=inf) return 1; // 有解
    else return -1;  // 无解
}
int main()
{
    while(~scanf("%d",&n))
    {
        m=n;
        init();
        getmap();
        spfa(ri,le-1);
        printf("%d\n",-dis[le-1]);// 因为边的权值都是负的
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值