蓝桥杯双向排序,巧用API,但是会造成超时,算法思路很简单!
解答如下:
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
public class Day7 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int m = scanner.nextInt();
int a[] = new int[n];
for(int i = 0 ; i < n ; i ++){
a[i] = i + 1;
}
for (int i = 0; i < m; i++) {
int control = scanner.nextInt();
int index = scanner.nextInt();
if (control == 0) {
Arrays.sort(a,0,index);
for(int x = 0 , y = index -1; x < y ; x ++ , y --){
int temp = a[x];
a[x] = a[y];
a[y] = temp;
}
}else if(control == 1){
Arrays.sort(a,index - 1,n);
}
}
for (int i = 0;i < a.length;i++) {
System.out.print(a[i]+" ");
}
}
}