分析:借助STL的multiset实现,很方便。
#include<iostream>
#include<set>
using namespace std;
class Node
{
public:
Node(int _h=0,int _pos=0):h(_h),pos(_pos) //注意_h一定要在_pos的前面,方便lower_bound的调用
{
}
int pos,h;
};
struct cmp //仿函数
{
bool operator()(const Node& a,const Node& b)const
{
if(a.h==b.h) return a.pos<b.pos;
else return a.h<b.h;
}
};
multiset<Node,cmp> trees;
int main()
{
int i,n,m,v;
Node tmp;
multiset<Node,cmp>::iterator it;
while(scanf("%d%d",&n,&m)==2)
{
for(i=1;i<=n;i++)
{
scanf("%d",&tmp.h);
tmp.pos=i;
trees.insert(tmp);
}
while(m--)
{
scanf("%d",&v);
it=trees.lower_bound(v);
if(it==trees.end() || v<it->h)
printf("-1\n");
else
{
printf("%d\n",it->pos);
trees.erase(it);
}
}
trees.clear();
}
return 0;
}原文地址:http://blog.csdn.net/a809146548/article/details/45970455