(PAT 1141) PAT Ranking of Institutions (排序+unorded_map的使用)

1141 PAT Ranking of Institutions (25 point(s))

After each PAT, the PAT Center will announce the ranking of institutions based on their students' performances. Now you are asked to generate the ranklist.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤10​5​​), which is the number of testees. Then N lines follow, each gives the information of a testee in the following format:

ID Score School

where ID is a string of 6 characters with the first one representing the test level: B stands for the basic level, A the advanced level and T the top level; Scoreis an integer in [0, 100]; and School is the institution code which is a string of no more than 6 English letters (case insensitive). Note: it is guaranteed that ID is unique for each testee.

Output Specification:

For each case, first print in a line the total number of institutions. Then output the ranklist of institutions in nondecreasing order of their ranks in the following format:

Rank School TWS Ns

where Rank is the rank (start from 1) of the institution; School is the institution code (all in lower case); ; TWS is the total weighted score which is defined to be the integer part of ScoreB/1.5 + ScoreA + ScoreT*1.5, where ScoreX is the total score of the testees belong to this institution on level X; and Ns is the total number of testees who belong to this institution.

The institutions are ranked according to their TWS. If there is a tie, the institutions are supposed to have the same rank, and they shall be printed in ascending order of Ns. If there is still a tie, they shall be printed in alphabetical order of their codes.

Sample Input:

10
A57908 85 Au
B57908 54 LanX
A37487 60 au
T28374 67 CMU
T32486 24 hypu
A66734 92 cmu
B76378 71 AU
A47780 45 lanx
A72809 100 pku
A03274 45 hypu

Sample Output:

5
1 cmu 192 2
1 au 192 3
3 pku 100 1
4 hypu 81 2
4 lanx 81 2

解题思路:

利用map的映射建立对应关系,由题目涉及到排序,而map中无法根据value进行排序,所以我们在读完数据计算完总成绩后,需要把map中的键值对放到一个vector中,然后在vector中根据键值对的值进行排序

这里如果使用map的话,最后两个测试点会超时.改用unorded_map就可以解决问题

unorded_map是无序的map,其实现原理是哈希表映射,查找效率为O(1)

这篇博客中博主很好地说明了map和unorded_map的关系: https://blog.youkuaiyun.com/BillCYJ/article/details/78985895

该题也要注意int和double转换的问题,输入和计算时是double型,输出时为int型

#include <iostream>
#include <algorithm>
#include <vector>
#include <unordered_map>
#include <set>
#include <string>
#include <string.h>
using namespace std;
unordered_map<string, double>  schoolToScore;
unordered_map<string, int> schoolToPeople;
vector<pair<string, int>> mainMap;
bool cmp(pair<string, int>& a, pair<string, int>& b) {
	if (a.second != b.second) return a.second > b.second;
	else {
		if (schoolToPeople[a.first] != schoolToPeople[b.first]) return schoolToPeople[a.first] < schoolToPeople[b.first];
		else {
			return strcmp(a.first.c_str(), b.first.c_str()) < 0;
		}
	}
}
int main() {
	int N;
	scanf("%d", &N);
	for (int i = 0; i < N; ++i) {
		string ID, School;
		double score;
		cin >> ID;
		scanf("%lf", &score);
		cin >> School;
		for (int j = 0; j < School.size(); ++j) {
			School[j] = tolower(School[j]);
		}
		
		if (ID[0] == 'T') {
			if (!schoolToScore[School]) schoolToScore[School] = score * 1.5;
			else schoolToScore[School] += score * 1.5;
		}
		else if (ID[0] == 'A') {
			if (!schoolToScore[School]) schoolToScore[School] = score;
			else schoolToScore[School] += score;
		}
		else {
			if (!schoolToScore[School]) schoolToScore[School] = score / 1.5;
			else schoolToScore[School] += score / 1.5;
		}
		if (!schoolToScore[School]) schoolToPeople[School] = 1;
		else schoolToPeople[School]++;
	}
	
	for (auto itr = schoolToScore.begin(); itr != schoolToScore.end();++itr) {
		mainMap.push_back(make_pair(itr->first, (int)itr->second));
	}
	sort(mainMap.begin(), mainMap.end(), cmp);
	
	printf("%d\n", schoolToPeople.size());
	int norder = 1;
	int order = 1;
	int pres = -1;
	for (int i = 0; i < mainMap.size(); ++i) {
		if (pres != mainMap[i].second) {
			norder = order;
			printf("%d %s %d %d\n", norder, mainMap[i].first.c_str(), mainMap[i].second, schoolToPeople[mainMap[i].first]);
			pres = mainMap[i].second;
			order++;
		}
		else {
			printf("%d %s %d %d\n", norder, mainMap[i].first.c_str(), mainMap[i].second, schoolToPeople[mainMap[i].first]);
			order++;
		}
	}
	system("PAUSE");
	return 0;
}

 

你帮我按刚才报错的问题修改一下,并生成完整代码# 4. 结果可视化 # 4.1 系数空间分布图 plot_coefficients <- function(model, var_name, dep_var) { # 提取系数数据 coef_data <- st_as_sf(model$SDF) # 创建地图 tm_shape(coef_data) + tm_dots( col = paste0(var_name, "_coef"), size = 0.5, palette = "-RdBu", midpoint = 0, title = paste(var_name, "系数"), style = "cont" ) + tm_layout(main.title = paste(dep_var, "模型:", var_name, "系数分布")) } # 示例:绘制NDVI在LST模型中的系数分布 plot_coefficients(lst_model, "NDVI", "LST") # 4.2 显著性排序图(百分比形式) plot_significance_ranking <- function(model, dep_var) { # 提取p值并计算显著性比例 sig_data <- st_as_sf(model$SDF) %>% st_drop_geometry() %>% select(ends_with("_p")) %>% pivot_longer(everything(), names_to = "variable", values_to = "p_value") %>% mutate(variable = gsub("_p", "", variable)) %>% group_by(variable) %>% summarise(significance_ratio = mean(p_value < 0.05, na.rm = TRUE)) %>% filter(!variable %in% c("Intercept")) %>% arrange(desc(significance_ratio)) # 绘制排序图 ggplot(sig_data, aes(x = reorder(variable, significance_ratio), y = significance_ratio, fill = significance_ratio)) + geom_col() + geom_text(aes(label = scales::percent(significance_ratio, accuracy = 1)), hjust = -0.1, size = 3) + coord_flip() + scale_fill_viridis_c() + labs(title = paste(dep_var, "模型自变量显著性排序"), subtitle = "站点p<0.05的比例", x = "自变量", y = "显著站点比例") + scale_y_continuous(labels = scales::percent, limits = c(0, 1.1)) + theme_minimal() } # 绘制两个模型的显著性排序 plot_significance_ranking(lst_model, "LST") plot_significance_ranking(tem_model, "TEM") # 4.3 残差空间分布图 plot_residuals <- function(model, dep_var) { # 提取残差数据 residuals <- st_as_sf(model$SDF) %>% mutate(abs_residual = abs(residual)) # 创建地图 tm_shape(residuals) + tm_dots( col = "residual", size = "abs_residual", palette = "viridis", title = c("残差值", "残差大小"), style = "cont" ) + tm_layout(main.title = paste(dep_var, "模型残差空间分布")) } # 绘制残差图 plot_residuals(lst_model, "LST") plot_residuals(tem_model, "TEM") # 5. 导出结果 # 保存系数和显著性 write.csv(st_drop_geometry(st_as_sf(lst_model$SDF)), "gwr_lst_results.csv") write.csv(st_drop_geometry(st_as_sf(tem_model$SDF)), "gwr_tem_results.csv") # 保存地图(可选) tmap_save(tm = plot_coefficients(lst_model, "NDVI", "LST"), filename = "lst_ndvi_coef.png")
最新发布
06-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值