题意:
给你一组数据(n,k),n代表价值,k代表当前数据 之前有k组价值大于等于n的数据,让你排序,使得这组数据合理。
思路:
关键在于维护价值大于等于n的组数,所以应该先确当价值大的数据的位置。 只要确定了 当前数据 与 价值大于等于n的数据 的位置关系,后面价值小的数据不管插在哪都与当前数据无关了。
代码:
bool cmp(pair<int,int> a,pair<int,int> b)
{
if(a.first==b.first)
return a.second<b.second;
return a.first>b.first;
}
class Solution {
public:
vector<pair<int, int>> reconstructQueue(vector<pair<int, int>>& people) {
int n = people.size();
vector<pair<int,int> > ans(n);
if(n==0)
return ans;
sort(people.begin(),people.end(),cmp);
for(int i = 0;i<n;i++)
{
int pos = people[i].second;
for(int j = i;j>pos;j--)
{
ans[j] = ans[j-1];
}
ans[pos] = people[i];
}
return ans;
}
};