3 solutions
-
0
Guest MOD
-
1
二分,简单易懂
#include <bits/stdc++.h> using namespace std; const int N = 1e5+10; int n,m; int a[N]; int main(){ cin >> n >> m; for(int i = 0;i < n;i++){ cin >> a[i]; } sort(a,a+n); while(m--){ int w; cin >> w; int l = 0,r = n; while(l < r){ int mid = (l+r)/2; if(a[mid]<=w){ l = mid+1; }else{ r = mid; } } cout << l << endl; } return 0; } -
0
#include <iostream> #include <vector> #include <algorithm> int main() { int n, q; std::cin >> n >> q; // 存储每家店键盘的价格 std::vector<int> prices(n); for (int i = 0; i < n; ++i) { std::cin >> prices[i]; } // 对键盘价格进行排序 std::sort(prices.begin(), prices.end()); // 处理每次预算查询 for (int i = 0; i < q; ++i) { int budget; std::cin >> budget; // 使用二分查找找到第一个大于预算的位置 auto it = std::upper_bound(prices.begin(), prices.end(), budget); // 计算满足条件的店铺数量 int count = it - prices.begin(); // 输出结果 std::cout << count << std::endl; } return 0; }
- 1
Information
- ID
- 95
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- 9
- Tags
- (None)
- # Submissions
- 262
- Accepted
- 24
- Uploaded By