蓝桥杯1458双向排序

这道题暴力解能拿60%的分数,如果想要满分得用到线性树,(如果不熟练考场上很容易写错)

60%:

java 复制代码
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String[] s = br.readLine().split(" ");
        int n=Integer.parseInt(s[0]),m=Integer.parseInt(s[1]);
        Integer num[]=new Integer[n+1];
        for (int i=1;i<=n;i++){
            num[i]=i;
        }
        for (int i=0;i<m;i++){
            String[] s1 = br.readLine().split(" ");
            int p=Integer.parseInt(s1[0]),q=Integer.parseInt(s1[1]);
            if (p==0){
                Arrays.sort(num,1,q+1,(a,b)->b-a);
            }else {
                Arrays.sort(num,q,n+1);
            }
        }
        for (int i=1;i<=n;i++){
            System.out.print(num[i]+" ");
        }
    }
}

100%:

java 复制代码
import java.io.IOException;
import java.util.*;
import java.io.*;

class InputReader {
    private final static int BUF_SZ = 65536;
    BufferedReader in;
    StringTokenizer tokenizer;

    public InputReader(InputStream in) {
        super();
        this.in = new BufferedReader(new InputStreamReader(in), BUF_SZ);
        tokenizer = new StringTokenizer("");
    }

    private String next() {
        while (!tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(in.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }
}

class Tree {
    public int l = 0;
    public int r = 0;
    public int sum = 0;
    public int lazy = -1;
}

public class Main {
    public static final int N = 100010;
    public static int n;
    public static int m;
    public static int op;
    public static int pos;
    public static Tree[] tree = new Tree[N << 2];

    public static void push_up(int rt) {
        tree[rt].sum = tree[rt << 1].sum + tree[rt << 1 | 1].sum;
    }

    public static void push_down(int rt) {
        int x = tree[rt].lazy;
        if (x != -1) {
            int len1 = tree[rt << 1].r - tree[rt << 1].l + 1;
            int len2 = tree[rt << 1 | 1].r - tree[rt << 1 | 1].l + 1;
            tree[rt << 1].sum = len1 * x;
            tree[rt << 1 | 1].sum = len2 * x;
            tree[rt << 1].lazy = tree[rt << 1 | 1].lazy = x;
            tree[rt].lazy = -1;
        }
    }

    public static void build(int l, int r, int rt) {
        tree[rt].l = l;
        tree[rt].r = r;
        tree[rt].lazy = -1;
        if (l == r) {
            tree[rt].sum = 1;
            return;
        }
        int mid = l + r >> 1;
        build(l, mid, rt << 1);
        build(mid + 1, r, rt << 1 | 1);
        push_up(rt);
    }

    public static void update1(int L, int R, int rt, int cnt) {
        if (cnt == 0) {
            return;
        }
        if (tree[rt].sum == cnt) {
            tree[rt].sum = tree[rt].lazy = 0;
            return;
        }
        push_down(rt);
        if (cnt < tree[rt << 1].sum) {
            update1(L, R, rt << 1, cnt);
        } else {
            update1(L, R, rt << 1 | 1, cnt - tree[rt << 1].sum);
            tree[rt << 1].sum = 0;
            tree[rt << 1].lazy = 0;
        }
        push_up(rt);
    }

    public static void update2(int L, int R, int rt, int cnt) {
        if (cnt == 0) {
            return;
        }
        int l = tree[rt].l;
        int r = tree[rt].r;
        int len = r - l + 1;
        if (len - tree[rt].sum == cnt) {
            tree[rt].sum = len;
            tree[rt].lazy = 1;
            return;
        }
        push_down(rt);
        int cnt1 = tree[rt << 1].r - tree[rt << 1].l + 1 - tree[rt << 1].sum;
        if (cnt < cnt1) {
            update2(L, R, rt << 1, cnt);
        } else {
            update2(L, R, rt << 1 | 1, cnt - cnt1);
            tree[rt << 1].sum = tree[rt << 1].r - tree[rt << 1].l + 1;
            tree[rt << 1].lazy = 1;
        }
        push_up(rt);
    }

    public static int query(int L, int R, int rt) {
        int l = tree[rt].l;
        int r = tree[rt].r;
        if (L <= l && r <= R) {
            return tree[rt].sum;
        }
        push_down(rt);

        int mid = l + r >> 1;
        int res = 0;
        if (L <= mid) {
            res += query(L, R, rt << 1);
        }
        if (R > mid) {
            res += query(L, R, rt << 1 | 1);
        }
        return res;
    }

    public static void main(String[] args) {
        for (int i = 0; i < (N << 2); i++) tree[i] = new Tree();
        InputReader cin = new InputReader(System.in);
        n = cin.nextInt();
        m = cin.nextInt();
        build(1, n, 1);
        while ((m--) != 0) {
            op = cin.nextInt();
            pos = cin.nextInt();
            if (op == 0) {
                int cnt0 = n - tree[1].sum;
                int cnt = Math.max(0, pos - cnt0);
                update1(1, n, 1, cnt);
            } else {
                int cnt1 = tree[1].sum;
                int cnt = Math.max(0, n - pos + 1 - cnt1);
                update2(1, n, 1, cnt);
            }
        }
        int vv1 = 0;
        int vv2 = 0;
        for (int i = n; i >= 1; i--) {
            if (query(i, i, 1) == 0) {
                System.out.print(i + " ");
            }
        }
        for (int i = 1; i <= n; i++) {
            if (query(i, i, 1) == 1) {
                System.out.print(i + " ");
            }
        }
        System.out.println();
    }
}
相关推荐
zww89491117 分钟前
家政多商户系统开发:从架构设计到落地实践
java·eclipse
snow@li41 分钟前
Java:微服务项目与单体项目区别、市场占有率全景深度分析
java·开发语言·微服务
还是大剑师兰特43 分钟前
Maven‑3.9.16 Windows 下载+安装+环境变量完整教程
java·windows·maven
吴声子夜歌1 小时前
Java——性能和效率
java·开发语言
meilindehuzi_a1 小时前
LangChain.js 对话 Memory 实战:History 持久化、截断与摘要压缩
java·javascript·langchain
飞Link1 小时前
动作方法中的分割与过度分割方法全解析(含代码实战与踩坑案例)
人工智能·python·算法·计算机视觉
铅笔小新z1 小时前
【数据结构】栈和队列
数据结构
zhougl9961 小时前
主流漏洞扫描工具
java
张宇Joaquin2 小时前
鲲鹏统一并行加速库KUPL--异步数据拷贝
线性代数·算法·矩阵·numpy
GlobalInfo2 小时前
创新不是追逐热点,是在变化中建立长期竞争力
大数据·算法