冷血格鬥場

不知道爲什麼RuntimeError

#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 if(a2<a1)
			{
				return name_score_vec[i+1].first;
			}
			else
			{
				return name_score_vec[i+1].first>name_score_vec[i-1].first?name_score_vec[i-1].first:name_score_vec[i+1].first;
			}
		}
	}
 
	return 0;
} 
 
int main()
{
	vector<string> vecstr;
	int N=0,i=0;
	map<int,long> mapmembers;
	mapmembers.insert(pair<int,long>(1,1000000000));
	//cin>>N;
	scanf("%d",&N);
	while((N--)>0)
	{
		string strret;
		int id=0;
		long idv=0;
		//cin>>id>>idv;
		scanf("%d%d",&id,&idv);
		mapmembers.insert(pair<int,long>(id,idv));
 
		stringstream ss;
		ss<<id;
		int gid=GetId(id,mapmembers);
		//printf("%d %d\n",id,gid);
		stringstream ss2;
		ss2<<gid;
		strret=ss.str()+" "+ss2.str();
		vecstr.push_back(strret);
		//cout<<strret;
	}
	for(int j=0;i<vecstr.size();j++)
	{
		if(j==(vecstr.size()-1))
		{
			//cout<<vecstr[j];
			printf("%s",vecstr[j].c_str());
		}
		else
		{
			printf("%s\n",vecstr[j].c_str());
		}
	}
	return 0;
}

 

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