【PTA】L2-032 Rainbow bottle (25 points)

The process of making a rainbow bottle is (not) like this: first spread a large batch of empty bottles on the filling field, and then spread the balls of each color evenly into the batch of bottles in a certain order.

Suppose that the rainbow bottle is filled with balls of N colors in order (may wish to number the order from 1 to N). Now the factory has a box of each color of balls, and workers need to move the balls from the factory to the filling site one box at a time. If the box of balls is just the color that can be filled, unpack and fill directly; if not, put the box on a temporary shelf first, and stack the boxes one by one. When a color is filled, first check whether the box at the top of the shelf is the next color to be filled. If it is, take it off and fill it, otherwise go to the factory to move another box.

If the order of delivery in the factory is better, the workers can complete the filling smoothly. For example, if you want to load 7 colors in order, the factory will deliver the goods in the order of 7, 6, 1, 3, 2, 5, and 4. Then the workers will first get the colors 7 and 6 that cannot be loaded, and put them in the order of 7 and 6. 6 are stacked on the shelves in the order on the top; when you get 1, you can directly load them; when you get 3, you have to temporarily code them on the color box No. 6; when you get 2, you can directly load them; then remove 3 from the top of the shelf for loading ; Then get 5, and put the temporary code on top of 6; finally, take the color of No. 4 and fill it directly; the rest of the work is to sequentially remove 5, 6, and 7 from the shelf and fill it in sequence.

But if the factory ships in the order of 3, 1, 5, 4, 2, 6, and 7, the workers will have to toss the shelves angrily, because after filling the No. 2 color, they will not remove the multiple boxes on the shelves. If you can't get Box 3, it's impossible to successfully complete the mission.

In addition, the capacity of the shelves is limited. If the goods to be stacked exceed the capacity, the workers cannot successfully complete the task. For example, the factory delivers goods in the order of 7, 6, 5, 4, 3, 2, and 1. If the shelf is high enough to stack 6 boxes, it can still be completed smoothly; but if the shelf can only stack 5 boxes, the workers getting angry again...

In this question, please judge whether the factory's delivery order can allow the workers to successfully complete the task.

Input format:
Input first give 3 positive integers in the first line, which are the number of colors of rainbow bottles N (1<N≤10
​3
​​), the capacity of temporary shelves M (<N), and the need to judge Quantity K of the shipping order.

Then K lines, each giving N numbers, is a permutation of 1 to N, corresponding to the factory's shipping order.

The numbers on a line are separated by spaces.

Output format:
For each shipping sequence, if the worker can finish happily, output YES in one line; otherwise, output NO.

Input example:
7 5 3
7 6 1 3 2 5 4
3 1 5 4 2 6 7
7 6 5 4 3 2 1

Sample output:
YES
NO
NO

Thought analysis:

In fact, it is a boxing problem, to see if the ascending order can be achieved through the stack. It's just that this question has more conditions for the maximum capacity of the shelf.
Another point that is more pitiful is that it is necessary to pay attention to the fact that all the data must be read, and do not break when the shelves are full in the middle.

#include <bits/stdc++.h>
using namespace std;

int main()
{
    
    
	int n, m, k, x;
	scanf("%d%d%d",&n,&m,&k);
	while(k--)
	{
    
    
		stack<int> st;
		bool flag = 0;
		int cur = 1;
		int cnt = 0;
		for(int i = 0;i < n;i++)
		{
    
    
			scanf("%d",&x);
			if(flag) continue;	//已经NO就没必要继续了 
			if(x > cur)	//该货物不可直接放,装箱
			{
    
    
				if(!st.empty() && st.top() < x) flag = 1;	//limit1:若栈顶元素 < x,必无法按升序堆放 
				st.push(x);
				cnt++;
				if(cnt > m) flag = 1;	//limit2:货架超载 
			} 
			else if(x == cur)	//该货物可直接放 
			{
    
    
				cur++;
				while(!st.empty() && st.top() == cur)	//货架里能出就出 
				{
    
    
					st.pop();
					cnt--;
					cur++;
				}
			}
		}
		if(flag) cout<<"NO"<<endl;
		else cout<<"YES"<<endl;
	}
	return 0;
} 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325740837&siteId=291194637