打卡第32天 - P1918 - 2026 - 7 - 15

📅 2026/7/15 23:51:36 👁️ 阅读次数 📝 编程学习
打卡第32天 - P1918 - 2026 - 7 - 15

水一道题
其实就是在一个数组中找一个指定长度的元素,找到就返回原始下表,没找到就返回0

AC代码

#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
int main () {ios::sync_with_stdio(false); cin.tie(0);int n; cin>>n;vector<pair<int, int>> vec(n);for(int i=0; i<n; i++) {int t; cin>>t;vec.push_back({t, i+1});}int q; cin>>q;sort(vec.begin(), vec.end());while(q--) {int m; cin>>m;auto it = lower_bound(vec.begin(), vec.end(), make_pair(m, 0));if(it == vec.end()) cout<<"0\n";else if(it->first != m) cout<<"0\n";else cout<<it->second<<"\n";}return 0;
}