[算法题]chika和蜜柑

top k 问题, 通过排序可以解决问题, 只不过题目要求如果有多种方案, 希望总酸度尽可能小. 那么可以使用 sort 并自定义排序规则, 让甜度大的往前排, 甜度相同但酸度小的往前排, 可以升序也可以降序, 后边遍历求和时注意顺序即可, 题解代码如下:
 

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

int main()
{
    int n, k;
    cin >> n >> k;
    vector<pair<long, long>> arr(n);
    //输入酸度
    for(int i = 0; i < n; ++i)
    {
        cin >> arr[i].first;
    }
    //输入甜度
    for(int i = 0; i < n; ++i)
    {
        cin >> arr[i].second;
    }
    sort(arr.begin(), arr.end(), [&](const pair<long, long>& p1, const pair<long, long>& p2){
        if(p1.second == p2.second)
        {
            return p1.first > p2.first;
        }
        return p1.second < p2.second;
    });
    long acid = 0, sweet = 0;
    for(int i = arr.size() - 1; i >= n - k; --i)
    {
        acid += arr[i].first;
        sweet += arr[i].second;
    }
    cout << acid << " " << sweet << endl;
    return 0;
}

1. "Open Source Software for the Internet of Things: A Review" by Souvik Pal and Bivas Mitra, published in Journal of Computer Science and Technology, 2018. 2. "Open Source Software in Education: A Review of Research" by David Wiley, published in Journal of Educational Technology Development and Exchange, 2017. 3. "Open Source Software and Open Standards in e-Government: A Review" by Osman Balci, published in Journal of e-Government Studies and Best Practices, 2016. 4. "Open Source Software and Its Impact on Innovation: A Review of Research" by Eric von Hippel, published in Journal of Technology Transfer, 2015. 5. "Open Source Software for Disaster Management: A Review" by Amir Khorram-Manesh, published in Journal of Disaster Medicine and Public Health Preparedness, 2014. 6. "Open Source Software and the Future of Healthcare: A Review" by Aiden Gregg and John Fox, published in Journal of Healthcare Information Management, 2013. 7. "Open Source Software and Its Role in Sustainable Development: A Review" by Chika Ezeanya-Esiobu, published in Journal of Sustainable Development, 2012. 8. "Open Source Software in Government: A Review of Research" by Marijn Janssen and Jan Grijpink, published in Journal of e-Government, 2011. 9. "Open Source Software for Business: A Review of Research" by Brian Fitzgerald, published in Journal of Information Technology, 2010. 10. "Open Source Software and Intellectual Property: A Review" by Lawrence Rosen, published in Journal of Intellectual Property Law & Practice, 2009.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值