贪心问题
来源:互联网 发布:apache监控工具 编辑:程序博客网 时间:2024/03/28 18:13
F - Integer Intervals
Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64uDescription
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
43 62 40 24 7
Sample Output
4
差分约束问题,用贪心也挺简单,不断把区间右侧得数加入集合是的每个集合有俩个数出现在集合中,当然先排序按照右侧
端点排序;
#include<stdio.h>#include<algorithm>using namespace std;struct node{int x;int y;}b[100001];bool cmp(node a,node b){return a.y<b.y;}int main(){int n,j,i,m[10001];scanf("%d",&n);for(i=0;i<n;i++)scanf("%d%d",&b[i].x,&b[i].y);sort(b,b+n,cmp);m[0]=b[0].y-1;m[1]=b[0].y;j=0;for(i=1;i<n;i++){ if(b[i].x<=m[j]&&m[j+1]<=b[i].y) {continue; } if(m[j+1]>=b[i].x&&m[j]<b[i].x){ m[j+2]=b[i].y; j=j+1;} if(m[j+1]<b[i].x){ m[j+2]=b[i].y-1; m[j+3]=b[i].y; j=j+2;}}printf("%d\n",j+2);}
0 0
- 贪心问题
- 贪心问题
- 贪心问题
- 贪心问题
- 贪心问题--过河问题
- 【贪心DP】过河问题
- 贪心--会场安排问题
- 背包问题--贪心
- 贪心算法 - 背包问题
- 【贪心】删数问题
- 【贪心】活动安排问题
- 【贪心】删数问题
- 箱子问题(贪心)
- 贪心背包问题
- 贪心法-背包问题
- 区间选点问题【贪心】
- 区间覆盖问题【贪心】
- 正统贪心问题!!!!
- 黑马程序员-JAVA高级(IO输入与输出)PART4
- 01背包&&小P寻宝记
- 关于文件输入输出的集中方法总结
- 【PAT1044】Shopping in Mars 二分法
- JAVA MAIL发送中文附件失败的原因
- 贪心问题
- 《学习OpenCV》练习7-2
- java转移字符
- java学习笔记<十>接口
- Cracking the coding interview--Q5.1
- Linux远程访问windows时,出现"连接被对端重置"错误
- 寒假学习 第25天 (linux 高级编程)
- CSS按钮在线生成工具
- HTTP 返回状态值详解分类