加瓦86
查看原帖
加瓦86
1215903
hwljh楼主2024/3/4 21:48

/我想的是已经放了count瓶,下次放到这的试试count索引的位置,代价是(i-count),有人解决了麻烦踢一下我谢谢/

import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.CountedCompleter;
import java.util.function.BiConsumer;

public class Main {
    static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    static StreamTokenizer in = new StreamTokenizer(bf);
    static PrintWriter out = new PrintWriter(System.out);

    static int nextInt() throws IOException {
        in.nextToken();
        return (int) in.nval;
    }

    static String nextLine() throws IOException {
        return bf.readLine();
    }

    static boolean[] contains = new boolean[5 * 100000 + 5];

    public static void main(String[] args) throws IOException {
        int n = nextInt();
        int k = nextInt();

        int count = 0;//前面已经放了几瓶不同的
        int ans = 0;
//        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            int a = nextInt();
            if (!contains[a] && count < k) {
                //这一瓶没有放过且前面放过的瓶数小于k,大于等于k都可以直接跳过,已经完成了
                ans += i - count;
                //前面count瓶果汁是不同的,这次出现的是没出现过的,可以放到(count-1位置)
                //代价是索引之差,i-(下次不同放入的位置)
                contains[a] = true;
                count++;//又放了一瓶
            }
        }
        if (count < k) {
            out.println(-1);
        } else {
            out.println(ans);
        }


        out.flush();
    }
}
2024/3/4 21:48
加载中...