码迷,mamicode.com
首页 > 其他好文 > 详细

HDU ACM 5233 Gunner II

时间:2015-05-25 16:39:22      阅读:105      评论:0      收藏:0      [点我收藏+]

标签:c   c++   acm   算法   编程   

分析:借助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;      
}


HDU ACM 5233 Gunner II

标签:c   c++   acm   算法   编程   

原文地址:http://blog.csdn.net/a809146548/article/details/45970455

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!