为什么会被MLE啊,第一次玩洛谷,真看不懂
查看原帖
为什么会被MLE啊,第一次玩洛谷,真看不懂
853554
ONELINE楼主2023/2/20 16:53

import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) {
 InputReader in = new InputReader(System.in);
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
int n = in.nextInt();
int T = in.nextInt();
int[] num = new int[n];
for(int i = 0;i < n;i ++) num[i] = in.nextInt();
while(T-- > 0)
{
	int k = in.nextInt();
	int l = 0;
	int r = n-1;
	int f = 0;
	while(l < r)
	{
		int mid = l - ( l - r) /2;
		if(num[mid] >= k) r = mid;
		else l = mid + 1;
		
	}
	if(num[l] == k)
		out.print(l+1+" ");
	else
		out.print(-1+" ");
}
        	
	out.flush();

	
	
}
}


class InputReader {
    private final BufferedReader buf;
    private StringTokenizer tkl;

    public InputReader(InputStream is) {
        buf = new BufferedReader(new InputStreamReader(is));
    }

    public boolean hasNext() {
        try {
            while (tkl == null || !tkl.hasMoreElements()) tkl = new StringTokenizer(buf.readLine());
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public String next() {
        return hasNext() ? tkl.nextToken() : null;
    }

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

    public double nextDouble() {
        return Double.parseDouble(next());
    }

    public long nextLong() {
        return Long.parseLong(next());
    }
}
2023/2/20 16:53
加载中...