寒假第二周训练——STL题目汇总

这篇博客汇总了寒假期间关于STL的编程训练题目,涉及队列操作和项目管理问题。首先,文章讲解了一个关于队列管理的编程题目,描述了如何根据不同的管理策略移除进程。接着,提到了一个单词排序的挑战,要求从输入文本中提取并排序所有不同的单词。最后,文章探讨了一个关于用户改名请求的问题,需要跟踪用户在多次改名后的最终用户名。所有解决方案都需要考虑字符串操作和数据结构的使用。
摘要由CSDN通过智能技术生成
MANAGER
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions:3112   Accepted: 1106

Description

One of the programming paradigm in parallel processing is the producer/consumer paradigm that can be implemented using a system with a "manager" process and several "client" processes. The clients can be producers, consumers, etc. The manager keeps a trace of client processes. Each process is identified by its cost that is a strictly positive integer in the range 1 .. 10000. The number of processes with the same cost cannot exceed 10000. The queue is managed according to three types of requests, as follows: 
  • a x - add to the queue the process with the cost x; 
  • r - remove a process, if possible, from the queue according to the current manager policy; 
  • p i - enforce the policy i of the manager, where i is 1 or 2. The default manager policy is 1 
  • e - ends the list of requests. 

There are two manager policies: 
  • 1 - remove the minimum cost process 
  • 2 - remove the maximum cost process 

The manager will print the cost of a removed process only if the ordinal number of the removed process is in the removal list. 

Your job is to write a program that simulates the manager process. 

Input

The input is from the standard input. Each data set in the input has the following format: 
  • the maximum cost of the processes 
  • the length of the removal list 
  • the removal list - the list of ordinal numbers of the removed processes that will be displayed; for example 1 4 means that the cost of the first and fourth removed processes will be displayed
  • the list of requests each on a separate line. 

Each data set ends with an e request. The data sets are separated by empty lines.

Output

The program prints on standard output the cost of each process that is removed, provided that the ordinal number of the remove request is in the list and the queue is not empty at that moment. If the queue is empty the program prints -1. The results are printed on separate lines. An empty line separates the results of different data sets. 

An example is given in the following:

Sample Input

5
2
1 3
a 2
a 3
r
a 4
p 2
r
a 5
r
e

Sample Output

2
5

Source



题意:完成一个manager系统,包含四种指令,a x加入一个x,p x转化policy为x,r根据policy来删除,e结束指令。其中默认的policy为1,即删除最小的那个,policy2则是删除最大的那个。输入一个removal list,问你第i个被删除的元素是什么?并将其打印出来。


#include <iostream>
#include <cstdio>
#include <set>
//multiset的头文件还是set 
#include <vector>
using namespace std;
vector<int> q;
multiset<int> s;
//multiset可重复的set 
int policy,r_list[1000];
//创建removal list和policy
 
int main()
{
	int maxc,r_len,cost;
	//这个maxc好像并没有什么用,r_len为输入的removal list的长度 
	char ch;
	while(cin >> maxc >> r_len)
	{
		getchar();
		for (int i=0;i<r_len;i++)
			cin >> r_list[i];
		q.clear(); s.clear(); policy=1;
		//初始化,清空q,s,policy置1 
		while(cin >> ch && ch!='e')
		{
			if (ch=='a') {cin >> cost; getchar(); s.insert(cost);}
			else if (ch=='p') {cin >> policy; getchar();}
			else if (ch=='r')
			{
			    if(s.empty())  
					cout << "-1" << endl; 
    			else if(policy==1)
				{  
					multiset<int>::iterator it=s.begin();
					//迭代器,找到set的首位,即最小值 
        			q.push_back(*it); 
        			//将其值放入要输出的队列中 
        			s.erase(*it);
        			//删除最小值 
        		}
				else if(policy==2)
				{  
        			multiset<int>::reverse_iterator it=s.rbegin();
        			//逆向迭代器 
       				q.push_back(*it);
        			s.erase(*it);
    			}  
			}
		}
        for(int i=0;i<r_len;i++)  
        {  
			cout << q[r_list[i]-1] << endl;  
        }
        cout << endl;  
	}
	return 0;
}

                                        Andy's First Dictionary

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值