#include <iostream> #include <algorithm> #include <cstring> using namespace std; /************************************************************************/ /* HDU 5182题 http://acm.hdu.edu.cn/showproblem.php?pid=5182*/ /************************************************************************/ struct node { node() { number=0; first=0; second=0; difference=0; }; int number; int first; int second; int difference; }; node arr[120]; int cmp(const void* pa,const void* pb) { node* a = (node*)pa; node* b = (node*)pb; if(a->difference != b->difference)return b->difference - a->difference; else return a->second - b->second; } int main() { int n; node temp; while (cin >> n) { memset(arr,0,sizeof(arr)); for (int i=0;i<n;i++) { arr[i].number=i; cin >> arr[i].first >> arr[i].second; arr[i].difference = arr[i].first - arr[i].second; } //qsort(arr,n,sizeof(node),cmp); //冒泡排序 //当两个node的difference和second都相等时,按照题意应该满足这两个元素 //应该按照输入时的顺序排序,如果利用内置的qsort,这就无法满足要求,因此我用冒泡排序来替换 for (int i =0;i<n-1;i++) { for (int j =0;j<n-1-i;j++) { if (arr[j].difference < arr[j+1].difference) { temp = arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } } } for (int i =0;i<n-1;i++) { for (int j =0;j<n-1-i;j++) { if (arr[j].difference == arr[j+1].difference && arr[j].second > arr[j+1].second) { temp = arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } } } for (int i=0;i<n-1;i++) { cout << arr[i].number << " "; } cout << arr[n-1].number<<endl; } return 0; }