poj 1716 Integer Intervals
来源:互联网 发布:mac修改淘宝登录密码 编辑:程序博客网 时间:2024/06/06 02:55
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.
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 the minimal number of elements in a set containing at least two different integers from each interval.
43 62 40 24 7
4
题目大概意思是给出数轴上的n个区间,每个区间都是连续的int区间。
现在要在数轴上任意取一堆元素,构成一个元素集合V
要求每个区间和元素集合V的交集至少有两个不同的元素
求集合V最小的元素个数,可以用贪心做
因为是找最小的个数且至少有两个
,所以假如两个集合有交集那么肯定包含最后两个数,
开始分情况(1).就是两个集合根本没有交集的话,那么元素的数目直接加上2,并且进行数值的更新。
(2).就是一个集合包含另一个集合,此时直接continue.并且不进行数值更新.因为一开始的那个数组已经确定了两个值,所以后面这个数组肯定也包含那两个数值,你找出的那两个数值就代表集合v中的数,假如你此时把数值进行更新,那么集合v中肯定和正确值不相符,所以在集合包含是什么都不用做,也可以认为当两个集合包含时,以较小的值作为a2.
(3).当两个集合有交集时则进行数值更新,但此时的数值更新和(1)不同,所以看程序吧.
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;struct note{ int x,y;} l[200000];int cmp(note a,note b){ return a.y<b.y;}int main(){ int n; scanf("%d",&n); for(int i=0; i<n; i++) scanf("%d%d",&l[i].x,&l[i].y); sort(l,l+n,cmp); int sum=0; int a1=l[0].y-1; int a2=l[0].y; for(int i=1; i<n; i++) { if(l[i].x>a2) { a1=l[i].y-1; a2=l[i].y; sum=sum+2; } else if(l[i].x<=a1) { } else if(l[i].x<=a2) { a1=a2; a2=l[i].y; sum=sum+1; } } printf("%d\n",sum+2);}
阅读全文
0 0
- 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
- poj 1716 Integer Intervals
- poj 1716 贪心 Integer Intervals
- poj 1716 Integer Intervals (贪心)
- POJ 1716 Integer Intervals 笔记
- POJ 1201 Intervals || POJ 1716 Integer Intervals 差分约束
- POJ--1716[Integer Intervals] 差分约束
- poj 1716 ——Integer Intervals
- (Relax 贪心1.7)POJ 1716 Integer Intervals
- IO流学习记录
- 动态规划 计算二项式系数
- RCNN算法的tensorflow实现
- Flatten Binary Tree to Linked List
- 关于resource加载配置文件问题
- poj 1716 Integer Intervals
- Windows和Mac OS下清除登录共享网络、共享文件的密码
- 点滴记录:大量数据时,系统性能的优化
- win32汇编环境搭建以及第一个程序
- java8系列之重新认识hasMap
- poj 1465 求一个数的最小倍数满足一个条件
- 背包算法
- mac 下运行 android 配置
- Zookeeper学习笔记 --- Zookeeper调研报告3