当前位置:网站首页>Informatics Olympiad 1333: [example 2-2] blah data set | openjudge noi 3.4 2729:blah data set
Informatics Olympiad 1333: [example 2-2] blah data set | openjudge noi 3.4 2729:blah data set
2022-06-27 18:59:00 【Junyi_ noip】
【 Topic link 】
ybt 1333:【 example 2-2】Blah Number set
OpenJudge NOI 3.4 2729:Blah Number set
【 Topic test site 】
1. queue
【 Their thinking 】
To fill in Blah There are two kinds of numbers in a number set
The first category : from 2x+1 The number generated
The second category : from 3x+1 The number generated
Then open two queues q2 And q3, Stored separately by 2x+1 and 3x+1 The resulting number . The numbers in these two queues must be monotonically increasing .
Constantly compare the head of the two queues , Out of line smaller value , The number thus obtained is monotonic . The number of the heads of the two queues may be the same , There is only one number in the set , In this case, two queues leave the queue at the same time , But in the ascending sequence, only 1 A digital . The... In the ascending sequence n Number , Namely Blah Number in the data set n Number .
specific working means :
Start by entering the base a Generate 2a+1 And 3a+1, Join the team separately q2 And q3 in . At this time, the count is 1.
The two line-up heads are compared , Which number is smaller , Which number will be out of the team . If equal , At the same time .
Set the number just out of the team to a, Count up 1, And will 2*a+1,3*a+1 Join the team separately .
The count increases to n when , here a The value is Blah No. in the data set n Small numbers .
【 Solution code 】
How to write it 1: Using arrays and expressions to implement queues
#include <bits/stdc++.h>
using namespace std;
#define N 1000005
int q2[N], q3[N], a, n, h_2, t_2, h_3, t_3;//q2,q3: Two queues h,t The head and tail pointers of the two queues
int main()
{
while(scanf("%d %d", &a, &n) != EOF)
{
h_2 = t_2 = h_3 = t_3 = 0;// Clear two queues
int count = 1;//count Count
q2[++t_2] = 2*a + 1;// The team
q3[++t_3] = 3*a + 1;
while(count < n)
{
if(q2[h_2+1] < q3[h_3+1])// Team leader comparison
a = q2[++h_2];// queue 2 Out of the team
else if(q2[h_2+1] > q3[h_3+1])
a = q3[++h_3];// queue 3 Out of the team
else// Two equal , All out of the team
{
a = q2[++h_2];
++h_3;
}
q2[++t_2] = 2*a + 1;// The team
q3[++t_3] = 3*a + 1;
count++;
}
printf("%d\n", a);
}
return 0;
}
How to write it 2: Use C++ STL
#include<bits/stdc++.h>
using namespace std;
int main()
{
queue<int> q2, q3;
int a, n;
while(cin >> a >> n)
{
q2 = queue<int>();// Clear queue
q3 = queue<int>();
for(int i = 2; i <= n; ++i)// Please i Small value , The initial conditions a It's No 1 Small value
{
q2.push(2*a+1);
q3.push(3*a+1);
if(q2.front() > q3.front())// If 3x+1 The head of the queue is smaller
{
a = q3.front();
q3.pop();
}
else if(q2.front() < q3.front())// If 2x+1 The head of the queue is smaller
{
a = q2.front();
q2.pop();
}
else
{
a = q2.front();
q2.pop();
q3.pop();
}
}
cout << a << endl;
}
return 0;
}
边栏推荐
- 【网络研讨会】MongoDB 携手 Google Cloud 加速企业数字化创新
- Two methods of MySQL database login and logout
- Market status and development prospect forecast of global tetramethylammonium hydroxide developer industry in 2022
- 时间序列数据的特点
- 【协会通知】关于举办人工智能与物联网领域暑假专题师资培训的通知
- SQL update批量更新
- How to view the index information of MySQL tables?
- 原创 | 2025实现“5个1”奋斗目标!解放动力全系自主非道路国四产品正式发布
- After the number length of Oracle exceeds 19, the entity uses long mapping. Why does this cause problems?
- Characteristics of time series data
猜你喜欢

How to view the index information of MySQL tables?

PostgreSQL database Wal - resource manager rmgr

MySQL数据库登录和退出的两种方式

Wechat applet association search

数据同步工具 DataX 已经正式支持读写 TDengine

Camera calibration with OpenCV

Galaxy Kirin V10 system activation

如何使用物联网低代码平台进行画面管理?
![[Tang Laoshi] C -- encapsulation: member method](/img/47/9a4ffd787624f6208b6aee66c38b48.jpg)
[Tang Laoshi] C -- encapsulation: member method
![[webinar] mongodb and Google cloud accelerate enterprise digital innovation](/img/ea/4680381ce78fd5d956ed009692b424.png)
[webinar] mongodb and Google cloud accelerate enterprise digital innovation
随机推荐
SQL update批量更新
IDEA 官网插件地址
实现时序数据库(Time Series Database)在特定场景下“远超”通用数据库的难点
Application of scaleflux CSD 2000 in Ctrip
Bit. Store: long bear market, stable stacking products may become the main theme
Mise à jour SQL mise à jour par lots
Redis Series 2: data persistence improves availability
数据分析师太火?月入3W?用数据告诉你这个行业的真实情况
为什么要从 OpenTSDB 迁移到 TDengine
Two methods of MySQL database login and logout
Market status and development prospect forecast of global epoxy resin active toughener industry in 2022
Asemi rectifier bridge kbp307 parameters, kbp307 details, kbp307 pictures
SQL update批量更新
实施MES管理系统前,要对哪些问题进行评估
Market status and development prospect forecast of global off-road recovery rope industry in 2022
Seata server database connection user and service database undo_ What permissions do log users need?
如何查看 MySQL 表的索引信息?
Campus book resource sharing platform
【ELT.ZIP】OpenHarmony啃论文俱乐部—见证文件压缩系统EROFS
Keras deep learning practice (12) -- facial feature point detection