当前位置:网站首页>1324:【例6.6】整数区间
1324:【例6.6】整数区间
2022-07-07 08:14:00 【暴揍键盘的程序猿】
1324:【例6.6】整数区间
时间限制: 1000 ms 内存限制: 65536 KB
提交数: 10710 通过数: 6431
【题目描述】
请编程完成以下任务:
1.读取闭区间的个数及它们的描述;
2.找到一个含元素个数最少的集合,使得对于每一个区间,都至少有一个整数属于该集合,输出该集合的元素个数。
【输入】
首行包括区间的数目nn,1≤n≤100001≤n≤10000,接下来的nn行,每行包括两个整数a,ba,b,被一空格隔开,0≤a≤b≤100000≤a≤b≤10000,它们是某一个区间的开始值和结束值。
【输出】
第一行集合元素的个数,对于每一个区间都至少有一个整数属于该集合,且集合所包含元素数目最少。
【输入样例】
4
3 6
2 4
0 2
4 7
【输出样例】
2
【算法分析】
算法模型:给n个闭区间[ai,bi],在数轴上选尽量少的点,使每个区间内至少有一个点。
算法:首先按b1<=b2<=...<=bn排序。每次标记当前区间的右端点n,并右移当前区间指针,直到当前区间不包含n,再重复上述操作。
【AC代码】
#include<algorithm>
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<string>
#include<vector>
using namespace std;
const int N=1e4+10;
inline int fread()
{
char ch=getchar();
int n=0,m=1;
while(ch<'0' or ch>'9')
{
if(ch=='-')m=-1;
ch=getchar();
}
while(ch>='0' and ch<='9')n=(n<<3)+(n<<1)+ch-48,ch=getchar();
return n*m;
}
void fwrite(int n)
{
if(n>9)fwrite(n/10);
putchar(n%10+'0');
}
int n,a[N],b[N],ans,m;
void qsort(int n,int m)//多关键字快排
{
int i=n,j=m,mid=b[n+m>>1],mid1=a[n+m>>1];
while(i<=j)
{
while(b[i]<mid or(b[i]==mid and a[i]<mid1))i++;
while(b[j]>mid or(b[j]==mid and a[j]>mid1))j--;
if(i<=j)swap(b[i],b[j]),swap(a[i],a[j]),i++,j--;
}
if(n<j)qsort(n,j);
if(i<m)qsort(i,m);
}
signed main()
{
n=fread();
for(int i=1;i<=n;i++)a[i]=fread(),b[i]=fread();
qsort(1,n);
for(int i=1,j=-1;i<=n;i++)//在初始化循环变量的同时,初始化j
{//令j=-1可以使第一个区间与其他区间的操作相同
if(j>=a[i])continue;//如果当前区间包含标记点,就跳过
ans++,j=b[i];//更新标记点
}
fwrite(ans);
return 0;
}
时间限制: 1000 ms 内存限制: 65536 KB
提交数: 10710 通过数: 6431
【题目描述】
请编程完成以下任务:
1.读取闭区间的个数及它们的描述;
2.找到一个含元素个数最少的集合,使得对于每一个区间,都至少有一个整数属于该集合,输出该集合的元素个数。
【输入】
首行包括区间的数目nn,1≤n≤100001≤n≤10000,接下来的nn行,每行包括两个整数a,ba,b,被一空格隔开,0≤a≤b≤100000≤a≤b≤10000,它们是某一个区间的开始值和结束值。
【输出】
第一行集合元素的个数,对于每一个区间都至少有一个整数属于该集合,且集合所包含元素数目最少。
【输入样例】
4
3 6
2 4
0 2
4 7
【输出样例】
2
【算法分析】
算法模型:给n个闭区间[ai,bi],在数轴上选尽量少的点,使每个区间内至少有一个点。
算法:首先按b1<=b2<=...<=bn排序。每次标记当前区间的右端点n,并右移当前区间指针,直到当前区间不包含n,再重复上述操作。
【AC代码】
#include<algorithm>
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<string>
#include<vector>
using namespace std;
const int N=1e4+10;
inline int fread()
{
char ch=getchar();
int n=0,m=1;
while(ch<'0' or ch>'9')
{
if(ch=='-')m=-1;
ch=getchar();
}
while(ch>='0' and ch<='9')n=(n<<3)+(n<<1)+ch-48,ch=getchar();
return n*m;
}
void fwrite(int n)
{
if(n>9)fwrite(n/10);
putchar(n%10+'0');
}
int n,a[N],b[N],ans,m;
void qsort(int n,int m)//多关键字快排
{
int i=n,j=m,mid=b[n+m>>1],mid1=a[n+m>>1];
while(i<=j)
{
while(b[i]<mid or(b[i]==mid and a[i]<mid1))i++;
while(b[j]>mid or(b[j]==mid and a[j]>mid1))j--;
if(i<=j)swap(b[i],b[j]),swap(a[i],a[j]),i++,j--;
}
if(n<j)qsort(n,j);
if(i<m)qsort(i,m);
}
signed main()
{
n=fread();
for(int i=1;i<=n;i++)a[i]=fread(),b[i]=fread();
qsort(1,n);
for(int i=1,j=-1;i<=n;i++)//在初始化循环变量的同时,初始化j
{//令j=-1可以使第一个区间与其他区间的操作相同
if(j>=a[i])continue;//如果当前区间包含标记点,就跳过
ans++,j=b[i];//更新标记点
}
fwrite(ans);
return 0;
}
边栏推荐
- JMeter loop controller and CSV data file settings are used together
- Or in SQL, what scenarios will lead to full table scanning
- STM32 Basics - memory mapping
- Wallys/IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL CONCURRENT
- The Hal library is configured with a general timer Tim to trigger ADC sampling, and then DMA is moved to the memory space.
- 反卷积通俗详细解析与nn.ConvTranspose2d重要参数解释
- C logging method
- The story of Plato and his three disciples: how to find happiness? How to find the ideal partner?
- 2022.7.4DAY596
- 大整数类实现阶乘
猜你喜欢
JMeter about setting thread group and time
Weekly recommended short videos: what are the functions of L2 that we often use in daily life?
【acwing】789. 数的范围(二分基础)
ES6中的原型对象
【acwing】789. Range of numbers (binary basis)
柏拉图和他的三个弟子的故事:如何寻找幸福?如何寻找理想伴侣?
Use of JSON extractor originals in JMeter
STM32中AHB总线_APB2总线_APB1总线这些是什么
Leetcode exercise - 113 Path sum II
01 use function to approximate cosine function (15 points)
随机推荐
ORM -- grouping query, aggregation query, query set queryset object properties
Mongodb creates an implicit database as an exercise
SolidWorks工程图中添加中心线和中心符号线的办法
JMeter installation
Embedded background - chip
STM32 ADC and DMA
柏拉图和他的三个弟子的故事:如何寻找幸福?如何寻找理想伴侣?
PDF文档签名指南
【acwing】789. Range of numbers (binary basis)
STM32基础知识—内存映射
STM32 Basics - memory mapping
Web3.0 series distributed storage IPFs
【二开】【JeecgBoot】修改分页参数
嵌入式背景知识-芯片
电表远程抄表拉合闸操作命令指令
STM32 product introduction
ORM model -- creation and query of data records
Using keras in tensorflow to build convolutional neural network
XML configuration file parsing and modeling
[learning notes - Li Hongyi] Gan (generation of confrontation network) full series (I)