博客
关于我
C. Fruits 对map的映射排序
阅读量:164 次
发布时间:2019-02-28

本文共 2062 字,大约阅读时间需要 6 分钟。

为了求解购买所有水果的最大开销和最小开销,我们可以按照以下步骤进行:

  • 统计水果出现次数:使用哈希表记录每个水果名称的出现次数。
  • 排序价格:将所有价格从小到大排序,以便后续处理。
  • 排序水果列表:根据水果出现次数从多到少排序,出现次数多的水果标价较低,出现次数少的标价较高。
  • 计算最小开销:将排序后的价格从小到大依次乘以对应的水果数量,累加得到最小开销。
  • 计算最大开销:将排序后的价格从大到小依次乘以对应的水果数量,累加得到最大开销。
  • 以下是优化后的详细步骤说明:

    1. 统计水果出现次数

    • 使用哈希表(例如C++中的map)记录每个水果名称及其出现次数。
    • 遍历所有水果名称,更新哈希表中的计数。

    2. 排序价格

    • 将所有价格从小到大排序,准备后续分配。

    3. 排序水果列表

    • 根据水果出现次数从多到少排序,出现次数多的水果排在前面。
    • 这样,出现次数多的水果会被分配到较小的价格,出现次数少的分配到较大的价格。

    4. 计算最小开销

    • 遍历排序后的价格,从小到大依次乘以对应水果数量,累加得到总开销。

    5. 计算最大开销

    • 遍历排序后的价格,从大到小依次乘以对应水果数量,累加得到总开销。

    代码示例

    #include 
    using namespace std;int main() { int n, m; map
    mp; vector
    price; vector
    num; cin >> n >> m; // 读取水果名称和价格 for (int i = 0; i < n; ++i) { string temp; cin >> temp; if (mp.find(temp) != mp.end()) { mp[temp]++; } else { mp[temp] = 1; } } // 读取价格 for (int i = 0; i < m; ++i) { int p; cin >> p; price.push_back(p); } // 将价格从小到大排序 sort(price.begin(), price.end()); // 根据出现次数排序水果列表 vector
    > fruits; for (auto& pair : mp) { fruits.push_back(pair); } // 自定义排序,根据出现次数降序 sort(fruits.begin(), fruits.end(), [](const pair
    & a, const pair
    & b) { return a.second > b.second; }); // 分配价格 int min_cost = 0, max_cost = 0; vector
    k(n); for (int i = 0; i < n; ++i) { k[i] = i+1; // 索引从1开始 } // 计算最小开销 sort(k.begin(), k.end(), [](int a, int b) { return a > b; }); for (int i = 0; i < n; ++i++) { min_cost += price[i] * fruits[i].second * k[i]; } // 计算最大开销 sort(k.begin(), k.end(), [](int a, int b) { return a < b; }); for (int i = 0; i < n; ++i++) { max_cost += price[i] * fruits[i].second * k[i]; } cout << "最小开销:" << min_cost << endl; cout << "最大开销:" << max_cost << endl; return 0;}

    代码解释

    • 读取输入:首先读取水果数量和价格数量,然后读取每个水果名称和对应的价格。
    • 统计水果出现次数:使用哈希表记录每个水果名称的出现次数。
    • 排序价格:将价格从小到大排序,以便后续处理。
    • 排序水果列表:根据水果出现次数从多到少排序,确保出现次数多的水果排在前面。
    • 计算最小开销:将排序后的价格从小到大分配,计算最小开销。
    • 计算最大开销:将排序后的价格从大到小分配,计算最大开销。

    通过这种方法,我们可以有效地计算出购买所有水果的最大和最小开销。

    转载地址:http://ieoc.baihongyu.com/

    你可能感兴趣的文章
    Notepad ++ 安装与配置教程(非常详细)从零基础入门到精通,看完这一篇就够了
    查看>>
    Notepad++在线和离线安装JSON格式化插件
    查看>>
    notepad++最详情汇总
    查看>>
    notepad++正则表达式替换字符串详解
    查看>>
    notepad如何自动对齐_notepad++怎么自动排版
    查看>>
    Notes on Paul Irish's "Things I learned from the jQuery source" casts
    查看>>
    Notification 使用详解(很全
    查看>>
    NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
    查看>>
    NotImplementedError: Could not run torchvision::nms
    查看>>
    nova基于ubs机制扩展scheduler-filter
    查看>>
    Now trying to drop the old temporary tablespace, the session hangs.
    查看>>
    nowcoder—Beauty of Trees
    查看>>
    np.arange()和np.linspace()绘制logistic回归图像时得到不同的结果?
    查看>>
    np.power的使用
    查看>>
    NPM 2FA双重认证的设置方法
    查看>>
    npm build报错Cannot find module ‘webpack/lib/rules/BasicEffectRulePlugin‘解决方法
    查看>>
    npm build报错Cannot find module ‘webpack‘解决方法
    查看>>
    npm ERR! ERESOLVE could not resolve报错
    查看>>
    npm ERR! fatal: unable to connect to github.com:
    查看>>
    npm ERR! Unexpected end of JSON input while parsing near '...on":"0.10.3","direc to'
    查看>>