Directorio de artículos
Enlace de blog especial
Titulo original
Implementación de código (tiempo de espera de 1000 ms)
#include <iostream>
#include <string>
#include <map>
#include <cmath>
#include <limits.h>
using namespace std;
typedef multimap<int,int> mmii;
int main()
{
int members,id,attack,save1,save2,ret1,ret2;
cin>>members;
string func;
mmii boxingmap;
mmii::iterator p1,p2,temp;
boxingmap.insert(make_pair(1000000000,1));
while(members--)
{
cin>>id>>attack;
p1 = boxingmap.lower_bound(attack);
p2 = boxingmap.upper_bound(attack);
if(boxingmap.find(attack) == boxingmap.end())
{
save2 = abs(p2->first-attack);
save1 = (p1 != boxingmap.begin() ? abs((--p1)->first-attack) : INT_MAX);
if(save2 <= save1)
{
for(temp=p2,ret2=temp->second;temp != boxingmap.end() && abs(temp->first-attack) == save2;temp++)
{
if(temp->second < ret2)
ret2 = temp->second;
}
if(save2 == save1)
{
for(temp=p1,ret1=temp->second;p1!=boxingmap.begin() && abs(temp->first-attack) == save1;temp--)
{
if(temp->second < ret1)
ret1 = temp->second;
if(temp == boxingmap.begin())break;
}
}
}
for(temp=p1,ret1=temp->second;p1!=boxingmap.begin() && abs(temp->first-attack) == save1;temp--)
{
if(temp->second < ret1)
ret1 = temp->second;
if(temp == boxingmap.begin())break;
}
if(save2 < save1) ret1 = ret2;
else if(save2 == save1) ret1 = (ret1<ret2 ? ret1: ret2);
}
else
{
for(temp=p1,ret1=p1->second;temp!=p2;temp++)
{
if(temp->second < ret1)
ret1 = temp->second;
}
}
cout<<id<<' '<<ret1<<endl;
boxingmap.insert(make_pair(attack,id));
}
return 0;
}
Enviar resultado
Implementación de código (código conciso de 954ms)
#include <iostream>
#include <string>
#include <map>
#include <cmath>
using namespace std;
typedef multimap<int,int> mmii;
int main()
{
mmii boxingmap;
mmii::iterator p1,p2,ret;
string func;
int opernumbers,id,attack,temp1,temp2,tempname;
cin>>opernumbers;
boxingmap.insert(make_pair(1000000000,1));
while(opernumbers--)
{
cin>>id>>attack;
p1 = boxingmap.lower_bound(attack);
if(p1 == boxingmap.begin())
ret = p1;
else
{
p2 = p1--;
temp1 = abs(p1->first-attack),temp2 = abs(p2->first-attack);
if(temp2 < temp1) ret = p2;
else if(temp1 < temp2) ret = p1;
else ret = (p1->second < p2->second ? p1:p2);
}
tempname = ret->second;
cout<<id<<' '<<tempname<<endl;
if(ret->first != attack)
boxingmap.insert(make_pair(attack,id));
else
ret->second = (id < tempname ? id : tempname);
}
return 0;
}