方法很多,这里提供两种,还有用链表写的就不提供了。
第一种方法
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int[][] e = new int[n + 1][2];
for(int i = 1; i <= n; i ++ )
e[i][0] = e[i][1] = i;
int min = 1, max = n;
while(m -- > 0) {
char c = sc.next().charAt(0);
int x = sc.nextInt();
e[x][1] = (c == 'L' ? -- min : ++ max);
}
Arrays.sort(e, 1, n + 1, ((o1, o2) -> o1[1] - o2[1]));
for(int i = 1; i <= n; i ++ )
System.out.print(e[i][0] + " ");
}
}
第二种方法(忽略快读)
import java.io.*;
import java.util.*;
public class Main {
static int N = 200010;
static boolean st[] = new boolean[N];
static int a[] = new int[N], ops[][] = new int[N][2];
static int b[] = new int[N];
public static void main(String[] args) {
IR sc = new IR();
int n = sc.nextInt();
int m = sc.nextInt();
int l = 1, r = n, size = 0;
for(int i = 1; i <= m; i ++ ) {
char c = sc.next().charAt(0);
int x = sc.nextInt();
ops[i][0] = c == 'L' ? 1 : 2;
ops[i][1] = x;
}
while(m > 0) {
int op = ops[m][0];
int x = ops[m][1];
m -- ;
if(st[x]) continue;
st[x] = true;
if(op == 1)
a[l ++ ] = x;
else
a[r -- ] = x;
}
for(int i = 1; i <= n; i ++ ) {
if(!st[i]) b[size ++ ] = i;
}
size = 0;
for(int i = l; i <= r; i ++ ) {
a[i] = b[size ++ ];
}
for(int i = 1; i <= n; i ++ )
System.out.print(a[i] + " ");
}
}
class IR {
public BufferedReader reader;
public StringTokenizer tokenizer;
public IR() {
reader = new BufferedReader(new InputStreamReader(System.in), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}