codeforces 558B. Amr and The Large Array 解题报告
题目链接:http://codeforces.com/problemset/problem/558/B
题目意思:给出一个序列,然后找出出现次数最多,但区间占用长度最短的区间左右值。
由于是边读入边比较,因此问题最关键的是,记录每个数第一次出现的位置,即左值。因为要保证次数是出现最多,因此需要一个cnt[]数组来记录出现次数。然后当最多出现次数与当前cnt[x]次数相同时,要选择区间较短的,再更新左右区间值。
赛中短路竟然想不出来~~~泪啊~~泪啊- >_<
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = 1e6 + ;
int cnt[maxn];
int pl[maxn]; // 记录每个数第一次出现的位置 int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif // ONLINE_JUDGE int n, a;
while (scanf("%d", &n) != EOF) {
memset(cnt, , sizeof(cnt));
memset(pl, , sizeof(pl));
int max_cnt = ;
int min_dis = ;
int l = , r = ; for (int i = ; i < n; i++) {
scanf("%d", &a); if (!cnt[a]) {
pl[a] = i;
}
cnt[a]++; if (max_cnt < cnt[a]) {
max_cnt = cnt[a];
l = pl[a], r = i;
min_dis = i - tl;
}
if (max_cnt == cnt[a] && i-pl[a] < min_dis) {
min_dis = i - pl[a];
l = pl[a];
r = i;
}
}
printf("%d %d\n", l+, r+);
}
return ;
}
codeforces 558B. Amr and The Large Array 解题报告的更多相关文章
- Codeforces 558B Amr and The Large Array
B. Amr and The Large Array time limit per test 1 second memory limit per test 256 megabytes input st ...
- codeforces 558B Amr and The Large Array-yy
题意:有一个数组.如今要削减它的尺寸.数组中同样元素的个数的最大值为数组的魅力值,要求削减后魅力值不能降低,同一时候要尽可能的把尺寸减到最小 分析:水题,主要是不要想复杂了.还有就是沉下心来做 代码: ...
- codeforces 558B B. Amr and The Large Array(水题)
题目链接: B. Amr and The Large Array time limit per test 1 second memory limit per test 256 megabytes in ...
- Codeforces Round #312 (Div. 2)B. Amr and The Large Array 暴力
B. Amr and The Large Array Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...
- B. Amr and The Large Array(Codeforces Round #312 (Div. 2)+找出现次数最多且区间最小)
B. Amr and The Large Array time limit per test 1 second memory limit per test 256 megabytes input st ...
- CF 558B(Amr and The Large Array-计数)
B. Amr and The Large Array time limit per test 1 second memory limit per test 256 megabytes input st ...
- 【LeetCode】697. Degree of an Array 解题报告
[LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...
- 【LeetCode】153. Find Minimum in Rotated Sorted Array 解题报告(Python)
[LeetCode]153. Find Minimum in Rotated Sorted Array 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode. ...
- 【36.86%】【codeforces 558B】Amr and The Large Array
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
随机推荐
- [译]bare repository
git init --bare 使用--bare创建的repository没有工作目录, 在这个repository中不能修改文件和commit. 中心repository必须是bare reposi ...
- 今天微信群需要人家通过吗?是微信bug吗
今天遇到微信群拉人的问题,所以来和大家取经,刚开始拉人一下就拉进去了,后来拉的需要人家通过,今天朋友些也帮我拉人也是这样的,所以想问下微信群扩容的问题.晚上有位朋友跟我说一次拉十个人,不能拉多,这样就 ...
- Swift实战之2048小游戏
上周在图书馆借了一本Swift语言实战入门,入个门玩一玩^_^正好这本书的后面有一个2048小游戏的实例,笔者跟着实战了一把. 差不多一周的时间,到今天,游戏的基本功能已基本实现,细节我已不打算继续完 ...
- Javascript高级程序设计——客户端检测
ECMAScript虽然是Javascript的核心,但是要在web中使用Javascript,那么BOM才是核心,BOM为我们提供了操作访问浏览器对象的借口, 但是由于BOM没有标准规范,导致存在不 ...
- 关于sql用<>不等于查询数据不对问题
平常查询数据 ' 当想要查询 不等于1 的数据的时候,一般会这样查询 ' 此处查询结果没查到所有想要的结果,如果night_flag 列数据为 null时,此行数据是查询不到的. 解决方法一: ' 解 ...
- iOS开发——UI进阶篇(十六)Quartz2D实战小例子
一.画线 只有在drawRect中才能获取到跟view相关联的上下文 - (void)drawRect:(CGRect)rect {} 一条线 // 1.获取跟当前View相关联的layer上下文(画 ...
- mysql bin-log 使用说明
如何开启bin-log日志? vi /etc/my.cnf [mysqld] log-bin=mysql-bin 默认开启 ls /usr/local/mysql/data/ mysql-bin.00 ...
- 2016年11月14日--SQL创建数据库、表-查、插、删、改
--创建数据库(create database 数据库名)create database hq20161114go --使用选择数据库(use 数据库名)use hq20161114go --创建学生 ...
- 51Nod 1766 树上的最远点对
Description 一棵树,询问两个端点编号分别在在 \([a,b]\) 和 \([c,d]\) 两个区间中的最长链. Sol 线段树+ST表. 树上最长链可以合并,只需要合并两个区间最长链的两个 ...
- xp远程桌面登陆需要身份验证问题解决
今天在客户这边部署了一个专门用于远程访问的计算机,但是系统是xp,windows服务器最新的已经到2012版本了,windows服务器从2008开始就要求远程桌面登陆支持nla,因此在使用xp登陆wi ...