题解 UVA10474 【Where is the Marble?】

Anguei

2018-11-17 22:59:58

Solution

怎么都是用 stl 的呀,我来贴一个手写的二分。 需要注意这道题要找的是第一块合法的石头。所以不能在二分过程中第一次找到后就输出位置。 ```cpp void find(int target) { // target 是要寻找的石头,每组数据调用 k 次 int l = 1, r = n; while (l < r) { int mid = (l + r) >> 1; if (a[mid] >= target) r = mid; else l = mid + 1; } if (a[l] == target) std::cout << target << " found at " << l << std::endl; else std::cout << target << " not found\n"; } ```