HDU3785-寻找大富翁

STL最大堆算法

http://blog.csdn.net/fdl19881/article/details/6685744

#include
#include
#include
#include
#include
int n,m;
std::vector men;
 
int in;
int main()
{
	while (std::cin>>n>>m&&(m!=0||n!=0)){
		
		if(m>=n){
			for( int i = 0 ; i < n; ++i ){ 
				std::cin>>in;
				men.push_back(in);
			}  
			std::sort(men.begin(),men.end(),std::greater());
		}else{
			for( int i = 0 ; i < m; ++i ){ 
				std::cin>>in;
				men.push_back(in);
			}  

			std::make_heap( men.begin() , men.end() , std::greater() ); 
			int i=m;
			while(i!=n){  
				scanf("%d",&in);

				if ( men.front() < in ){  
					std::pop_heap( men.begin() , men.end() , std::greater() );  
					men.pop_back();  
					men.push_back(in);  
					std::push_heap( men.begin() , men.end() , std::greater() );  
				}  
				i++;
			}  
			std::sort_heap( men.begin() , men.end() , std::greater() );  
		}
		
		std::cout<


你可能感兴趣的:(基本数据结构与算法,AC水题)