Sicily-1443

一.      题意:

模拟队列的操作,按优先级pop。如果有元素pop,则其他在队列里面的元素的时间都要加1.如果队头的元素因为优先级不够高而要push回队列的时候,所有元素的时间都不用改变。

二.      注意选中元素最终输出时,本身的时间戳要加1.

三. 代码

 1 //

 2 //  main.cpp

 3 //  sicily-1443

 4 //

 5 //  Created by ashley on 14-10-11.

 6 //  Copyright (c) 2014年 ashley. All rights reserved.

 7 //

 8 

 9 #include <iostream>

10 #include <deque>

11 #include <iterator>

12 using namespace std;

13 typedef struct

14 {

15     int priorty;

16     int position;

17     int time;

18 }node;

19 int main(int argc, const char * argv[])

20 {

21     int cases, size, pos, pri;

22     int result = 1;

23     cin >> cases;

24     while (cases--) {

25         deque<node> myQueue;

26         cin >> size >> pos;

27         for (int i = 0; i < size; i++) {

28             cin >> pri;

29             node newNode = {pri, i, 0};

30             myQueue.push_back(newNode);

31         }

32         while (!myQueue.empty()) {

33             node head = myQueue.front();

34             //cout << head.position << endl;

35             myQueue.pop_front();

36             bool canPrint = true;

37             for (deque<node>::iterator it = myQueue.begin(); it != myQueue.end() ; it++) {

38                 if (it->priorty > head.priorty) {

39                     myQueue.push_back(head);

40                     canPrint = false;

41                     break;

42                 }

43             }

44             if (canPrint) {

45                 if (head.position == pos) {

46                     result = head.time + 1;

47                 }

48                 for (deque<node>::iterator it = myQueue.begin(); it != myQueue.end(); it++) {

49                     it->time++;

50                 }

51             }

52         }

53         cout << result << endl;

54     }

55     return 0;

56 }

 

你可能感兴趣的:(CI)