当前位置:网站首页>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;
}
边栏推荐
- STM32 ADC和DMA
- Factorial implementation of large integer classes
- 1321:【例6.3】删数问题(Noip1994)
- Talking about the return format in the log, encapsulation format handling, exception handling
- Leetcode-303: region and retrieval - array immutable
- Application of OpenGL gllightfv function and related knowledge of light source
- 对存储过程进行加密和解密(SQL 2008/SQL 2012)
- 【STM32】STM32烧录程序后SWD无法识别器件的问题解决方法
- 0x0fa23729 (vcruntime140d.dll) (in classes and objects - encapsulation.Exe) exception thrown (resolved)
- Several schemes of building hardware communication technology of Internet of things
猜你喜欢

Encrypt and decrypt stored procedures (SQL 2008/sql 2012)

求方程ax^2+bx+c=0的根(C语言)

Use the fetch statement to obtain the repetition of the last row of cursor data
![[daiy5] jz77 print binary tree in zigzag order](/img/ba/b2dfbf121798757c7b9fba4811221b.png)
[daiy5] jz77 print binary tree in zigzag order
![[STM32] solution to the problem that SWD cannot recognize devices after STM32 burning program](/img/03/41bb3870b9a6c2ee66099abac08eb3.png)
[STM32] solution to the problem that SWD cannot recognize devices after STM32 burning program

【二开】【JeecgBoot】修改分页参数

对存储过程进行加密和解密(SQL 2008/SQL 2012)

HAL库配置通用定时器TIM触发ADC采样,然后DMA搬运到内存空间。
![[second on] [jeecgboot] modify paging parameters](/img/59/55313e3e0cf6a1f7f6b03665e77789.png)
[second on] [jeecgboot] modify paging parameters

Several schemes of building hardware communication technology of Internet of things
随机推荐
Hdu-2196 tree DP learning notes
Sword finger offer 38 Arrangement of strings [no description written]
【HigherHRNet】 HigherHRNet 详解之 HigherHRNet的热图回归代码
【acwing】786. 第k个数
Use the fetch statement to obtain the repetition of the last row of cursor data
JS实现链式调用
[牛客网刷题 Day6] JZ27 二叉树的镜像
1323:【例6.5】活动选择
Encrypt and decrypt stored procedures (SQL 2008/sql 2012)
Mendeley--免费的文献管理工具,给论文自动插入参考文献
[second on] [jeecgboot] modify paging parameters
[homework] 2022.7.6 write your own cal function
Several schemes of building hardware communication technology of Internet of things
P2788 数学1(math1)- 加减算式
Remote meter reading, switching on and off operation command
关于easyflash v3.3使用过程的记录
Slurm资源管理与作业调度系统安装配置
Easyexcel read write simple to use
BUUCTF---Reverse---reverse1
leetcode-303:区域和检索 - 数组不可变