日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

4kyu Twice linear

發布時間:2025/3/21 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 4kyu Twice linear 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

4kyu Twice linear

題目背景:

Task
Consider a sequence u where u is defined as follows:

1. The number u(0) = 1 is the first one in u. 2. For each x in u, then y = 2 * x + 1 and z = 3 * x + 1 must be in u too. 3. There are no other numbers in u.

Given parameter n the function dbl_linear (or dblLinear…) returns the element u(n) of the ordered (with <) sequence u (so, there are no duplicates).

Example

u = [1, 3, 4, 7, 9, 10, 13, 15, 19, 21, 22, 27, ...] dbl_linear(10) should return 22

題目分析:
本道題目的思路很清晰,就是不斷迭代數組中的元素,以及該元素 u 的 2 * u + 1 和 3 * u + 1,我們需要做的是如何把這些元素按照從小到大的順序排序。簡單的思路就是借用隊列去維護 2 * u + 1,及 3 * u + 1 的非記錄元素,每個隊列里面的數據是從小到大的排序,每一次都是比對兩個隊列的隊首元素,然后次數 +1。

AC代碼:

#include <queue>class DoubleLinear { public:static int dblLinear(int n); };int DoubleLinear::dblLinear(int n) {std::queue<int> q2;std::queue<int> q3;int tmp = 1, cnt = 0;while( cnt != n ) {q2.push(tmp * 2 + 1);q3.push(tmp * 3 + 1);if ( q2.front() < q3.front() ) {tmp = q2.front();q2.pop();}else if ( q2.front() > q3.front() ) {tmp = q3.front();q3.pop();}else {tmp = q2.front();q2.pop();q3.pop();}cnt++; }return tmp; }

總結

以上是生活随笔為你收集整理的4kyu Twice linear的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。