8、sort排序中比较函数的几种应用方式
1、待排序中的元素作数组的下标或map的键值
例题:PAT甲级_1141 PAT Ranking of Institutions
#include<bits/stdc++.h>
using namespace std;
const int maxn = ; struct Node {
int personNum = ;
int scoreB = , scoreA = , scoreT = ;
int score;
}; map<string, Node> mp;
vector<string> ansSchoolName; bool cmp(string a, string b) {
if (mp[a].score != mp[b].score) return mp[a].score > mp[b].score;
else return mp[a].personNum < mp[b].personNum;
}
int main() {
int n;
scanf("%d", &n);
int score;
string id, schoolName;
for (int i = ; i < n; i++) {
cin >> id >> score >> schoolName;
transform(schoolName.begin(), schoolName.end(), schoolName.begin(), ::tolower);
mp[schoolName].personNum++;
if (id[] == 'A') mp[schoolName].scoreA += score;
else if (id[] == 'B') mp[schoolName].scoreB += score;
else mp[schoolName].scoreT += score;
}
for (auto it = mp.begin(); it != mp.end(); it++) {
ansSchoolName.push_back(it->first);
it->second.score = (int)(it->second.scoreA + it->second.scoreB / 1.5 + it->second.scoreT*1.5);
}
sort(ansSchoolName.begin(), ansSchoolName.end(), cmp);
int r = ;
printf("%d", ansSchoolName.size());
for (int i = ; i < ansSchoolName.size(); i++) {
if (i > && mp[ansSchoolName[i]].score != mp[ansSchoolName[i - ]].score) {
r = i + ;
}
printf("%d %s %d %d\n", r, ansSchoolName[i].c_str(), mp[ansSchoolName[i]].score, mp[ansSchoolName[i]].personNum);
}
return ; }
8、sort排序中比较函数的几种应用方式的更多相关文章
- spring 整合 mybatis 中数据源的几种配置方式
因为spring 整合mybatis的过程中, 有好几种整合方式,尤其是数据源那块,经常看到不一样的配置方式,总感觉有点乱,所以今天有空总结下. 一.采用org.mybatis.spring.mapp ...
- Django中提供的6种缓存方式
由于Django是动态网站,所有每次请求均会去数据进行相应的操作,当程序访问量大时,耗时必然会更加明显,最简单解决方式是使用: 缓存,缓存将一个某个views的返回值保存至内存或者memcache中, ...
- 【温故知新】——原生js中常用的四种循环方式
一.引言 本文主要是利用一个例子,讲一下原生js中常用的四种循环方式的使用与区别: 实现效果: 在网页中弹出框输入0 网页输出“欢迎下次光临” 在网页中弹出框输入1 网页输出“查询中……” 在 ...
- Springboot中IDE支持两种打包方式,即jar包和war包
Springboot中IDE支持两种打包方式,即jar包和war包 打包之前修改pom.xml中的packaging节点,改为jar或者war 在项目的根目录执行maven 命令clean pa ...
- JAVA高级架构师基础功:Spring中AOP的两种代理方式:动态代理和CGLIB详解
在spring框架中使用了两种代理方式: 1.JDK自带的动态代理. 2.Spring框架自己提供的CGLIB的方式. 这两种也是Spring框架核心AOP的基础. 在详细讲解上述提到的动态代理和CG ...
- Vue中常用的几种传值方式
Vue中常用的几种传值方式 1. 父传子 父传子的实现方式就是通过props属性,子组件通过props属性接收从父组件传过来的值,而父组件传值的时候使用 v-bind 将子组件中预留的变量名绑定为da ...
- sort排序中的坑
问题的产生原因: 在一篇阿里面试题的跟帖中,很多人应用sort()方法对数组进行排序.看似合情合理的代码,运行结果却频频出错.为什么呢?因为很多人都忽略掉了一点,那就是sort()排序默认情况下是按A ...
- Android中BroadcastReceiver的两种注册方式(静态和动态)详解
今天我们一起来探讨下安卓中BroadcastReceiver组件以及详细分析下它的两种注册方式. BroadcastReceiver也就是"广播接收者"的意思,顾名思义,它就是用来 ...
- JAVA中单例模式的几种实现方式
1 线程不安全的实现方法 首先介绍java中最基本的单例模式实现方式,我们可以在一些初级的java书中看到.这种实现方法不是线程安全的,所以在项目实践中如果涉及到线程安全就不会使用这种方式.但是如果不 ...
随机推荐
- 原生JS获取DOM 节点到浏览器顶部的距离或者左侧的距离
关于js获取dom 节点到浏览器顶/左部的距离,Jquery里面有封装好的offset().top/offset().left,只到父级的顶/左部距离position().top/position() ...
- activiti 快速入门--组任务(candidate users)分配(6)
http://blog.csdn.net/u011320740/article/details/53018040
- MongoDB一键安装(参数全部定制)
cat init_MongoDB.sh #!/bin/bash export lang=C export my_port=27019 export my_dbpath=/opt/var/data/m ...
- hello2部分代码解析
/** * Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.** You may not modify, us ...
- 3、简单了解Angular应用的启动过程
首先,了解一下目录结构: 然后,简明扼要的说一下应用的启动过程: 1.首先找到main.ts(模块启动入口),main.ts去找到app中的根模块app.module.ts 2.根模块app.modu ...
- test pthread code
#include <iostream> #include <pthread.h> using namespace std; ; void * add(void *); pthr ...
- Component(组件)
1.Component是一个模板的控制类用于处理应用和逻辑页面的视图部分. 2.Component时Angular2应用最基础的建筑砖块. 3.任何一个Component都是NgModule的一部分, ...
- UVa 11627 - Slalom 二分. oj错误题目 难度: 0
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- window配置 mysql 详细步骤
1.配置环境变量:右击“我的电脑”-->"高级"-->"环境变量" 3)在(系统变量)path变量(已存在不用新建)添加变量值:G:\MySQL\m ...
- plsql中文乱码问题
问题: 打开 plsql,执行 sql 语句,中文显示乱码: 解决方案: 1)输入 sql 语句 select userenv('language') from dual 查看数据库字符集 输出结 ...