Codeforces Round 934 (Div. 2) --- C. MEX Game 1 --- 题解

C. MEX Game 1:

题目大意:

思路解析:

        重要的是那种只有一个的数字,因为如果这个数字有两个及以上,那么我可以再鲍勃删除之后,再拿,也一定能拿得到,所以瓶颈是只有一个的数字,如果这样的数字有多个,那我们只能选择最小的那个。然后循环所有数字,看我们最小的拿不到的数字是那个,这个数字就是答案。

代码实现:

        

import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class Main {

    static int[] a = new int[200005];
    public static void main(String[] args) throws IOException {
        int t = f.nextInt();
        for (int o = 0; o < t; o++) {
            int n = f.nextInt();
            for (int i = 0; i <= n; i++) {
                a[i] = 0;
            }
            for (int i = 0; i < n; i++) {
                int num = f.nextInt();
                a[num]++;
            }
            int f = 0;
            for (int i = 0; i <= n; i++) {
                if (a[i] == 0) {w.println(i); break;}
                else {
                    if (a[i] == 1){
                        if (f == 1) {w.println(i); break;}
                        else f = 1;
                    }
                }
            }
        }
        w.flush();
        w.close();
        br.close();
    }


    static PrintWriter w = new PrintWriter(new OutputStreamWriter(System.out));
    static Input f = new Input(System.in);
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    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());
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Studying~

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

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

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

打赏作者

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

抵扣说明:

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

余额充值