poj 1716 Integer Intervals(差分约束||贪心)(中等)

Integer Intervals
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 13229   Accepted: 5613

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
思路:
这题贪心和差分约束都可以,在poj上交wa了6变,不是我的问题,是系统傻逼。。。这类型的题要写出自己的风格

代码:
#include<iostream>
using namespace std;
//#define maxn 10001
#define inf 20000

struct edge
{
    int u,v;
}edge[10001];
int dist[10001];
int l,r;
int n;

int main(int i)
{
    while(cin>>n)
    {
        l=inf,r=0;
        for(i=0;i<n;i++)
        {
            int a,b;
            cin>>a>>b;
            edge[i].u=a,edge[i].v=b+1;
            if(edge[i].u<l) l=edge[i].u;
            if(edge[i].v>r) r=edge[i].v;
            dist[i]=0;
        }
        int flag=1;
        while(flag)
        {
            flag=0;
            for(i=0;i<n;i++)
            {
                if(dist[edge[i].u]>dist[edge[i].v]-2)
                {
                    dist[edge[i].u]=dist[edge[i].v]-2;
                    flag=1;
                }
            }
            for(i=l;i<l;i++)
            {
                if(dist[i+1]>dist[i]+1)
                {
                    dist[i+1]=dist[i]+1;
                    flag=1;
                }
            }
            for(i=r-1;i>=l;i--)
            {
                if(dist[i]>dist[i+1])
                {
                    dist[i]=dist[i+1];
                    flag=1;
                }
            }
        }
        cout<<dist[r]-dist[l]<<endl;
    }
    return 0;
}

贪心:
这个模型建的好,速度更快
//260K	141MS
#include<iostream>
#include<algorithm>
using namespace std;

typedef struct edge
{
    int s,e;
}edge;
int n;
int e1,e2;

bool cmp(edge a,edge b)
{
    if(a.e!=b.e)
        return a.e<b.e;
    return a.s<b.s;
}

int main()
{
    while(cin>>n)
    {
        int sum=2;
        edge *t=new edge[n];
        for(int i=0;i<n;i++)
            cin>>t[i].s>>t[i].e;
        sort(t,t+n,cmp);
        e1=t[0].e-1,e2=t[0].e;
        for(int k=1;k<n;k++)
        {
            if(e1>=t[k].s)
                continue;
            else if(e1<t[k].s && e2>=t[k].s)
            {
                e1=e2;
                e2=t[k].e;
                sum++;
            }
            else
            {
                sum+=2;
                e1=t[k].e-1;
                e2=t[k].e;
            }
        }
        cout<<sum<<endl;
        delete t;
    }
    return 0;
}




猜你喜欢

转载自blog.csdn.net/kaisa158/article/details/47153269