Codeforces Round #312 (Div. 2) B. Amr and The Large Array
来源:互联网 发布:mssql分布式 编辑:程序博客网 时间:2024/05/22 09:01
题目地址
题目大意:给一个有n个元素的数组,求数组中出现次数最多的元素的位置区间,若有多个元素出现次数相同且最多,则输出区间长度最短的左边位置和右边位置
解题思路:记录每个数出现的次数及其左右位置,先按次数排序,再按区间长度排序,问题在于有些数存在,有些数不存在,但是我们只需要输出出现次数最多并且区间最短数的最先出现位置和最后出现位置,用一个map来解决有些数不存在的情况,将数组元素映射成连续的数,排序即可
#include <bits/stdc++.h>using namespace std;const int maxn = 1e5+100;const int maxm = 1e6+100;int a[maxn];struct P{ int cnt,r,l;}p[maxm];bool cmp(P a, P b){ if(a.cnt == b.cnt) return a.r-a.l < b.r-b.l; return a.cnt>b.cnt;}int main(){ int n; while(scanf("%d",&n) != EOF) { map<int,int> mp; for(int i = 0; i < maxm; i++) p[i].cnt = p[i].r = p[i].l = 0; int ct = 1; for(int i = 1; i <= n; i++) { scanf("%d",&a[i]); if(!mp[a[i]]) mp[a[i]] = ct++; p[mp[a[i]]].cnt++; if(p[mp[a[i]]].l == 0) p[mp[a[i]]].l = i; if(p[mp[a[i]]].l) p[mp[a[i]]].r = i; } sort(p+1,p+ct,cmp); printf("%d %d\n",p[1].l,p[1].r); }return 0;}
0 0
- Codeforces Round #312 (Div. 2) B. Amr and The Large Array
- Codeforces Round #312 (Div. 2) B. Amr and The Large Array
- Codeforces Round #312 (Div. 2) B. Amr and The Large Array
- Codeforces Round #312 (Div. 2)-B. Amr and The Large Array-暴力
- Codeforces Round #312 (Div. 2) B. Amr and The Large Array
- 【打CF,学算法——二星级】Codeforces Round #312 (Div. 2) B. Amr and The Large Array
- B. Amr and The Large Array(Codeforces Round #312 (Div. 2)+找出现次数最多且区间最小)
- codeforces #312 B B. Amr and The Large Array(统计)
- Codeforces 558B Amr and The Large Array(hash)
- Codeforces 558B Amr and The Large Array
- codeforces 558B Amr and The Large Array-yy
- 【36.86%】【codeforces 558B】Amr and The Large Array
- B. Amr and The Large Array
- B-codeforces558B-Amr and The Large Array
- B. Amr and Pins( Codeforces Round #287 (Div. 2))
- Codeforces 558B Amr and The Large Array 数组美丽值
- Amr and The Large Array
- Amr and The Large Array
- codeforces #316 E. Pig and Palindromes (dp~)
- MVC IIS创建过程问题收录
- BZOJ 3670 [Noi2014]动物园 (KMP next数组应用)
- 借点操作
- [Cocos2d塔防游戏开发]Cocos2dx-3.X完成塔防游戏《王国保卫战》--防御塔(四)之升级防御塔
- Codeforces Round #312 (Div. 2) B. Amr and The Large Array
- Java中的多线程Thread Runnable及android的handler
- (一)、一步一步学GTK+之开篇
- linux vim 编辑器命令记忆技巧
- java数组的行数和列数
- FineReport 填报报表数据自定义提交
- PHP实用函数 过滤数据
- Opencv videowriter保存的视频只有几K
- PHP文件加密解