解题思路: 把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 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复