P1918 保龄球(排序+二分)

tech2022-07-07  209

题目描述

代码

#include<bits/stdc++.h> using namespace std; const int maxn = 1e5+50; int n,a[maxn],q,t; vector<pair<int,int> >v; bool cmp(pair<int,int> p1,pair<int,int> p2){ //比较函数 return p1.second < p2.second; } int f(int t){ //手动二分 int l = 0,r = n; while(l < r){ int mid = l + (r-l)/2; if(v[mid].second >= t ){ r = mid; } else{ l = mid+1; } } if(l == n) return 0; if(v[l].second == t){ return v[l].first; } return 0; } int main(){ //freopen("a.txt","r",stdin); cin>>n; for(int i = 0;i< n;i++){ cin>>t; pair<int,int> p = make_pair(i+1,t); v.push_back(p); } sort(v.begin(),v.begin()+n,cmp); cin>>q; for(int i = 0;i< q;i++){ cin>>t; cout<<f(t)<<endl; } return 0; }
最新回复(0)