#2,#4,#6MLE,求助,怎么修改?
查看原帖
#2,#4,#6MLE,求助,怎么修改?
470881
郭亮20510507027楼主2022/11/16 20:13
import java.io.*;

public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
		PrintWriter cout = new PrintWriter(new OutputStreamWriter(System.out));
		String[] words1,words2,words3;
		words1 = cin.readLine().split(" ");
		words2 = cin.readLine().split(" ");
		words3 = cin.readLine().split(" ");
		int n = Integer.parseInt(words1[0]),m = Integer.parseInt(words1[1]);
		int[] seq = new int[n];
		int[] sel = new int[m];
		for (int i = 0; i < n; i++) {
			seq[i] = Integer.parseInt(words2[i]);
		}
		for (int i = 0; i < m; i++) {
			sel[i] = Integer.parseInt(words3[i]);
		}
		for (int i = 0; i < words3.length; i++) {
			int l = 0, r = n-1,mid = 0;
			while (l<r) {
				mid = (l+r)/2;
				if (seq[mid] >= sel[i]) {
					r = mid;
				}else {
					l = mid+1;
				}
			}
			if (seq[l] == sel[i]) {
				cout.print((l+1)+" ");
			}else {
				cout.print("-1 ");
			}
			
		}
		cout.flush();
	}
}
2022/11/16 20:13
加载中...