当前位置:网站首页>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;
}
边栏推荐
- 9.OpenFeign服务接口调用
- 【ELT.ZIP】OpenHarmony啃论文俱乐部—数据密集型应用内存压缩
- TIA博途_基于SCL语言制作模拟量输入输出全局库的具体方法
- laravel框架中 定时任务的实现
- Market status and development prospect forecast of global active quality control air sampler industry in 2022
- How to use the low code platform of the Internet of things for picture management?
- Market status and development prospect forecast of global epoxy resin active toughener industry in 2022
- Application of scaleflux CSD 2000 in Ctrip
- 中国工业软件市场研究报告出炉,力控SCADA、MES丰富国产工业软件生态
- Hikvision Tools Manager海康威视工具大全(含SADP、录像容量计算等工具)百万安防从业者的实用工具
猜你喜欢

TDengine 连接器上线 Google Data Studio 应用商店

MySQL中的行转列和列转行

Hikvision tools manager Hikvision tools collection (including sadp, video capacity calculation and other tools) a practical tool for millions of security practitioners

How to use the low code platform of the Internet of things for picture management?

The first in China! EMQ joined Amazon cloud technology's "startup acceleration - global partner network program"

【协会通知】关于举办人工智能与物联网领域暑假专题师资培训的通知

PostgreSQL database Wal - resource manager rmgr

Teach you how to install Oracle 19C on Windows 10 (detailed picture and text with step on pit Guide)

阿里巴巴的使命、愿景、核心价值观

GAC Mitsubishi's new outlander made its first domestic debut in the year, and its product strength was fully renewed
随机推荐
新中大冲刺科创板:年营收2.84亿 拟募资5.57亿
信息学奥赛一本通 1333:【例2-2】Blah数集 | OpenJudge NOI 3.4 2729:Blah数集
VS code 运行yarn run dev 报yarn : 无法加载文件XXX的问题
Market status and development prospect forecast of global tetramethylammonium hydroxide developer industry in 2022
How to turn off the server terminal name of vscode
Openssf security plan: SBOM will drive software supply chain security
Vscode suggests that you enable gopls. What exactly is it?
使用 WebDAV 替代445端口文件共享
Push NFT out of the regulatory dilemma, and BSN launched NFT supporting infrastructure network
Application of tdengine in monitoring of CNC machine tools
【云驻共创】高校数字化差旅建设“解决之道”
Tdengine connector goes online Google Data Studio store
TP5 generates the most detailed two-dimensional code tp6 (also available)
GAC Mitsubishi's new outlander made its first domestic debut in the year, and its product strength was fully renewed
Usage of rxjs mergemap
推荐几个开源的物联网平台
Google Earth Engine(GEE)——ImageCollection (Error)遍历影像集合产生的错误
9.OpenFeign服务接口调用
在arcgis中以txt格式导出点的坐标
Wechat applet payment countdown