首页 > 移动平台 > 详细

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

时间:2020-04-29 00:33:18      阅读:104      评论:0      收藏:0      [点我收藏+]

 技术分享图片

 

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

 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 }

 

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

原文:https://www.cnblogs.com/wszhu/p/12798023.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!