【打CF,学算法——二星级】Codeforces Round #312 (Div. 2) B. Amr and The Large Array

【CF简介】

提交链接:B. Amr and The Large Array


题面:

B. Amr and The Large Array
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller.

Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array.

Help Amr by choosing the smallest subsegment possible.

Input

The first line contains one number n (1 ≤ n ≤ 105), the size of the array.

The second line contains n integers ai (1 ≤ ai ≤ 106), representing elements of the array.

Output

Output two integers l, r (1 ≤ l ≤ r ≤ n), the beginning and the end of the subsegment chosen respectively.

If there are several possible answers you may output any of them.

Sample test(s)
Input
5
1 1 2 2 1
Output
1 5
Input
5
1 2 2 3 1
Output
2 3
Input
6
1 2 2 1 1 2
Output
1 5
Note

A subsegment B of an array A from l to r is an array of size r - l + 1 whereBi = Al + i - 1 for all1 ≤ i ≤ r - l + 1


题意: 求含有最多相同数字的最小子区间的范围。


解题: 我比较冒险的用了vector映射,没有爆内存,比较好的写法是开【100000】【3】的数组分别记录起始位置,结束位置,和相同数的数量。再用一个值记录是哪个元素重复,它重复的次数为多少(即当前最优值)。


代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <string>
#include <map>
#include <vector> 
#include <set>
#include <queue>
using namespace std;
vector <int> store[1000010];
int main()
{
    int n,tmp,maxn,beginn,endd,b,e;
    scanf("%d",&n);
	for(int i=1;i<=n;i++) 
	{
		scanf("%d",&tmp);
		store[tmp].push_back(i);
	}
	maxn=0;
	for(int i=1;i<=1000000;i++)
	{
		if(store[i].size()>maxn)
		{
			beginn=store[i][0];
			endd=store[i][store[i].size()-1];
			maxn=store[i].size();
		}
		else if(store[i].size()==maxn&&maxn)
		{
			b=store[i][0];
			e=store[i][store[i].size()-1];
			if((e-b)<(endd-beginn))
			{
				beginn=b;
				endd=e;
			}
		}
	}
	printf("%d %d\n",beginn,endd);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值