赞
踩
身高从低到高
身高相同体重从轻到重
体重相同维持原来顺序
输入
4
100 100 120 130
40 30 60 50
输出:
2 1 3 4
输入
3
90 110 90
45 60 45
输出
1 3 2
// // Created by HANWENKE on 2022/9/5. // #include <iostream> #include <vector> #include <sstream> #include <algorithm> #include <map> using namespace std; static bool cmp(pair<int,int>&a,pair<int,int >&b){ if(a.first>b.first){ //如果a的heigh大于b.height就要将b放在前 return false; }else if(a.first==b.first&&a.second<b.second){ //如果身高相同--那么就要将体重体重小的放到前面 return true; }else if(a.second==b.second){ //如果体重相同则维持原来的顺序 return false; } return true; } int main(){ int n; cin>>n; vector<pair<int,int>>arr1(n); for(int i=0;i<n;i++){ int height; cin>>height; arr1[i].first=height; } map<int ,int>ismap; for(int i=0;i<n;i++){ int weight; cin>>weight; arr1[i].second=weight; ismap[i+1]=weight; } sort(arr1.begin(),arr1.end(),cmp); for(auto &x:arr1){ for(auto it=ismap.begin();it!=ismap.end();++it){ if((*it).second==x.second){ cout<<(*it).first<<" "; ismap.erase(it); //在这里一定要跳出循环--因为这里删除了一个元素-如果不跳出循环迭代器就会失效 break; } } } return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。