1716 Integer Intervals
来源:互联网 发布:浙江农家乐数据 编辑:程序博客网 时间:2024/05/19 15:44
Description
Write a program that: finds the minimal number of elements in a set containing at least two different integers from each interval.
Input
Output
Sample Input
43 62 40 24 7
Sample Output
4
Source
贪心能过,不过我还是用了差分约束系统,也就是SPFA
1201的简化版
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
const int E=10005;
const int V=10005;
const int INF=1<<25-1;
struct EDGE
{
int link,val,next;
}edge[E*3];
int head[V],dist[V],e,rmin,rmax;
bool vis[V];
void addedge(int a,int b,int c)
{
edge[e].link=b;
edge[e].val=c;
edge[e].next=head[a];
head[a]=e++;
}
int relax(int u,int v,int c)
{
if(dist[v]<dist[u]+c)//不能是<=,会死循环
{
dist[v]=dist[u]+c;
return 1;
}
return 0;
}
void SPFA(int src)
{
memset(vis,false,sizeof(vis));
for(int i=rmin;i<=rmax;i++) dist[i]=-INF;
int top=1;
dist[src]=0;
vis[src]=true;
queue<int> q;
q.push(src);
while(!q.empty())
{
int u,v;
u=q.front();
q.pop();
vis[u]=false;
for(int i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].link;
if(relax(u,v,edge[i].val)==1&&!vis[v])
{
q.push(v);
vis[v]=true;
}
}
}
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
e=0;
memset(head,-1,sizeof(head));
rmin=1<<25-1; rmax=-1;
for(int i=1;i<=n;i++)
{
int a,b,c;
scanf("%d%d",&a,&b);
if(a<rmin) rmin=a;
if(b+1>rmax) rmax=b+1;
addedge(a,b+1,2);
}
for(int i=rmin;i<rmax;i++) addedge(i,i+1,0);
for(int i=rmin;i<rmax;i++) addedge(i+1,i,-1);
SPFA(rmin);
printf("%d/n",dist[rmax]-dist[rmin]);
}
return 0;
}
- POJ 1716 Integer Intervals
- 1716 Integer Intervals
- poj 1716 Integer Intervals
- POJ 1716 Integer Intervals
- poj 1716-Integer Intervals
- POJ 1716 Integer Intervals
- POJ 1716 Integer Intervals
- POJ 1716 Integer Intervals
- POJ 1716 Integer Intervals
- POJ 1716 Integer Intervals
- POJ-1716 Integer Intervals
- poj 1716 Integer Intervals
- 1716 Integer Intervals 解题报告
- poj 1716 贪心 Integer Intervals
- poj 1716 Integer Intervals (贪心)
- POJ 1716 Integer Intervals 笔记
- Integer Intervals
- Integer Intervals
- 关于数据集与源代码
- 端口 使用 查看 pid
- ....帝国同学的截图软件 ----
- 保证速度与心情——pdg转pdf与djvu转pdf大法(不像网上的好多方法那样麻烦,方便快捷,纯傻瓜化操作!)
- .net企业级架构实战之7——Spring.net整合Asp.net mvc
- 1716 Integer Intervals
- windows下编译FFmpeg源代码
- 开始 Useless browser (base on webkit)项目.
- JAVA实例讲解:股指期货交易系统的构建
- java软件工程师培训学习日记------java语言核心基础之面向对象编程
- 控件
- 打包
- 在MFC中创建动态控件的生成与响应
- 二进制_十进制转换