23 Blue Bridge Cup Martians

Posted by vishi83 on Tue, 23 Nov 2021 05:09:54 +0100

Daily question 1 - Martians

subject

Man finally landed on the land of Mars and met the mysterious Martians. Neither humans nor Martians can understand each other's language, but our scientists have invented a way to communicate digitally. This communication method is like this. First, the Martians tell human scientists a very large number. After scientists solve the meaning of this number, they add a small number to the large number and tell the results to the Martians as human answers.
Martians use a very simple way to represent numbers - breaking fingers. Martians have only one hand, but this hand has thousands of fingers. These fingers are arranged in a row, numbered 1, 2, 3... 1, 2, 3. Any two fingers of Martians can exchange positions at will. That's how they count.
The following table shows the six three digits that can be formed when there are only three fingers and the numbers they represent:
Three digit 12313221321312321
The numbers represented are 1,2,3,4,5,6
Now you are lucky to be the first person on earth to communicate with Martians.
A Martian will show you his fingers, and scientists will tell you a small number to add.
Your task is to add the number represented by Martian fingers to the number told by scientists, and change the order of Martian fingers according to the addition result. Input data to ensure that the result will not exceed the range that Martian fingers can represent.

Input format

The input consists of three lines. The first line has a positive integer N, which represents the number of Martian fingers.
The second line is a positive integer M, which represents the small integer to be added.
The next line is an arrangement of N integers from 1 to N, separated by spaces, indicating the order of Martian fingers.

Output format

The output has only one line, which contains N integers, indicating the changed order of Martian fingers.
Every two adjacent numbers shall be separated by a space, and there shall be no redundant space.

Data range

1≤N≤10000,
1≤M≤100

sample input

5
3
1 2 3 4 5

sample output

1 2 4 5 3

Problem solving ideas

Pre knowledge

Dictionary order: in mathematics, dictionary or dictionary order (also known as word order, dictionary order, alphabetical order or dictionary order) is a method of arranging words alphabetically based on alphabetical order. This generalization is mainly to define the total order of the sequence of elements (commonly known as words in Computer Science) of an ordered and completely ordered set (commonly known as the alphabet).
For the arrangement of numbers 1, 2, 3... n, the sequence of different arrangements is determined by comparing the sequence of corresponding numbers one by one from left to right. For example, for the five digit permutations 12354 and 12345, the permutation 12345 is in the front and the permutation 12354 is in the back. According to this provision, the top of all the arrangements of the five numbers is 12345 and the bottom is 54321.

The problem is long. In fact, it is simply to rearrange the given number sequence into the next m larger permutations in the dictionary order (that is, combine the next m larger integers).
This problem has been transformed into a classic problem to find the next arrangement. There are many excellent solutions to this problem. Here are links and codes, which can be saved as a board. Next spread ----Next permutation problem solution

AC code

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        Solution s=new Solution();
        int n=scan.nextInt();
        int m=scan.nextInt();
        int[] ans=new int[n];
        for(int i=0;i<n;i++)//Read an existing spread into an array
          ans[i]=scan.nextInt();
        for(int i=0;i<m;i++)//Each time the nextPermutation(ans) function is executed, the ans array becomes the next larger permutation
          s.nextPermutation(ans);
        for(int i=0;i<n;i++)
          System.out.print(ans[i]+" ");//output
        scan.close();
    }
}
//Next arranged board
class Solution {
/*Core idea: find the most right digit to be changed to minimize the change range, select the larger number on the right of the number to exchange with the one with the smallest difference, and the subsequent numbers are arranged in ascending order
*/
    public void nextPermutation(int[] nums) {
        int len=nums.length;
        int left=len-2;
        int right=len-1;
        boolean flag=false;
        while(left>=0)
        {
            if(nums[left]<nums[right])
            {
                int find=len-1;
                for(;find>=right;find--)
                {
                    if(nums[find]>nums[left]&&(find+1>len-1||nums[find+1]<=nums[left]))
                    {
                        int tmp=nums[left];
                        nums[left]=nums[find];
                        nums[find]=tmp;
                        flag=true;
                        break;
                    }  
                }
            }
            if(!flag){
                left--;
                right--;
            }
            else
                break;
        }
        reverse(nums,right,len-1);
    }
    public void reverse(int[] nums,int start,int end)
    {
        while(start<end)
        {
            int tmp=nums[start];
            nums[start]=nums[end];
            nums[end]=tmp;
            start++;
            end--;
        }
    }
}

Topics: Algorithm leetcode