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书中看到.这种实现方法不是线程安全的,所以在项目实践中如果涉及到线程安全就不会使用这种方式.但是如果不 ...
随机推荐
- [LeetCode]题100:Same Tree
Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...
- css实现0.5px
使用缩放来0.5来实现,注意兼容各种浏览器 .frz-list li:after { content: ''; position: absolute; bottom:; height: 1px; wi ...
- 1022. Sum of Root To Leaf Binary Numbers从根到叶的二进制数之和
网址:https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers/ 递归调用求和,同时注意%1000000007的位置 /** * ...
- 颜色(color)透明
颜色写法: 1.英文单词 backgroud-color:red: 2. #fff background-color:#fff; 3.#fefefe background-color:fefefe; ...
- 学生信息管理系统(C语言)
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct student ...
- gcc4.9.1新特性
C family Support for colorizing diagnostics emitted by GCC has been added. The -fdiagnostics-color=a ...
- Convert Binary Search Tree to Doubly Linked List
Convert a binary search tree to doubly linked list with in-order traversal. Example Given a binary s ...
- python笔记13-文件读写
1.打开文件 f=open('a.txt','a+',encoding='utf-8')#f代表的是文件对象,叫句柄 f.seek(0)把文件指针到最前 文件打开模式有3种: 1:w写模式,它是不能读 ...
- windows2008 转 centos7 数据磁盘NTFS无损挂载
转换时 原win硬盘里作为系统稳盘的硬盘必须重新格式化才能装机 数据盘在安装ntfs-3g可以直接挂载 几个重要命令: #lsblk //查看硬盘情况 df -T 只可以查看已经挂载的分区和文件系统 ...
- 1.3 解决pip使用异常问题
1.3 解决pip使用异常问题 1.3.1 pip出现异常有一小部分童鞋在打开cmd输入pip后出现下面情况:Didnot provide a command Did not provide a co ...