poj1716 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 integers beginning with a and ending with b. 
Write a program that: finds the minimal number of elements in a set containing at 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 a single space, 0 <= a < b <= 10000. They are the beginning and the end of an interval.

Output

Output the minimal number of elements in a set containing at least two different integers from each interval.

Sample Input

4
3 6
2 4
0 2
4 7

Sample Output

4

Source


一看就是差分约束,然后元素是两个,也就是dis[v]-dis[u]>=2.

然后这里不要忘记加一些隐藏的限制条件,一看intervals就知道不要忘记dis[v]-dis[v-1]>=0且<=1.

然后建完图就是求一个由右边界到左边界的一个最短路,最后不要忘了取一个负值。

//#include <bits/stdc++.h>
#include <cstdio>
#include <queue>
using namespace std;
const int MAXN = 1e4+7;
const int inf = 1e9;
int n;
int s,e;
bool vis[MAXN];
int dis[MAXN];
vector<pair<int,int> >head[MAXN];
void spfa()
{
    for(int i=e;i<=s;++i)
    {
        dis[i]=inf;
        vis[i]=0;
    }
    queue<int>q;
    q.push(s);
    dis[s]=0;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        vis[u]=0;
        for(int i=0,l=head[u].size();i<l;++i)
        {
            int v = head[u][i].first;
            int w = head[u][i].second;
            if(dis[u]+w<dis[v])
            {
                dis[v]=dis[u]+w;
                if(!vis[v])
                {
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
    printf("%d\n",-dis[e]);
}



int main()
{
    int n;
    scanf("%d",&n);
    int u,v;
    s=0,e=inf;
    for(int i = 0;i < n; ++ i)
    {
        scanf("%d%d",&u,&v);
        v++;
        s=max(s,v);
        e=min(e,u);
        head[v].push_back(make_pair(u,-2));
    }
    for(int i=e+1;i<=s;++i)
    {
        head[i-1].push_back(make_pair(i,1));
        head[i].push_back(make_pair(i-1,0));
    }
    spfa();
    return 0;
}








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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值