Pintia Problem Solution——7-12 Rainbow Bottle

7-12 Rainbow bottle

Original title:

The process of making rainbow bottles is (not) like this: first, a large number of empty bottles are laid out on the filling site, and then the balls of each color are evenly sprinkled into the batch of bottles in a certain order.

Suppose the rainbow bottle is to be filled with balls of N colors in order (you might as well number the order from 1 to N). Now there is one box of balls of each color in the factory, and workers need to move the balls box by box from the factory to the filling site. If the box of small balls that is moved happens to be of a color that can be filled, just unbox it and fill it; if not, put the boxes on a temporary shelf first. The method of stacking is to stack boxes one by one. After a color is filled, first check whether the box on the top of the shelf is the next color to be filled. If it is, take it out and fill it. Otherwise, go to the factory and bring another box.

If the order of shipment from the factory is better, workers can complete the filling smoothly. For example, if you want to fill 7 colors in order, and the factory ships the goods in the order of 7, 6, 1, 3, 2, 5, 4, the workers will first get the two colors 7 and 6 that cannot be filled, and put them in the order of 7 and 7. 6 are stacked on the shelf in the order above; when you get 1, you can load it directly; when you get 3, you have to temporarily put it on the color box No. 6; when you get 2, you can load it directly; then take 3 off the top of the shelf for filling ; Then I got 5, and put the temporary code on top of 6; finally I took color No. 4 and filled it directly; the remaining work was to take 5, 6, and 7 from the shelf in order and fill them in sequence.

But if the factory ships the goods in the order of 3, 1, 5, 4, 2, 6, 7, the workers will have to angrily toss the shelves because after filling the No. 2 color, they will not move the multiple boxes off the shelves. If you can't get box No. 3, you won't be able to successfully complete the mission.

In addition, the capacity of the shelves is limited. If the goods to be piled exceed the capacity, workers will not be able to complete the task smoothly. For example, the factory delivers goods in the order of 7, 6, 5, 4, 3, 2, 1. If the shelf is high enough to hold 6 boxes, it can still be completed successfully; but if the shelf can only hold 5 boxes, the worker will I'm going to be angry again...

This question asks you to judge whether the factory's delivery sequence allows workers to successfully complete their tasks.

Input format:

The input first gives 3 positive integers in the first line, which are the number of colors of rainbow bottles N (1< N ≤103), the capacity of the temporary shelf M (< N ), and the number of shipping sequences to be judged K.

Then there are K lines, each line gives N numbers, which is an arrangement from 1 to N , corresponding to the factory's delivery order.

Numbers in a row are separated by spaces.

Output format:

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

.

Problem-solving ideas:

  1. Import readlinethe module and create an interface object: First readlineintroduce the module and use createInterfacemethods to create an interface object rl. This object sets the input stream to standard input.
  2. Read input and store: By listening to 'line'events, store each line of input in an array buf.
  3. Parse the input: by buf[0].split(" ").map(Number)converting the first line of input into an array of three numeric values [N, M, K]. Where N represents the number of books on the entire bookshelf, M represents the maximum number of books that can be placed on the bookshelf, and K represents the number of test cases. For the input text from line 2 to line K+1, store it in an array order.
  4. Determine whether books can be placed: Use fora loop to traverse each test case. For each test case, initialize an empty array shelfand the current color number current_color. Then use fora loop to traverse the book numbers. If the book's number is equal to the current color number, add 1 to the color number and check whether the book can be removed from the bookshelf. If there are no books on the bookshelf or the bookshelf is full, the traversal stops. Finally, if the color number reaches N+1, it means that all books are placed and "YES" is output; otherwise, "NO" is output.

.

JavaScript (node) code:

const readline = require("readline");

const rl = readline.createInterface({
    
    
  input: process.stdin,
});

let buf = [];

rl.on("line", (input) => {
    
    
  buf.push(input);
});

rl.on("close", () => {
    
    
  const [N, M, K] = buf[0].split(" ").map(Number);

  for (let i = 1; i <= K; i++) {
    
    
    const order = buf[i].split(" ").map(Number);
    let shelf = [];
    let current_color = 1;

    for (let j = 0; j < N; j++) {
    
    
      if (order[j] === current_color) {
    
    
        current_color++;
        while (shelf.length > 0 && shelf[shelf.length - 1] === current_color) {
    
    
          shelf.pop();
          current_color++;
        }
      } else if (shelf.length < M) {
    
    
        shelf.push(order[j]);
      } else {
    
    
        break;
      }
    }

    if (current_color === N + 1) {
    
    
      console.log("YES");
    } else {
    
    
      console.log("NO");
    }
  }
});

.

Complexity analysis:

Time complexity: O(n)
Space complexity: O(n)

Guess you like

Origin blog.csdn.net/Mredust/article/details/133519183