poj 1716 Integer Intervals(差分约束||贪心)(中等)
2015-07-30 17:21
579 查看
Integer Intervals
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
Sample Output
思路:
这题贪心和差分约束都可以,在poj上交wa了6变,不是我的问题,是系统傻逼。。。这类型的题要写出自己的风格
代码:
贪心:
这个模型建的好,速度更快
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 13229 | Accepted: 5613 |
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 ; 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; }
相关文章推荐
- CSS侧边工具栏-背景图片法
- 2015 Multi-University Training Contest 4 (hdu 5335、5336)模拟
- 虚继承
- ios 开发小技巧
- HttpURLConnection及HttpClient选择(转)
- OC学习----可变数组与不可变数组
- swift创建单例
- javascript面向对象编程
- mybatis1,mybaits连接数据库
- 能屈能伸的“物”,弹性物联网能力的意义
- windows内核研究
- 51单片机自定义多个软件定时器
- MySQL数据表的CRUD(増、删、改、查)操作
- Android实现自适应正方形GridView
- Spark往Elasticsearch读写数据
- PHP中CURL方法curl_setopt()函数的一些参数
- spark shell学习笔记
- Excel开源组件介绍
- Activity在onDestory()后,activity的实例还存在吗
- Android: mediaplayer went away with unhandled events