trickyPriorityQueue

利用stl中的容器vector实现队列中元素较大的优先出队的问题
例如: 输入 6 0
1 1 9 1 1 1
output: 5

That is, there are six members in the queue, and you position is in 0, you have to calculate the order you can dequeue.

// 排队问题
#include<iostream>
#include<vector>
using namespace std;

//  善于使用struct存储数据,然后再使用vector和queue等容器,而不是盲目地使用map
//  vector用的是最频繁的也非常好用

struct node{
  int index;
  int pri;
  node(int i = 0, int p = 0):index(i), pri(p) {}
};

int main() {
  vector<node> que;
  int alljobs, myjob;
  cin >> alljobs >> myjob;
  int temp;
  for (int i = 0; i < alljobs; ++i) {
    cin >> temp;
    que.push_back(node(i, temp));
  }
  int result = 0;
  while (!que.empty()) {
    node top = que.front();
    bool flag = false;
    //  例: 1 3 7 5 2要找到3在第几个
    //  变为: 3 7 5 2 1
    // 再变为: 7 5 2 1 3
    // 此时在que的后面找不到比7大的数,所以7出队列(此处其实是从vector中erase掉)
    // sum++;
    // 依次循环下去
    for (int i = 1; i < que.size(); ++i) {
      if (que[i].pri > que[0].pri) {
        que.push_back(top);
        flag = true;
        break;
      }
    }
    que.erase(que.begin());
    if (flag == false) result++;
    if (flag == false && top.index == myjob) break;
  }
  cout << result << endl;
  return 0;
}

此处用的是vector实现而不是queue

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值