解题思路: 把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 人评分
2004年秋浙江省计算机等级考试二级C 编程题(1) (C语言代码)浏览:758 |
C语言训练-大、小写问题 (C语言代码)浏览:2352 |
C语言程序设计教程(第三版)课后习题6.10 (C语言代码)浏览:749 |
C语言程序设计教程(第三版)课后习题4.9 (C语言代码)浏览:377 |
【简单计算】 (C语言代码)浏览:622 |
【金明的预算方案】 (C++代码)浏览:841 |
母牛的故事 (C语言代码)浏览:1428 |
C语言程序设计教程(第三版)课后习题5.6 (C语言代码)浏览:531 |
Minesweeper (C语言描述,蓝桥杯)浏览:1125 |
1024题解浏览:807 |