「Codeforces」B. Odd Swap Sort

B. Odd Swap Sort

https://codeforces.com/contest/1638/problem/B

topic description

There is an array A, traverse this array, if ai + ai + 1 a_i+a_{i+1}ai+ai+1If the sum is odd, exchange the positions of these two numbers swap ( ai , ai + 1 ) swap(a_i, a_{i+1})swap(ai,ai+1) so that the resulting sequence is a non-decreasing sequence.

enter description

The first line contains a T (1≤t≤105) denoted as the number of test cases

The first line of each test case contains an integer n (1≤n≤105) — the length of the array.

The second line of each test case contains n integers a1,a2,…,an (1≤ai≤109) — elements of the array.

output description

Is it a non-decreasing sequence: Yes or No

sample

#1

4
4
1 6 31 14
2
4 2
5
2 9 6 7 10
3
6 6 6
Yes
No
No
Yes

#2

1
5
2 9 6 2 10
No

hint

analyze

This topic can actually be simulated, but the complexity is O ( n 2 ) O(n^2)O ( n2 ), the amount of data is large, so it must be TLE.

Let's analyze it, the title says ai + ai + 1 a_i + a_{i+1}ai+ai+1The positions can only be exchanged when the sum is an odd number, then the following information can be known:

  1. To exchange two numbers must be one odd and one even, because only in this way can we get an odd number.
  2. The ones that are connected by even numbers or those that are connected by odd numbers must be even numbers, so the positions cannot be exchanged.

For the second piece of information above, since it is impossible to exchange positions even or odd numbers are connected, if the final result is to be guaranteed to be a non-decreasing sequence, then all even and odd sequences must be non-decreasing.

For example: [ 1 , 2 , 5 , 4 , 3 , 6 ] [1, 2, 5, 4, 3, 6][1,2,5,4,3,6 ] can get the even sequence[2, 4, 6] [2, 4, 6][2,4,6 ] and the odd sequence[1, 5, 3] [1, 5, 3][1,5,3 ] , it will be found that no matter what the exchange is, 5 and 3 cannot be avoided in the end.

  • First swap: [ 1 , 2 , 4 , 5 , 3 , 6 ] [1, 2, 4, 5, 3, 6][1,2,4,5,3,6 ] (5+4=9 is an odd number, one odd and one even can be exchanged)
  • Second swap: [ 1 , 2 , 4 , 5 , 3 , 6 ] [1, 2, 4, 5, 3, 6][1,2,4,5,3,6 ] (5+3=8 is an even number, two odd numbers cannot be exchanged)

Therefore, we only need to judge whether the even sequence and the odd sequence are increasing.

AC Code

public class Main {
    
    
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static StreamTokenizer st = new StreamTokenizer(br);
    static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));

    public static void main(String[] args) throws Exception {
    
    
        int T = nextInt();
        while(T-- != 0) {
    
    
            int n = nextInt();
            int[] A = new int[n+1];
            for(int i = 1; i <= n; i++) A[i] = nextInt();
            int even = 0, odd = 0; // 偶数和奇数
            boolean flag = true; // 默认最终是递增序列
            for(int i = 1; i <= n; i++) {
    
    
                if(A[i] % 2 == 0) {
    
     // 偶数序列
                    if(A[i] >= even) even = A[i]; // 是否递增
                    else {
    
    
                        flag = false;
                        break;
                    }
                } else {
    
     // 奇数序列
                    if(A[i] >= odd) odd = A[i]; // 是否递增
                    else {
    
    
                        flag = false;
                        break;
                    }
                }
            }
            out.println(flag ? "Yes" : "No");
        }
        out.flush();
    }

    public static int nextInt() throws Exception {
    
    
        st.nextToken();
        return (int) st.nval;
    }
}

Guess you like

Origin blog.csdn.net/qq_43098197/article/details/130481394