专栏文章

P1097 题解

P1097题解参与者 6已保存评论 6

文章操作

快速查看文章及其快照的属性,并进行相关操作。

当前评论
6 条
当前快照
1 份
快照标识符
@miqi55xi
此快照首次捕获于
2025/12/04 05:11
3 个月前
此快照最后确认于
2025/12/04 05:11
3 个月前
查看原文

思路

本题考察 map 的基本语法。
首先考虑桶,对于每一个数,对应的桶增加 11 即可。可是发现数据范围为 1.5×1091.5\times10^9,普通的数组无法开到这么大,会导致 MLE。
于是考虑 map。读入照常,对于读入的数 xx,执行 mxmx+1m_x\gets m_x+1。最后再用 it 遍历一遍 map,分别输出 map 中存储的对应数字及其数量,分别为 it->firstit->second
map 的一次修改是 O(logn)\mathcal{O}(\log n) 的,所以时间复杂度为 O(nlogn)\mathcal{O}(n\log n),可以通过此题。
AC CODE
CPP
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
int n,x;
map<int,int>mp;
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;++i)
		scanf("%d",&x),++mp[x];
	for(auto it=mp.begin();it!=mp.end();++it)
		printf("%d %d\n",it->first,it->second);
	return 0;
}

评论

6 条评论,欢迎与作者交流。

正在加载评论...