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

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

题意:

给你n个价格 m个水果

你可以对水果按照你喜欢的价格标价
问你买完所有水果的最大开销最小开销

样例:

在这里插入图片描述

思路:

就是将出现次数最多的水果标上最小的价格就可以求最大 同理 求最小
所以我们需要用到Map来处理出现次数

问题:

map不好排序,去博客上学了挺久的 it->second的排序,好繁琐没看懂

题解:

我们可以使用idx(下标来) 将map的值映射过来,所以处理如下:

code:

#include 
using namespace std;const int N = 110;map
mp;int n,m;int num[N] ;int price[N];bool cmp(int a,int b){ return a>b;}int main(){ cin>>n>>m; int idx = 1; for(int i= 0; i
>price[i]; for(int i=0; i
>temp; if(!mp[temp]) mp[temp] = idx++; num[mp[temp]] ++; } sort(price,price+n); sort(num+1,num+1+idx,cmp); int minn = 0; int maxn = 0; int k = 0; for(int i=1;i<=idx;i++) { minn+=price[k++]*num[i]; } k = n -1; for(int i=1;i<=idx;i++) { maxn +=price[k--]*num[i]; } cout<
<<" "<

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

你可能感兴趣的文章
Nacos配置中心集群原理及源码分析
查看>>
nacos配置在代码中如何引用
查看>>
nacos配置新增不成功
查看>>
nacos配置自动刷新源码解析
查看>>
nacos集成分布式事务插件Seata的序列化问题,实际上是Seata本身存在bug!!
查看>>
Nacos集群搭建
查看>>
nacos集群搭建
查看>>
nacos集群网络分区对的影响和运维方式
查看>>
nacos集群节点故障对应用的影响以及应急方法
查看>>
nacos集群配置详解
查看>>
Nagios 3.0 Jumpstart Guide For Linux – Overview, Installation and Configuration
查看>>
nagios 实时监控 iptables 状态
查看>>
WAP短信格式解析及在Linux下用C语言实现
查看>>
nagios+cacti整合
查看>>
Nagios介绍
查看>>
nagios利用NSCient监控远程window主机
查看>>
nagios安装文档
查看>>
nagios服务端安装
查看>>
Nagios自定义监控脚本
查看>>
name_save matlab
查看>>