Integer Intervals
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 14913 | Accepted: 6310 |
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.
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
题意:n行,每行的a,b代表一条线段的两个端点。
求一个序列的长度满足每个a,b至少有两个点在序列里面。
样例序列为1 2 4 5
做法:隐藏条件b>a
S[ai - 1] <= S[bi] - 2
S[i] <= S[i - 1] + 1
S[i - 1] <= S[i]
//china no.1
#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <stack>
#include <cstring>
#include <queue>
#include <list>
#include <stdio.h>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <sstream>
#include <functional>
using namespace std;
#define pi acos(-1)
#define endl '\n'
#define rand() srand(time(0));
#define me(x) memset(x,0,sizeof(x));
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define close() ios::sync_with_stdio(0);
typedef long long LL;
const int INF=0x3f3f3f3f;
const LL LINF=0x3f3f3f3f3f3f3f3fLL;
const int dx[]={-1,0,1,0,-1,-1,1,1};
const int dy[]={0,1,0,-1,1,-1,1,-1};
const int maxn=1e3+5;
const int maxx=1e5+100;
const double EPS=1e-7;
const int MOD=10000007;
#define mod(x) ((x)%MOD);
template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}
template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}
template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}
template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}
//typedef tree<pt,null_type,less< pt >,rb_tree_tag,tree_order_statistics_node_update> rbtree;
long long gcd(long long a , long long b){if(b==0) return a;a%=b;return gcd(b,a);}
#define FOR(x,n,i) for(int i=x;i<=n;i++)
#define FOr(x,n,i) for(int i=x;i<n;i++)
#define W while
int n,x,y,k,m,a,ni,ki,b,c,minn,maxc;
char oi[5];
struct node
{
int to,w,next;
}E[maxx*40];
int head[maxx];
int d[maxx],inq[maxx],num[maxx];
inline int Scan()
{
int res=0,ch,flag=0;
if((ch=getchar())=='-')flag=1;
else if(ch>='0' && ch<='9')res=ch-'0';
while((ch=getchar())>='0'&&ch<='9')res=res*10+ch-'0';
return flag ? -res : res;
}
void init()
{
memset(head,-1,sizeof(head));
k=0;
for(int i=0;i<maxx;i++)
{
inq[i]=0;
d[i]=-INF;
}
}
void add(int from,int to,int w)
{
E[k].to=to;
E[k].w=w;
E[k].next=head[from];
//cout<<E[k].to<<" "<<E[k].w<<endl;
head[from]=k++;
}
int SPFA()
{
int s=0;
queue<int >Q;
Q.push(s),d[s]=0,inq[s]=1;
me(num);
while(!Q.empty())
{
int now=Q.front();
//cout<<now<<endl;
Q.pop();
inq[now]=0;
// ++num[now];
//if(num[now]>n+1) break;
for(int i=head[now];i!=-1;i=E[i].next)
{
int v=E[i].to,w=E[i].w;
// cout<<v<<endl;
if(d[v]<d[now]+w)
{
d[v]=d[now]+w;
if(inq[v]==1) continue;
inq[v]=1;
Q.push(v);
}
}
}
cout<<d[minn]<<endl;
}
int main()
{
W(cin>>n)
{
maxc=INF,minn=0;
init();
W(n--)
{
cin>>a>>b;
b++;
add(a,b,2);
minn=max(minn,b);
maxc=min(maxc,a);
}
//cout<<minn<<" "<<maxc<<endl;
FOR(0,minn-1,i)
{
add(i,i+1,0);
add(i+1,i,-1);
}
SPFA();
}
}