牛客练习赛 A 牛牛的mex 存储前缀最小值和后缀最小值 思维题

链接:https://ac.nowcoder.com/acm/contest/7079/A
来源:牛客网

题目描述
牛牛现在有一个长度为 nnn 的序列 a1,a2,…,ana_1,a_2,\ldots,a_na1​,a2​,…,an​。现在牛牛有 qqq 次询问,每次想询问区间 [l,r][l,r][l,r] 的 mex 是什么。

一个序列的 mex 定义为最小未出现的自然数。
输入描述:

第一行两个整数 n,qn,qn,q 表示序列长度和询问次数。

接下来一行 nnn 个非负整数,表示序列 aia_iai​。

接下来 qqq 行,每行两个整数 li,ril_i,r_ili​,ri​ 表示询问的区间。

输出描述:

qqq 行,每行表示询问的答案。

示例1
输入
复制

5 2
4 3 0 1 2
2 4
1 5

输出
复制

2
5

备注:


注 意 题 目 的 备 注 : 每 个 数 各 不 相 同 , 并 且 在 0 到 n 之 间 \color{red}{注意题目的备注:每个数各不相同,并且在0到n之间} :0n

  • 所以求区间[L,R]里没出现过的最小的数,一定是在左右两边[1,L-1][R+1,n]的最小值
  • 注意把边界设置为N
  • 用两个数组分别存储前缀最小值后缀最小值,对于每次询问比较一下即可
#define debug
#ifdef debug
#include <time.h>
#include "win_majiao.h"
#endif

#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <math.h>

#define MAXN ((int)1e5+7)
#define ll long long int
#define INF (0x7f7f7f7f)
#define fori(lef, rig) for(int i=lef; i<=rig; i++)
#define forj(lef, rig) for(int j=lef; j<=rig; j++)
#define fork(lef, rig) for(int k=lef; k<=rig; k++)
#define QAQ (0)

using namespace std;

#define show(x...) \
    do { \
       cout << "\033[31;1m " << #x << " -> "; \
       err(x); \
    } while (0)

void err() { cout << "\033[39;0m" << endl; }
template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }

namespace FastIO{

    char print_f[105];
    void read() {}
    void print() { putchar('\n'); }

    template <typename T, typename... T2>
       inline void read(T &x, T2 &... oth) {
           x = 0;
           char ch = getchar();
           ll f = 1;
           while (!isdigit(ch)) {
               if (ch == '-') f *= -1; 
               ch = getchar();
           }
           while (isdigit(ch)) {
               x = x * 10 + ch - 48;
               ch = getchar();
           }
           x *= f;
           read(oth...);
       }
    template <typename T, typename... T2>
       inline void print(T x, T2... oth) {
           ll p3=-1;
           if(x<0) putchar('-'), x=-x;
           do{
                print_f[++p3] = x%10 + 48;
           } while(x/=10);
           while(p3>=0) putchar(print_f[p3--]);
           putchar(' ');
           print(oth...);
       }
} // namespace FastIO
using FastIO::print;
using FastIO::read;

int n, m, Q, K, a[MAXN], lef[MAXN], rig[MAXN];

signed main() {
#ifdef debug
    freopen("test.txt", "r", stdin);
    clock_t stime = clock();
#endif
    read(n, Q);
    for(int i=1; i<=n; i++) {
        read(a[i]);
        lef[i] = rig[i] = INF;
    }

    //注意左右边界设置为 n
    lef[0] = rig[0] = lef[n+1] = rig[n+1] = n;

    //分别统计前缀最小和后缀最小值,询问的时候比较一下即可
    for(int i=1; i<=n; i++) lef[i] = min(lef[i-1], a[i]);
    for(int i=n; i>=1; i--) rig[i] = min(rig[i+1], a[i]);
#if 0
    forarr(a, 0, n+1);
    forarr(lef, 0, n+1);
    forarr(rig, 0, n+1);
#endif
    while(Q--) {
        int L, R;
        read(L, R);
        printf("%d\n", min(lef[L-1], rig[R+1]));
    }





#ifdef debug
   clock_t etime = clock();
   printf("rum time: %lf 秒\n",(double) (etime-stime)/CLOCKS_PER_SEC);
#endif 
   return 0;
}



以下是java版本


import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

public class Main {
    public static final boolean debug = true;
    public static String INPATH = "C:\\Users\\majiao\\Desktop\\test.txt",
                          OUTPATH = "C:\\Users\\majiao\\Desktop\\out.txt";
    public static StreamTokenizer tok;
    public static BufferedReader cin;
    public static PrintWriter cout;

    public static long start_time = 0, out_time = 0;
    public static int n, m, K, Q, MAXN = (int)1e5+7, INF = 0x3f3f3f3f;
    public static byte buf[] = new byte[MAXN];
    public static int a[] = new int[MAXN], lef[] = new int[MAXN], rig[] = new int[MAXN];

    public static void main(String[] args) throws IOException {
        main_init();
        if(debug) { start_time = System.currentTimeMillis(); }
        if(false) { System.setOut(new PrintStream(OUTPATH)); }

        n = read_int(); Q = read_int();
        for(int i=1; i<=n; i++) a[i] = read_int();
        lef[0] = rig[0] = lef[n+1] = rig[n+1] = n;
        for(int i=1; i<=n; i++) lef[i] = Math.min(lef[i-1], a[i]);
        for(int i=n; i>=1; i--) rig[i] = Math.min(rig[i+1], a[i]);
        int L = 0, R = 0;
        while(Q-- > 0) {
            L = read_int(); R = read_int();
            cout.print(Math.min(lef[L-1], rig[R+1]));
            cout.print('\n');
        }
        if(debug) {
            out_time = System.currentTimeMillis();
            cout.printf("run time : %d ms\n", out_time-start_time);
        }
        cout.flush();
    }

    public static void main_init() {
        try {
            if (debug) {
                cin = new BufferedReader(new InputStreamReader(
                        new FileInputStream(INPATH)));
            } else {
                cin = new BufferedReader(new InputStreamReader(System.in));
            }
            cout = new PrintWriter(new OutputStreamWriter(System.out));
//            cout = new PrintWriter(OUTPATH);
            tok = new StreamTokenizer(cin);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String next_str() {
        try {
            tok.nextToken();
            if (tok.ttype == StreamTokenizer.TT_EOF)
                return null;
            else if (tok.ttype == StreamTokenizer.TT_NUMBER) {
                return String.valueOf((int)tok.nval);
            } else if (tok.ttype == StreamTokenizer.TT_WORD) {
                return tok.sval;
            } else return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static int read_int() { return Integer.parseInt(next_str()); }
    public static long read_long() { return Long.parseLong(next_str()); }
    public static double read_double() { return Double.parseDouble(next_str()); }
    public static BigInteger read_big() { return new BigInteger(next_str()); }
    public static BigDecimal read_dec() { return new BigDecimal(next_str()); }

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值