Queue and queue-queue for food

Source: Internet
Author: User

Data structure experiment queue 1: queuing for meals

Time Limit: 1000 ms memory limit: 65536 K Description

There were a lot of people buying meals at noon, and the dining room was too crowded. It was hard to buy a meal. The children of science and engineering were still very smart. They went straight to Zhengtong supermarket. Haha, indeed, Zhengtong supermarket also sold meals, there are several kinds of dishes, which are much better than the canteen, and the price is no more expensive than the canteen. Also, soy milk is sent when you buy food, attracting a lot of children's shoes. So sometimes, there are still a lot of people, and queuing is inevitable. The tragedy is that there are only two cashier windows in the supermarket.

The problem is: at first two teams were waiting in the queue. Now we only study the first team. Now we give each person a number to ensure that the numbers are different, the people in front of the queue will leave after they have bought the meal. Some people will wait for the payment after they have picked the meal, while others will be smarter and there will be fewer people in the other team, leave the team directly to another team. I want to ask the total number of the team and the serial number of the person in a certain position.



Input

First, enter an integer m (M <10000), which indicates that there are currently m people. The number of M is entered in the second row, which indicates the number of each person. The number of an integer n (n <10000) is entered in the third row ), it indicates a total of N changes and inquiries in the queue. In the next n rows, join X indicates that the number is X (Ensure that the number is different from the previous number; leave y indicates that the person from the position y (Y is smaller than the length of the current queue) leaves the team; ask Z (Z is smaller than the length of the current queue) indicates the number of the person from the position Z; finish D indicates that D people have bought the meal and left; Length indicates the length of the query queue. Make sure that all data is within the int range.

Output

Output the corresponding answer for each query. Each answer occupies one row.

Sample Input
3
1 2 3
6
JOIN 4
ASK 2
LEAVE 2
LENGTH
FINISH 2
LENGTH
Sample output
2
3
1

Think of the entire queue as a queue. When someone leaves, the people behind them move forward.

The Code is as follows:

# Include <stdio. h> # include <string. h> int main () {int n, m, I, K, a [11000]; char s [10]; scanf ("% d", & M ); for (I = 1; I <= m; I ++) scanf ("% d", & A [I]); /* m always indicates the length of the Team */scanf ("% d", & N); While (n --) {scanf ("% s", S ); if (strcmp (S, "Join") = 0) {scanf ("% d", & A [I ++]); m ++ ;} else if (strcmp (S, "leave") = 0) {scanf ("% d", & K); for (I = K; I <m; I ++)/* if someone leaves, the team will be rescheduled. The front will not move, but the back will only move forward */A [I] = A [I + 1]; M --;} else if (strcmp (S, "Ask") = 0) {scanf ("% d", & K); printf ("% d \ n ", A [k]);} else if (strcmp (S, "Finish") = 0) {scanf ("% d", & K); for (I = 1; I <= (m-k); I ++)/* again, here we need to re-arrange */A [I] = A [I + k]; M = m-K;} else if (strcmp (S, "length") = 0) printf ("% d \ n", m) ;}return 0 ;}


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.