[Codeforces1174B]Ehab Is an Odd Person

Topic Link

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

The meaning of problems

To an array, and the exchange can only be an odd number is two, and asked lexicographically smallest sequence eventually obtained.

answer

  • Heart OS: the title, only the operation of the exchange parity. (Can be a little: beginning to think that the same internal sequence of odd order, even sequence internal order of the same, then the priority queue later thought clearly not the case ... can be changed by the parity parity with the internal order of the exchange.). The conclusion is that as long as there is parity in the array can easily exchange places. It can be exchanged with the parity interchanged by other with their different elements of the other elements.
  • Solution to a problem: When the array only odd / even, then output the original array, otherwise the output can be sorted array.

all

Well this is not an AC code actually a timeout in the sample. Card may be fast row sample? But with a List, shuffle reordering and no fruit. to be solved.

Code

import java.util.Arrays;
import java.util.Scanner;

public class OddReOrder {
    public static void main(String args[]) {
        Scanner in=new Scanner(System.in);
        int n=in.nextInt();
        int[] arr=new int[n];   
        for(int i=0;i<n;++i) {
            arr[i]=in.nextInt();
        }
        
        boolean oddFlag=false;
        boolean evenFlag=false;
        for(int i=0;i<n;++i) {
            if((arr[i]&1)==1){
                oddFlag=true;
            }
            else {
                evenFlag=true;
            }
            if(evenFlag&&oddFlag) {
                break;
            }
        }
        
        if(evenFlag&&oddFlag) {
            Arrays.sort(arr);
        }
        
        for(int i=0;i<n;++i) {
            System.out.print(arr[i]+" ");
        }
    } 
}

Guess you like

Origin www.cnblogs.com/coding-gaga/p/11006862.html