codeforce 558B Amr and The Large Array 思维题
来源:互联网 发布:上州屋渔具淘宝店 编辑:程序博客网 时间:2024/05/14 06:42
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.
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 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.
51 1 2 2 1
1 5
51 2 2 3 1
2 3
61 2 2 1 1 2
1 5
A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≤ i ≤ r - l + 1
题目的关键:每个位置只能使某个值出现的次数+1;
我的解题思路:对于所有可能出现的值记录其出现的位置,然后在所有可能值中查找。
如果某个值没有出现或是出现的次数少于 题目所需的最多出现次数,那么跳过,求这个值的最后一个和最前一个的距离。进行最短距离的更新。
具体做法: 对于每种值,如果出现次数达到要求,直接求最后一个和最前一个的距离。
看了别人的思路:比我要简单点,1~n出现次数要求最多的那个值,反过来说不管是何值,都要让他出现次数尽可能多,顺次遍历,每到一个点,某个值次数就+1,计算一下,这个值是否是当前出现次数最多的值,如果是或是相等,进行对应判断和更新。
具体做法:对于每个位置记录下当前位置的值的出现次数(这个次数是从左往右的次数)cnt[maxn];
对于每个值,记录下第一次出现的位置。
#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<cmath>#include<algorithm>#include<climits>#include<queue>#include<vector>#include<map>#include<sstream>#include<set>#include<stack>#include<utility>#pragma comment(linker, "/STACK:102400000,102400000")#define PI 3.1415926535897932384626#define eps 1e-10#define sqr(x) ((x)*(x))#define FOR0(i,n) for(int i=0 ;i<(n) ;i++)#define FOR1(i,n) for(int i=1 ;i<=(n) ;i++)#define FORD(i,n) for(int i=(n) ;i>=0 ;i--)#define lson num<<1,le,mid#define rson num<<1|1,mid+1,ri#define MID int mid=(le+ri)>>1#define zero(x)((x>0? x:-x)<1e-15)#define mp make_pair#define _f first#define _s secondusing namespace std;const int INF =0x3f3f3f3f;const int maxn= 100000+5 ;const int m= 1000000+5 ;//const int INF= ;typedef long long ll;const ll inf =1000000000000000;//1e15;//ifstream fin("input.txt");//ofstream fout("output.txt");//fin.close();//fout.close();//freopen("a.in","r",stdin);//freopen("a.out","w",stdout);//by yskysker123
int a[maxn];vector<int >ve[m];int n;int main(){ while(~scanf("%d",&n)) { for(int i=1;i<=m;i++) ve[i].clear(); int maxi=1; FOR1(i,n) { scanf("%d",&a[i]); ve[a[i]].push_back(i); if(ve[a[i]].size()>maxi) maxi=ve[a[i]].size(); } int ans=INF,st,ed; FOR1(i,m) { if(ve[i].size()<maxi) continue; for(int j=0;j<= ve[i].size()-maxi;j++) { if( ve[i][ve[i].size()-1 ]-ve[i][0]+1<ans ) {ans=ve[i][ve[i].size()-1 ]-ve[i][0]+1;st=ve[i][0];ed=ve[i][ve[i].size()-1];} } } printf("%d %d\n",st,ed); } return 0;}
- codeforce 558B Amr and The Large Array 思维题
- codeforce Amr and The Large Array
- B. Amr and The Large Array
- B-codeforces558B-Amr and The Large Array
- Codeforces 558B Amr and The Large Array(hash)
- CF 558B(Amr and The Large Array-计数)
- Codeforces 558B Amr and The Large Array
- codeforces 558B Amr and The Large Array-yy
- CF 558B Amr and The Large Array
- 【36.86%】【codeforces 558B】Amr and The Large Array
- codeforces #312 B B. Amr and The Large Array(统计)
- Amr and The Large Array
- Amr and The Large Array
- Amr and The Large Array
- Codeforces 558B Amr and The Large Array 数组美丽值
- CF 558B Amr and The Large Array(暴力法)
- Code Forces 558 B. Amr and The Large Array(水~)
- codeforcesB. Amr and The Large Array
- uva 11732 "strcmp()" Anyone? (trie+左儿子右兄弟表示法)
- 网易内推电话面试(一面)
- poj 1995 Raising Modulo Numbers 【快速幂 裸题】
- noip2000税收与补贴问题 (模拟,排序)
- iOS appStore中的应用 实现版本升级功能
- codeforce 558B Amr and The Large Array 思维题
- maven 手动加入jar到本地资源库
- 阿里巴巴2015实习生笔试真题
- hdu2602 Bone Collector
- 【洛谷】P1085 不高兴的津津
- 智慧旅游项目技术文档
- 弹出窗口之Cookie控制
- 简单几何 CF Gym 100338B Geometry Problem:
- Struts1与Struts2的那些事