此题有些奇怪(大概率是我理解有问题)

P2249 【深基13.例1】查找

可能 $\ldots$ 并不需要快读?
by _d_h_f @ 2024-04-11 19:29:28


~~我甚至 cin 都过了~~
by _d_h_f @ 2024-04-11 19:34:27


@[_d_h_f](/user/1057109) 666,但我就是想知道为什么有负数
by 20121202Tzy @ 2024-04-11 19:55:08


@[20121202Tzy](/user/1020835) 给个代码找下原因
by _d_h_f @ 2024-04-12 11:57:02


@[_d_h_f](/user/1057109) 0pts ```cpp #include <iostream> using namespace std; int n, m, x, a[1000001]; int read() { int s = 0; char c = getchar(); while (c >= '0' && c <= '9') { s = s * 10 + c - '0'; c = getchar(); } return s; } void write(int n) { if (n < 0) { putchar('-'); n = -n; } if (n >= 10) write(n / 10); putchar(n % 10 + '0'); } int find(int x) { int l = 1, r = n + 1; while (l < r) { int mid = l + (r - l) / 2; if (a[mid] >= x) r = mid; else l = mid + 1; } if (a[l] == x) return l; else return -1; } int main() { n = read(), m = read(); for (int i = 1; i <= n; i++) a[i] = read(); while (m--) { x = read(); write(find(x)); putchar(' '); } return 0; } ``````` 100pts ```cpp#include <iostream> using namespace std; int n, m, x, a[1000001]; int read() { int s = 0, w = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') w = -1; c = getchar(); } while (c >= '0' && c <= '9') { s = s * 10 + c - '0'; c = getchar(); } return s; } void write(int n) { if (n < 0) { putchar('-'); n = -n; } if (n >= 10) write(n / 10); putchar(n % 10 + '0'); } int find(int x) { int l = 1, r = n + 1, mid; while (l < r) { mid = l + (r - l) / 2; if (a[mid] >= x) r = mid; else l = mid + 1; } if (a[l] == x) return l; return -1; } int main() { n = read(), m = read(); for (int i = 1; i <= n; i++) a[i] = read(); while (m--) { x = read(); write(find(x)); putchar(' '); } return 0; } ``````
by 20121202Tzy @ 2024-04-12 14:19:31


|