POJ 1716 Integer Intervals(差分约束)


Integer Intervals

Time Limit: 1000MS

 

Memory Limit: 10000K

Total Submissions: 14689

 

Accepted: 6226

Description

An integer interval [a,b], a < b, is a set of all consecutive integersbeginning with a and ending with b. 
Write a program that: finds the minimal number of elements in a set containingat least two different integers from each interval.

Input

The first line of the input contains the number of intervals n, 1 <= n <=10000. Each of the following n lines contains two integers a, b separated by asingle space, 0 <= a < b <= 10000. They are the beginning and the endof an interval.

Output

Out put the minimal number of elements in a set containing at least two differentintegers from each interval.

SampleInput

4

3 6

2 4

0 2

4 7

Sample Output

4



差分约束总结:传送门

题意:给出数轴上的n个闭合int型区间。现在要在数轴上任意取一堆元素,构成一个元素集合V,要求给出的每个区间和元素集合V的交集至少有两个不同的元素,求集合V最小的元素个数。

思路:(1)贪心

           (2)差分约束:

对于差分约束,我们可以这样考虑,令sum[x]为[0,x]内所在集合V中元素个数,

那么我们就能很容易得到:对于每个区间【a,b】,sum[b+1]-sum[a]>=2。

但千万不能忘了隐含的约束条件:0<=sum[i+1]-sum[i]<=1.

综上所述:不等数组统一化为:sum[b+1]-sum[a]>= 2         建边:add(a,b+1,2)

                                                  sum[i+1]-sum[i]>= 0            建边:add(i,i+1,0)

                                                  sum[i]-sum[i+1]>= -1           建边:add(i+1,i,-1)

建边完毕后跑个最长路即可。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define f(i,a,b) for(int i=(a);i<(b);++i)
typedef long long ll;
const int maxn= 10005;
const int mod = 10007;
const int INF = 0x3f3f3f3f;
const double eps = 1e-6;
#define rush() int T;scanf("%d",&T);while(T--)

int dis[maxn];
int vis[maxn];
int num[maxn];
int head[maxn];
int n,cnt;

struct node
{
    int v,w,next;
} e[maxn*3];

void add(int u,int v,int w)
{
    e[cnt].v=v;
    e[cnt].w=w;
    e[cnt].next=head[u];
    head[u]=cnt++;
}


void init()
{
    cnt=0;
    mst(head,-1);
}


int spfa(int s,int t)
{
    mst(vis,0);
    mst(num,0);
    for(int i=0;i<=t;i++)
    {
        dis[i]=-INF;
    }
    queue<int>q;
    vis[s]=1;
    dis[s]=0;
    num[s]++;
    q.push(s);
    while(q.size())
    {
        int now=q.front();
        q.pop();
        vis[now]=0;
        for(int i=head[now]; i!=-1; i=e[i].next)
        {
            int v=e[i].v;
            int w=e[i].w;
            if(dis[v]<dis[now]+w)
            {
                dis[v]=dis[now]+w;
                if(!vis[v])
                {
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
    return dis[t];
}

int main()
{
    int a,b;
    while(~scanf("%d",&n))
    {
        init();
        int Max=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&a,&b);
            add(a,b+1,2);
            Max=max(Max,b+1);
        }
        for(int i=0;i<=Max;i++)
        {
            add(i,i+1,0);
            add(i+1,i,-1);
        }
        int ans=spfa(0,Max);
        printf("%d\n",ans);
    }
    return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值