蓝桥杯真题《左移右移》

在这里插入图片描述
方法很多,这里提供两种,还有用链表写的就不提供了。
第一种方法

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());
    }
}
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小崔崔谁用的

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值