Integer Intervals(贪心)
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 12123 | Accepted: 5129 |
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
4
3 6
2 4
0 2
4 7
Sample Output
4 题意:在数轴上给n个区间,区间上的点均是整数,再在数轴上取x个点构成的点集V使得V与上述每个区间的交集至少包含两个交点,输出满足题意的x的最小值。
思路:可以贪心,先按每个区间的有端点升序排序,
初始化:计数器 sum = 2,集合V的前两个元素selem,telem为第一个区间的最后两个整数;(selem和telem在整个循环中作为集合V的最后两个元素,所以必要时更新其值)
for:
若第i+1个区间包含selem和telem,不需要做任何改变;
若第i+1个区间只包含telem,更新selem和telem,sum加1;
若第i+1个区间不包含selem和telem,更新selem和telem,sum加2;
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; struct node
{
int s,t;
}L[];
int cmp(const struct node &a, const struct node &b)
{
return a.t < b.t;
} int main()
{
int n;
scanf("%d",&n);
for(int i = ; i < n; i++)
scanf("%d %d",&L[i].s,&L[i].t);
sort(L,L+n,cmp);
int sum,selem,telem;
sum = ;
selem = L[].t-;
telem = L[].t;
for(int i = ; i < n;)
{
if(selem >= L[i].s && selem <= L[i].t && telem >= L[i].s && telem <= L[i].t)
i++;
else if(selem < L[i].s && telem >= L[i].s && telem <= L[i].t)
{
selem = telem;
telem = L[i].t;
sum++;
i++;
}
else if(selem < L[i].s && telem < L[i].s)
{
selem = L[i].t-;
telem = L[i].t;
sum += ;
i++;
}
}
printf("%d\n",sum);
return ;
}
Integer Intervals(贪心)的更多相关文章
- POJ 1716 Integer Intervals#贪心
(- ̄▽ ̄)-* //求一个集合,这个集合与任意一个区间的交集,需至少有两个数字 //贪心过程:按n个区间的最右值从小到大对区间进行排列, //集合首先取第一个区间的最右两个数字, //到第二个区间, ...
- poj 1716 Integer Intervals (差分约束 或 贪心)
Integer Intervals Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12192 Accepted: 514 ...
- poj1716 Integer Intervals(差分约束)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Integer Intervals Time Limit: 1000MS Me ...
- 【POJ 1716】Integer Intervals(差分约束系统)
id=1716">[POJ 1716]Integer Intervals(差分约束系统) Integer Intervals Time Limit: 1000MS Memory L ...
- poj 1716 Integer Intervals(差分约束)
1716 -- Integer Intervals 跟之前个人赛的一道二分加差分约束差不多,也是求满足条件的最小值. 题意是,给出若干区间,需要找出最少的元素个数,使得每个区间至少包含两个这里的元素. ...
- POJ1716 Integer Intervals
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13984 Accepted: 5943 Description An i ...
- E - Intervals 贪心
Chiaki has n intervals and the i-th of them is [li, ri]. She wants to delete some intervals so that ...
- POJ 1716 Integer Intervals
题意:给出一些区间,求一个集合的长度要求每个区间里都至少有两个集合里的数. 解法:贪心或者差分约束.贪心的思路很简单,只要将区间按右边界排序,如果集合里最后两个元素都不在当前区间内,就把这个区间内的最 ...
- 【poj1716】 Integer Intervals
http://poj.org/problem?id=1716 (题目链接) 题意 给出n个区间,要求取出最少数量的不同的自然数,使每个区间中至少包含2个取出的数. Solution 差分约束. 运用前 ...
随机推荐
- storyBoard使用介绍
storyBoard使用介绍 转载地址:http://www.2cto.com/kf/201210/161737.html 一 .简述 Storyboard是你可以用来定义用户界面的一种新的方式,像x ...
- 使用 asp.net mv4开发企业级办公OA
大家好!这是我第一次写asp.net 开发笔记,哪里写的不好,请见谅! 本程序是一个在线办公(OA)系统 B/S项目: 项目开发环境:Microsoft Visual Studio 2012 + Sq ...
- PHP 5.6启动失败failed to open configuration file '/usr/local/php/etc/php-fpm.conf'
PHP编译安装完毕,启动失败,提示 [-Jun- ::] ERROR: failed to open configuration ) [-Jun- ::] ERROR: failed to load ...
- URAL 2032 - Conspiracy Theory and Rebranding【本源勾股数组】
[题意] 给出三角形的三个边长,均是10^7以内的整数,问三角形的三个角的坐标是否能均是整数,输出其中任意一个解. [题解] 一开始想的是枚举一条边的横坐标,然后通过勾股定理以及算角度求出其他点的坐标 ...
- hadoop集群环境搭建之安装配置hadoop集群
在安装hadoop集群之前,需要先进行zookeeper的安装,请参照hadoop集群环境搭建之zookeeper集群的安装部署 1 将hadoop安装包解压到 /itcast/ (如果没有这个目录 ...
- .NET Core的介绍
ASP.NET5应用程序默认使用.net core来构建应用程序,.net core是一个小的,优化过的.net运行时应用程序. 1. 什么是的.NET Core .NET Core 5 是一由模块化 ...
- openURL的使用方法
openURL的使用方法 openURL的使用方法: view plaincopy to clipboardprint? [[UIApplication sharedApplication] open ...
- 【转】IOS 30多个iOS常用动画,带详细注释
原文: http://blog.csdn.net/zhibudefeng/article/details/8691567 CoreAnimationEffect.h 文件 // CoreAnimati ...
- C# 线程间互相通信
C#线程间互相通信主要用到两个类:AutoResetEvent和ManualResetEvent. 一.AutoResetEvent AutoResetEvent 允许线程通过发信号互相通信,线程通过 ...
- confluence的权限管理
上一篇解讲如何破解,安装confluence5.8.10,这次主要是看权限管理的实现.因为公司对知识的管理不仅是简单的分享,还要求不同权限的人看到不同的内容,所以在一开始就需要对权限这一块有所了解,以 ...