题目大意:求第k个v的编号
方法很多,这里给出比较快的,用STL中的map和vector
#include<iostream>
#include<cstdio>
#include<map>
#include<cstring>
#include<vector>
using namespace std;
const int N=1000005;
map<int,vector<int> >ma;
int n,m;
int main(){
freopen("Uva.in","r",stdin);
freopen("Uva.out","w",stdout);
while((scanf("%d%d",&n,&m))==2){
ma.clear();
for(int i =1; i<=n;i++){
int x;
scanf("%d",&x);
if(!ma.count(x))ma[x]=vector<int>();//新建vector
ma[x].push_back(i);
}
for(int i = 1; i<= m; i++){
int k,v;
scanf("%d%d",&k,&v);
if(!ma.count(v) ||ma[v].size()<k)//don't forget the second one
printf("0\n");
else printf("%d\n",ma[v][k-1]);
}
}
return 0;
}
本文介绍了一种利用C++ STL中的map和vector快速解决序列中特定元素位置的问题。通过实例演示了如何存储和检索序列中第k个指定值的位置。
543

被折叠的 条评论
为什么被折叠?



