无聊发道水题,貌似电脑上的编译器有问题,只好多写了点代码
原理上就是set容器的应用
#include <cstdio> #include <iostream> #include <set> using namespace std; struct node { int num, grade; }; struct cpS{ bool operator()(const node &a,const node &b){return a.grade < b.grade;}}; struct cpB{ bool operator()(const node &a,const node &b){return a.grade > b.grade;}}; set< node, cpS> mms; set< node, cpS>::iterator its; set< node, cpB> mmb; set< node, cpB>::iterator itb; int main() { #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); #endif int n; while (scanf("%d", &n) && n) { mms.clear(); mmb.clear(); int a, b; node tp, q, p; tp.num = 1; tp.grade = 1000000000; mms.insert(tp); mmb.insert(tp); for (int i = 0; i< n; ++i) { scanf("%d%d", &tp.num, &tp.grade); a = tp.num; mms.insert(tp); mmb.insert(tp); its = mms.find(tp); its++; itb = mmb.find(tp); itb++; if (its != mms.end() && itb != mmb.end()) { q = *its; p = *itb; if (q.grade-tp.grade == tp.grade - p.grade) { b = p.num; } else if (q.grade-tp.grade < tp.grade - p.grade) { b = q.num; } else { b = p.num; } } else if (its != mms.end()) { q = *its; b = q.num; } else { p = *itb; b = p.num; } printf("%d %d\n", a, b); } } return 0; }