美文网首页
优先队列

优先队列

作者: _弓长_大人 | 来源:发表于2018-09-25 12:45 被阅读5次

    田浩廷 代码

    include <queue>

    include <iostream>

    include <stack>

    using namespace std;

    struct shoes
    {
    int power;
    int money;
    };

    struct cmp {
    bool operator ()(shoes &a, shoes &b) {
    if (a.power != b.power) {
    return a.power < b.power;
    }
    else {
    return a.money > b.money;
    }
    }
    };

    shoes a[100001];
    int main() {
    priority_queue<shoes, vector<shoes>, cmp> shooes;
    priority_queue<int> blood;
    int N, M, temp, ans, min = 0, k;
    bool flag = true;
    cin >> N >> M;
    shoes tempshoe;
    for (int i = 0; i < N; i++) {
    cin >> temp;
    blood.push(temp);
    }
    for (int i = 0; i < M; i++) {
    cin >> a[i].power;
    }
    for (int i = 0; i < M; i++) {
    cin >> a[i].money;
    }
    for (int i = 0; i < M; i++) {
    shooes.push(a[i]);
    }
    int len1 = blood.size();
    int len2 = shooes.size();
    //cout << len1 << " " << len2 << endl;
    for (int i = 0; i < len1; i++) {
    k = 0;
    ans = 9999999;
    temp = blood.top();
    blood.pop();
    for (int j = 0; j < len2; j++) {
    if (shooes.empty() || (shooes.top().power < temp)) { //最大能量踩不死猫
    if (j == 0) {
    flag = false;
    }
    break;
    }
    else {
    if (ans > shooes.top().money) {
    ans = shooes.top().money;
    tempshoe = shooes.top();
    }
    a[k++] = shooes.top();
    //t.push(shooes.top());
    shooes.pop();
    }
    }
    if (!flag) {
    break;
    }
    min += ans;
    //cout << ans << endl;
    /while (!t.empty()) {
    if (t.top().money == tempshoe.money && t.top().power == tempshoe.power) {
    t.pop();
    continue;
    }
    shooes.push(t.top());
    t.pop();
    }
    /
    for (int ii = 0; ii < k; ii++) {
    if (a[ii].money == tempshoe.money && a[ii].power == tempshoe.power) {
    continue;
    }
    shooes.push(a[ii]);
    }
    }
    if (flag) {
    cout << min << endl;
    }
    else {
    cout << "no" << endl;
    }
    return 0;
    }

    张楠的代码

    include <queue>

    include <iostream>

    include <stack>

    using namespace std;
    struct shoes
    {
    int power;
    int money;
    };
    struct cmp {
    bool operator ()(shoes &a, shoes &b) {//钱少 力量少
    if (a.money == b.money) {
    return a.power > b.power;
    }
    else {
    return a.money > b.money;
    }
    }
    };
    struct cmp1 {
    bool operator ()(shoes &a, shoes &b) {// 力量大
    if (a.power == b.power) {
    return a.money > b.money;
    }
    else {
    return a.power < b.power;
    }
    }
    };
    shoes a[100001];
    int main() {
    priority_queue<shoes, vector<shoes>, cmp1> shooes;
    priority_queue<shoes, vector<shoes>, cmp> sh;
    priority_queue<int> blood;
    int N, M, temp, ans, min = 0, k;
    bool flag = true;
    cin >> N >> M;
    shoes tempshoe;
    for (int i = 0; i < N; i++) {
    cin >> temp;
    blood.push(temp);
    }
    for (int i = 0; i < M; i++) {
    cin >> a[i].power;
    }
    for (int i = 0; i < M; i++) {
    cin >> a[i].money;
    }
    for (int i = 0; i < M; i++) {
    shooes.push(a[i]);//鞋大的排在前面
    }
    int t;
    int len1 = blood.size();//蚂蚁的个数
    int len2 = shooes.size();//鞋子的个数
    int temp1, temp2;
    int ff=0;//有没有鞋
    while(len1!=0) {//当蚂蚁的个数为零
    t = blood.top();//血量最大的
    len1--;//少一只蚂蚁
    blood.pop();
    while (!shooes.empty())//鞋不空
    {
    temp1 = shooes.top().power;//力量最大的鞋
    if (temp1 >= t)
    {
    sh.push(shooes.top());
    shooes.pop();
    }
    else
    {
    break;
    }
    }
    if (sh.empty())//没有鞋
    {
    cout << "No" << endl;
    return 0;
    }
    min += sh.top().money;//钱最少的弹出
    sh.pop();
    len2--;
    }
    cout << min << endl;
    return 0;
    }

    相关文章

      网友评论

          本文标题:优先队列

          本文链接:https://www.haomeiwen.com/subject/lqpnwttx.html