理解好排序的方式就行。
差值不一样,降序
差值一样,按照第二个数值升序
差值一样并且第二个数值也一样,按照id进行升序
#include <iostream> #include <algorithm> #include <cstdlib> #include <cstdio> #include <string> #include <cstring> #include <cmath> #include <ctime> #include <set> using namespace std; #define read() freopen("in.txt", "r", stdin) #define write() freopen("out.txt", "w", stdout) #define rep( i , a , b ) for ( int i = ( a ) ; i < ( b ) ; ++ i ) #define For( i , a , b ) for ( int i = ( a ) ; i <= ( b ) ; ++ i ) #define clr( a , x ) memset ( a , x , sizeof a ) #define cpy( a , x ) memcpy ( a , x , sizeof a ) #define _max(a,b) ((a>b)?(a):(b)) #define _min(a,b) ((a<b)?(a):(b)) #define LL long long const int maxNumber=10002; /*int gcd(int a,int b) { return b?gcd(b,a%b):a; } inline int lcm(int a,int b) { return a/gcd(a,b)*b; }*/ struct pm { int id; int num1; int num2; }; pm city[233]; int cmp(pm a,pm b) { if (a.num1-a.num2 == b.num1-b.num2 && a.num2 == b.num2) { return a.id < b.id; } if (a.num1-a.num2 == b.num1-b.num2) { return a.num2 < b.num2; } return a.num1 - a.num2 > b.num1 - b.num2; } int main() { //read(); int n; while(cin>>n) { for (int i = 0; i < n; ++i) { scanf("%d%d",&city[i].num1,&city[i].num2); city[i].id = i; } sort(city,city+n,cmp); for (int i = 0; i < n; ++i) { if (i == n - 1) { printf("%d\n",city[i].id ); }else { printf("%d ",city[i].id ); } } } return 0; }
时间: 2024-12-15 16:57:13