题解 | 小苯送礼物

小苯送礼物

https://www.nowcoder.com/practice/466e02d2177845589ab5fa5decc2857f

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

struct support {
    int dianzhan;
    int shouchan;
    int support_power;
    int bianhao;
};

int main() {
    int n, k;
    cin >> n >> k;

    vector<support> ben(n);
    for (int i = 0; i < n; ++i) {
        cin >> ben[i].dianzhan >> ben[i].shouchan;
        ben[i].support_power = ben[i].dianzhan + ben[i].shouchan * 2;
        ben[i].bianhao = i + 1;   // ✅ 编号从 1 开始
    }

    // 1️⃣ 按题目规则排序
    sort(ben.begin(), ben.end(),
        [](const support& a, const support& b) {
            if (a.support_power != b.support_power)
                return a.support_power > b.support_power;
            if (a.shouchan != b.shouchan)
                return a.shouchan > b.shouchan;
            return a.bianhao < b.bianhao;
        }
    );

    // 2️⃣ 取前 k 个
    vector<int> ans;
    for (int i = 0; i < k; ++i) {
        ans.push_back(ben[i].bianhao);
    }

    // 3️⃣ 按编号升序输出
    sort(ans.begin(), ans.end());

    for (int x : ans) {
        cout << x << " ";
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务