ZSLL @ 2023-03-16 18:12:30
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Arrays;
public class Main {
static int N = (int) 1e6 + 10;
static int n, m;
static int target;
static int binary(int num[], int target) {
int l = 0;
int r = num.length - 1;
int mid = l + (r - l) / 2;
while (r >= l) {
mid = l + (r - l) / 2;
if (num[mid] == target && num[mid - 1] != target) {
return mid;
}
else if (num[mid] < target) {
l = mid + 1;
}
else if (num[mid] > target) {
r = mid - 1;
}
}
return -1;
}
public static void main(String[] args) throws IOException {
StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
st.nextToken();
n = (int) st.nval;
st.nextToken();
m = (int) st.nval;
int num[] = new int[n + 1];
for (int i = 1; i <= n; i++) {
st.nextToken();
num[i] = (int) st.nval;
}
for (int i = 1; i <= m; i++) {
st.nextToken();
target = (int) st.nval;
pw.print(binary(num, target) + " ");
}
pw.flush();
}
}
by Singulet31258 @ 2023-03-16 18:26:51
因为 StreamTokenizer 比较慢,虽然在 JAVA 中算快的,但还是没资格跟 C/C++ 的那些读写函数比
by Loic_ @ 2023-03-16 18:27:02
请考虑使用C++。Java众所周知很容易TLE。