http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2159
set是集合,查找lower_bound,复杂度log2(N)+1
http://www.cplusplus.com/reference/algorithm/lower_bound/
不必开数组,也开不开。
#include <iostream>
#include <set>
#include <string>
using namespace std;
typedef struct point
{
int x,y;
} p;
bool operator <(const p a, const p b)
{
if(a.x == b.x)
{
return a.y < b.y;
}
else return a.x < b.x;
}
int main()
{
ios::sync_with_stdio(false);
int n;
int i = 0;
while(cin>>n && n)
{
set<p> a;
cout<<"Case "<<++i<<":"<<endl;
while(n--)
{
string s;
cin>>s;
if(s == "add")
{
p temp;
cin>>temp.x>>temp.y;
a.insert(temp);
}
if(s == "find")
{
p temp;
cin>>temp.x>>temp.y;
int flag = 0;
set<p>::iterator it = a.lower_bound(temp);
for(; it != a.end(); ++it)
{
if((*it).x > temp.x && (*it).y > temp.y)
{
cout<<(*it).x<<' '<<(*it).y<<endl;
flag = 1;
break;
}
}
if(flag == 0)
{
cout<<-1<<endl;
continue;
}
}
if(s == "remove")
{
p temp;
cin>>temp.x>>temp.y;
set<p>::iterator it = a.find(temp);
a.erase(it);
}
}
cout<<endl;
}
}