解法一:
deque实现队头入队和队尾入队即可得到编号排列,每个士兵有二个属性:编号、能力值。
#include<iostream>
#include<algorithm>
#include<deque>
#include<vector>
using namespace std;
#define endl '\n'
struct shib {
int bianh;
int nengl;
};
bool cmp(struct shib& a, struct shib& b) {
return a.nengl > b.nengl;
}
void solve() {
deque<int> p;
int n, a;
cin >> n;
string st;
cin >> st;
for (int i = 0; i < st.size(); i++) {
if (st[i] == '0') p.push_front(i + 1);
else p.push_back(i + 1);
}
vector<struct shib> vec(n);
for (int i = 0; i < n; i++) {
vec[i].bianh = p[i];
cin >> a;
vec[i].nengl = a;
}
sort(vec.begin(), vec.end(), cmp);
for (int i = 0; i < n; i++) {
cout << vec[i].bianh << " ";
}
cout << endl;
}
int main() {
solve();
return 0;
}
解法二:感谢dalao的分享
再来一篇迭代器遍历的
#include <iostream>
#include<algorithm>
#include<string>
#include<deque>
using namespace std;
struct arm {
int located;
int ability;
}ar[100005];
bool compare(const arm& a, const arm& b) {
return a.ability > b.ability;
}
int main()
{
int t, a;
deque<int> d;
string str;
cin >> t >> str;
for (int i = 0; i < t; i++) {
if (str[i] == '1') d.push_back(i+1);
else d.push_front(i+1);
}
int i = 0;
for (auto it = d.begin(); it != d.end(); it++) {
ar[i++].located = *it;
}
for (int i = 0; i < t; i++) {
cin >> a;
ar[i].ability = a;
}
sort(ar, ar + t, compare);
for (int i = 0; i < t; i++)
cout << ar[i].located << " ";
cout << endl;
}
关于下标在stl中的应用局限。
解法三:
用数组模拟循环队列也可得到编号排列
解法四:
用链表的插入也可得到编号排列
。。。