熱血格鬥場

不知道爲啥提交失敗。。。。

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;

typedef pair<int,long> PAIR;
struct CmpByValue 
{
  bool operator()(const PAIR& lhs, const PAIR& rhs) 
  {
    return lhs.second < rhs.second;
  }
};

int GetId(int id,map<int,long> mapm)
{
	vector<PAIR> name_score_vec;
	map<int,long>::iterator p;
	for(p=mapm.begin();p!=mapm.end();p++)
	{
		name_score_vec.push_back(PAIR(p->first,p->second));
	}
    sort(name_score_vec.begin(), name_score_vec.end(), CmpByValue());

	for(int i=0;i<name_score_vec.size();i++)
	{
		if(id==name_score_vec[i].first)
		{
			if(name_score_vec.size()==2)
			{
				if(i==0)
					return name_score_vec[1].first;
				else
					return name_score_vec[0].first;
			}
			if(i==name_score_vec.size()-1)
			{
				return name_score_vec[i-1].first;
			}

			int a2=name_score_vec[i+1].second-name_score_vec[i].second;
			int a1=name_score_vec[i].second-name_score_vec[i-1].second;
			if(a2>=a1)
			{
				return name_score_vec[i-1].first;
			}
			else
			{
				return name_score_vec[i+1].first;
			}
		}
	}

	return 0;
} 

int main()
{
	int N=0,i=0;
	map<int,long> mapmembers;
	mapmembers.insert(pair<int,long>(1,1000000000));
	cin>>N;
	string strret;
	while((N--)>0)
	{
		int id=0;
		long idv=0;
		cin>>id>>idv;
		mapmembers.insert(pair<int,long>(id,idv));

		stringstream ss;
		ss<<id;
		int gid=GetId(id,mapmembers);
		stringstream ss2;
		ss2<<gid;
		strret=strret+ss.str()+" "+ss2.str()+"\n";
	}

	cout<<strret;

	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章