鱼C论坛

 找回密码
 立即注册
查看: 321|回复: 1

队列操作时间复杂度问题

[复制链接]
发表于 2024-1-18 16:57:21 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
问题如下:

给定一个初始状态为空的序列,它将按顺序执行Q个操作
操作分为以下三种:
1 将a插入队列的末尾
2:查询队首元素,并将其删除。数据保证此时A非空
3:将队列升序排排序
输入描述:
第一行一个正整数Q(1 < Q < 2 x 10^5,表示操作数接下来Q行,每行表示一个操作
输出描述:
对于每个操作,输出一行一个正整数表示本次询问的答案。

求问大佬如何改进时间复杂度?


  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. typedef long long ll;

  5. using namespace std;

  6. int main() {
  7.     ios::sync_with_stdio(0);
  8.     cin.tie(0);
  9.     vector<ll> queue, dp;
  10.     ll num, cnum = 0;
  11.     ll temp,ans;
  12.     bool bol=0,judge;
  13.     cin >> num;

  14.     for (ll i = 0; i < num; i++) {
  15.         cin >> temp;

  16.         if (temp == 1) {
  17.             cin >> temp;
  18.             queue.push_back(temp);
  19.         }
  20.         else if (temp == 2) {
  21.             dp.push_back(queue.front());
  22.             queue.erase(queue.begin());
  23.             cnum += 1;
  24.         }
  25.         else if (temp == 3&&ans!=3) {
  26.             sort(queue.begin(), queue.end());
  27.         }
  28.         ans = temp;
  29.     }

  30.     for (ll i = 0; i < cnum; i++) {
  31.         cout << dp[i] << '\n';
  32.     }

  33.     return 0;
  34. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-1-18 16:58:55 | 显示全部楼层
这道题的关键是如何改进时间复杂度。目前的代码中,每次执行操作2时,都需要遍历整个队列来查询并删除队首元素,导致时间复杂度为O(n),其中n表示当前队列的长度。而操作3中,每次都需要对整个队列进行排序,时间复杂度也是O(nlogn)。

我们可以使用两个队列来优化这个问题。一个队列用来保存原始的输入数据,另一个队列用来保存当前队列的有序状态。具体做法如下:

1. 当执行插入操作时,将元素插入到原始队列的末尾,并同时插入到有序队列的合适位置。
2. 当执行查询并删除队首元素操作时,直接从有序队列的队首获取即可,并从原始队列中删除。
3. 当执行排序操作时,不需要对整个队列进行排序,只需重新确定有序队列的元素顺序即可。

以下是改进后的代码:

  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>

  4. typedef long long ll;

  5. using namespace std;

  6. int main() {
  7.     ios::sync_with_stdio(0);
  8.     cin.tie(0);
  9.    
  10.     vector<int> originalQueue, sortedQueue;
  11.     ll num, cnum = 0;
  12.     ll temp, ans;
  13.     bool bol = 0, judge;
  14.     cin >> num;

  15.     for (ll i = 0; i < num; i++) {
  16.         cin >> temp;

  17.         if (temp == 1) {
  18.             cin >> temp;
  19.             originalQueue.push_back(temp);
  20.             
  21.             // 将新插入的元素插入到有序队列的合适位置
  22.             auto it = lower_bound(sortedQueue.begin(), sortedQueue.end(), temp);
  23.             sortedQueue.insert(it, temp);
  24.             
  25.         } else if (temp == 2) {
  26.             cout << sortedQueue.front() << endl;
  27.             
  28.             // 从原始队列和有序队列中删除队首元素
  29.             originalQueue.erase(originalQueue.begin());
  30.             sortedQueue.erase(sortedQueue.begin());
  31.             
  32.             cnum += 1;
  33.             
  34.         } else if (temp == 3 && ans != 3) {
  35.             // 无需对整个队列进行排序,只需重新确定有序队列的元素顺序
  36.             sort(sortedQueue.begin(), sortedQueue.end());
  37.         }
  38.         ans = temp;
  39.     }

  40.     for (ll i = 0; i < cnum; i++) {
  41.         cout << sortedQueue[i] << endl;
  42.     }
  43.    
  44.     return 0;
  45. }
复制代码

这样做的话,每次执行操作2时只需要从有序队列的队首取出元素即可,时间复杂度为O(1),而操作3的时间复杂度也为O(nlogn),但是由于操作3不是连续执行的,所以整体的时间复杂度会得到极大的优化。

希望可以帮助到你!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-2 00:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表