poj1716 Integer Intervals

来源:互联网 发布:气象观测数据怎么画图 编辑:程序博客网 时间:2024/06/05 19:09

Integer Intervals
Time Limit: 1000MS Memory Limit: 10000KTotal 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

43 62 40 24 7

Sample Output

4

Source

CEOI 1997

一看就是差分约束,然后元素是两个,也就是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
原创粉丝点击