士兵排列问题

解法一:

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中的应用局限。

解法三:

用数组模拟循环队列也可得到编号排列

解法四:

用链表的插入也可得到编号排列

。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值