当前位置:网站首页>1324: [example 6.6] integer interval
1324: [example 6.6] integer interval
2022-07-07 10:32:00 【A program ape who beats the keyboard violently】
1324:【 example 6.6】 Integer interval
The time limit : 1000 ms Memory limit : 65536 KB
Submission number : 10710 Passing number : 6431
【 Title Description 】
Please program to complete the following tasks :
1. Read the number of closed intervals and their descriptions ;
2. Find a set with the least number of elements , So that for each interval , At least one integer belongs to the set , Output the number of elements of the collection .
【 Input 】
The first line contains the number of intervals nn,1≤n≤100001≤n≤10000, Next nn That's ok , Each line contains two integers a,ba,b, Separated by a space ,0≤a≤b≤100000≤a≤b≤10000, They are the start and end values of an interval .
【 Output 】
The number of set elements in the first line , For each interval, at least one integer belongs to the set , And the set contains the least number of elements .
【 sample input 】
4
3 6
2 4
0 2
4 7【 sample output 】
2【 Algorithm analysis 】
Algorithm model : to n Closed interval [ai,bi], Choose as few points as possible on the number axis , Make at least one point in each interval .
Algorithm : First click b1<=b2<=...<=bn Sort . Mark the right end of the current interval every time n, And move the current interval pointer to the right , Until the current interval does not contain n, Repeat the above operation .
【AC Code 】
#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)// Multi keyword quick sorting
{
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++)// While initializing the loop variable , initialization j
{// Make j=-1 You can make the operation of the first interval the same as that of other intervals
if(j>=a[i])continue;// If the current interval contains marked points , Just skip.
ans++,j=b[i];// Update marked points
}
fwrite(ans);
return 0;
}
The time limit : 1000 ms Memory limit : 65536 KB
Submission number : 10710 Passing number : 6431
【 Title Description 】
Please program to complete the following tasks :
1. Read the number of closed intervals and their descriptions ;
2. Find a set with the least number of elements , So that for each interval , At least one integer belongs to the set , Output the number of elements of the collection .
【 Input 】
The first line contains the number of intervals nn,1≤n≤100001≤n≤10000, Next nn That's ok , Each line contains two integers a,ba,b, Separated by a space ,0≤a≤b≤100000≤a≤b≤10000, They are the start and end values of an interval .
【 Output 】
The number of set elements in the first line , For each interval, at least one integer belongs to the set , And the set contains the least number of elements .
【 sample input 】
4
3 6
2 4
0 2
4 7【 sample output 】
2【 Algorithm analysis 】
Algorithm model : to n Closed interval [ai,bi], Choose as few points as possible on the number axis , Make at least one point in each interval .
Algorithm : First click b1<=b2<=...<=bn Sort . Mark the right end of the current interval every time n, And move the current interval pointer to the right , Until the current interval does not contain n, Repeat the above operation .
【AC Code 】
#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)// Multi keyword quick sorting
{
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++)// While initializing the loop variable , initialization j
{// Make j=-1 You can make the operation of the first interval the same as that of other intervals
if(j>=a[i])continue;// If the current interval contains marked points , Just skip.
ans++,j=b[i];// Update marked points
}
fwrite(ans);
return 0;
}
边栏推荐
- 5个chrome简单实用的日常开发功能详解,赶快解锁让你提升更多效率!
- mysql插入数据创建触发器填充uuid字段值
- 根据设备信息进行页面跳转至移动端页面或者PC端页面
- @Transcation的配置,使用,原理注意事项:
- Leetcode-303: region and retrieval - array immutable
- 【HigherHRNet】 HigherHRNet 详解之 HigherHRNet的热图回归代码
- IIC基本知识
- Jump to the mobile terminal page or PC terminal page according to the device information
- Elegant controller layer code
- EasyExcel读取写入简单使用
猜你喜欢

5个chrome简单实用的日常开发功能详解,赶快解锁让你提升更多效率!

openinstall与虎扑达成合作,挖掘体育文化产业数据价值

Guide de signature du Code Appx
![[sword finger offer] 42 Stack push in and pop-up sequence](/img/f4/eb69981163683c5b36f17992a87b3e.png)
[sword finger offer] 42 Stack push in and pop-up sequence

php \n 换行无法输出

Some properties of leetcode139 Yang Hui triangle

Yarn的基础介绍以及job的提交流程
![[higherhrnet] higherhrnet detailed heat map regression code of higherhrnet](/img/3c/890f2481231482645554f86dd614a9.png)
[higherhrnet] higherhrnet detailed heat map regression code of higherhrnet

JMeter loop controller and CSV data file settings are used together

【acwing】786. 第k个数
随机推荐
[homework] 2022.7.6 write your own cal function
Study summary of postgraduate entrance examination in August
leetcode-304:二维区域和检索 - 矩阵不可变
Multisim -- software related skills
Factorial implementation of large integer classes
基于HPC场景的集群任务调度系统LSF/SGE/Slurm/PBS
Use the fetch statement to obtain the repetition of the last row of cursor data
TypeScript 接口继承
How embedded engineers improve work efficiency
Multisim--软件相关使用技巧
IDA中常见快捷键
Serial communication relay Modbus communication host computer debugging software tool project development case
BigDecimal value comparison
使用 load_decathlon_datalist (MONAI)快速加载JSON数据
Some superficial understanding of word2vec
[sword finger offer] 42 Stack push in and pop-up sequence
When there are pointer variable members in the custom type, the return value and parameters of the assignment operator overload must be reference types
[牛客网刷题 Day5] JZ77 按之字形顺序打印二叉树
反射效率为什么低?
.NET配置系统