【小米OJ】带小学生排队(多元素排序后,按特定位置移动)

 

 

 解法:先按一定规则排序(身高从大到小,然后身高相同情况下,人数从小到大),然后移动元素。

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 struct xy{
 5   int x;
 6   int y;
 7 };
 8 bool cmp(struct xy a,struct xy b){
 9     if(a.x==b.x) return a.y<b.y;
10     else return a.x>b.x;
11 }
12 int main()
13 {
14     // please write your code here
15     int n;
16      ios::sync_with_stdio(false);///提高cin输入效率
17     cin>>n;
18     struct xy arr[n+10];
19     int ans[n+10];
20     for(int i=0;i<n;i++)
21        cin>>arr[i].x>>arr[i].y;
22     sort(arr,arr+n,cmp);
23     for(int i=0;i<n;i++){
24         int temp = arr[i].y;
25         for(int j=i;j>temp;j--) ans[j] = ans[j-1];
26         ans[temp] = i;
27     }
28     for(int i=0;i<n;i++)
29         cout<<arr[ans[i]].x<<" "<<arr[ans[i]].y<<" ";
30     return 0;
31 }

 

posted @ 2020-04-28 22:54  wusheng_z  阅读(224)  评论(0编辑  收藏  举报