(未AC)7-5 特殊队列 (30分)

普通的队列仅有 EnQueue 和 DeQueue 两种操作,分别表示在队尾增加元素和取出队首元素。现在给队列增加一种新的操作 DeleteMid,表示删除队列的中间元素。对于有 N 个元素的队列,若 N 为偶数,中间元素定义为从队首到队尾的第 N/2 个元素;若 N 为奇数,中间元素定义为第 (N+1)/2 个元素。现给出队列的一系列操作,输出相应结果。

输入格式:

第一行输入一个不超过 10?6?? 的正整数 M 和 N,分别表示指令条数和队列容量。

之后 M 行,每行给出一条指令,为下列3种指令之一:

EnQueue elem

DeQueue

DeleteMid

输出格式:

对于每个 EnQueue 指令,若未超出队列容量,不输出任何信息,否则在一行中输出Full Queue

对于每个 DeQueue 和 DeleteMid 指令,若队列不为空,则取出相应元素并输出;否则只在一行中输出Empty Queue

最后在一行中按从队首到队尾的顺序依次输出队列中的元素,以空格分隔。行尾不得有多余空格。

输入样例:

10 4
DeQueue
EnQueue 2
EnQueue 3
EnQueue 4
EnQueue 5
DeleteMid
DeleteMid
EnQueue 7
EnQueue 8
EnQueue 9

输出样例:

Empty Queue
3
4
Full Queue
2 5 7 8
 1 #include<iostream>
 2 #include<string>
 3 #include<vector>
 4 using namespace std;
 5 int main()
 6 {
 7     long M, N, i = 0;
 8     string s;
 9     int tmp;
10     cin >> M >> N;
11     vector<int>v;
12     for (long j = 0; j < M; j++)
13     {
14         i = 0;
15         cin >> s;
16         if (s[i] == ‘D‘&&s[i + 1] == ‘e‘&&s[i + 2] == ‘Q‘)
17         {
18             if (v.empty())
19             {
20                 cout << "Empty Queue" ;
21             }
22             else
23             {
24                 cout << v[i] << endl;
25                 v.erase(v.begin());
26             }
27             continue;
28         }
29         else
30         {
31             if (s[i] == ‘E‘)
32             {
33                 if (v.size() == N)
34                 {
35                     cout << "Full Queue" ;
36                 }
37                 else
38                 {
39                 cin >> tmp;
40                 v.push_back(tmp);
41                 continue;
42                 }
43             }
44             if (s[i] == ‘D‘&&s[i + 1] == ‘e‘&&s[i + 2] == ‘l‘)
45             {
46                 if (v.empty())
47                 {
48                     cout << "Empty Queue" ;
49                 }
50                 else
51                 {
52                     if (v.size() % 2 == 0)
53                     {
54                         cout << v[N / 2 - 1] << endl;
55                         v.erase(v.begin() + (N / 2 - 1));
56                     }
57                     else
58                     {
59                         cout << v[(N + 1) / 2 - 1];
60                         v.erase(v.begin() + ((N + 1) / 2 - 1));
61                     }
62                 }
63             }
64         }
65     }
66     for (int i = 0; i < v.size(); i++)
67     {
68         if (i != v.size() - 1)
69         {
70             cout << v[i] << ‘ ‘;
71         }
72         else
73             cout << v[i];
74     }
75 }

原文地址:https://www.cnblogs.com/luoyoooo/p/12215788.html

时间: 2024-10-17 18:02:48

(未AC)7-5 特殊队列 (30分)的相关文章

(未AC)7-10 冰壶比赛 (30分)

在3月29日举行的女子冰壶世锦赛决赛中,:钟志颖.陈佳衡.叶翰熙和傅琰组成的中国女子冰壶队以8比6击败了冬奥会和世锦赛双冠王瑞典队,夺得了中国冰壶历史上第一枚世锦赛金牌,创造了历史.美丽.实力兼具的中国冰壶姑娘们也赢得了超高的赞誉. 在冰壶比赛中,给出一个目标点P,以及一个规定的正整数r.每一局由甲乙两队轮流投冰壶各8次后,该局比赛结束.此时,哪一方的冰壶最终离目标点P更近,该方得分,另一方不得分.得分方每颗离目标点P距离小于或等于r.位置较另一队所有冰壶都更接近目标点P的冰壶都可以得1分. 比

PTA 1004 Counting Leaves (30)(30 分)(建树dfs或者bfs,未AC 段错误)

1004 Counting Leaves (30)(30 分) A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child. Input Each input file contains one test case. Each case starts with a line containing 0 < N < 10

PTA 07-图5 Saving James Bond - Hard Version (30分)

07-图5 Saving James Bond - Hard Version   (30分) This time let us consider the situation in the movie "Live and Let Die" in which James Bond, the world's most famous spy, was captured by a group of drug dealers. He was sent to a small piece of lan

1026 Table Tennis (30 分)

1026 Table Tennis (30 分) A table tennis club has N tables available to the public. The tables are numbered from 1 to N. For any pair of players, if there are some tables open when they arrive, they will be assigned to the available table with the sma

PAT 甲级 1014 Waiting in Line (30 分)(queue的使用,模拟题,有个大坑)

1014 Waiting in Line (30 分) Suppose a bank has N windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. The rules for the customers to wait in line are: The space inside the yellow line

PAT 甲级 1034 Head of a Gang (30 分)(bfs,map,强连通)

1034 Head of a Gang (30 分) One way that the police finds the head of a gang is to check people's phone calls. If there is a phone call between A and B, we say that A and B is related. The weight of a relation is defined to be the total time length of

PAT 甲级 1049 Counting Ones (30 分)(找规律,较难,想到了一点但没有深入考虑嫌麻烦)***

1049 Counting Ones (30 分) The task is simple: given any positive integer N, you are supposed to count the total number of 1's in the decimal form of the integers from 1 to N. For example, given N being 12, there are five 1's in 1, 10, 11, and 12. Inp

PAT 甲级1057 Stack (30 分)(不会,树状数组+二分)*****

1057 Stack (30 分) Stack is one of the most fundamental data structures, which is based on the principle of Last In First Out (LIFO). The basic operations include Push (inserting an element onto the top position) and Pop (deleting the top element). No

PAT 甲级 1068 Find More Coins (30 分) (dp,01背包问题记录最佳选择方案)***

1068 Find More Coins (30 分)   Eva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a universal shopping mall which could accept all kinds of coins as payments. However, there was a special