解题思路: 把1-n的值初始化大一点 保存最大最小值跟着变化最后根据下标排个序就行了
参考代码:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.math.BigInteger; import java.util.Arrays; import java.util.StringTokenizer; class Jgt { public long index, key; public Jgt(long index, long key) { this.index = index; this.key = key; } } public class Main { public static void main(String[] args) { Input sc = new Input(System.in); int n = sc.nextInt(); int m = sc.nextInt(); Jgt[] a = new Jgt[n + 1]; long Lindex = 999999999L; long Rindex = -1; for (int i = 1; i <= n; i++) { a[i] = new Jgt(i, i + 10000000); Lindex = Math.min(a[i].key, Lindex); Rindex = Math.max(a[i].key, Rindex); } while (m-- > 0) { char c = sc.next().charAt(0); int x = sc.nextInt(); if (c == 'L') { a[x].key = Lindex - 1; Lindex = a[x].key; } else { a[x].key = Rindex + 1; Rindex = a[x].key; } } Arrays.sort(a, 1, n + 1, (o1, o2) -> (int) (o1.key - o2.key)); for (int i = 1; i <= n; i++) { System.out.print(a[i].index + " "); } } static class Input { public BufferedReader reader; public StringTokenizer tokenizer; public Input(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 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 String nextLine() { String str = null; try { str = reader.readLine(); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } return str; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public Double nextDouble() { return Double.parseDouble(next()); } public BigInteger nextBigInteger() { return new BigInteger(next()); } } }
0.0分
4 人评分
P1002 (C语言代码)浏览:1019 |
C语言程序设计教程(第三版)课后习题7.1 (C语言代码)浏览:762 |
A+B for Input-Output Practice (III) (C语言代码)浏览:592 |
校门外的树 (C语言代码)浏览:733 |
C语言程序设计教程(第三版)课后习题5.5 (C语言代码)浏览:590 |
1013题解浏览:596 |
第三届阿里中间件性能挑战赛-总决赛亚军比赛攻略浏览:1170 |
简单的a+b (C语言代码)浏览:857 |
生日日数 (C语言代码)浏览:1575 |
班级人数 (C语言代码)浏览:981 |