codeforcesB. Amr and The Large Array
来源:互联网 发布:淘宝上卖保健品 编辑:程序博客网 时间:2024/05/24 07:36
B. Amr and The Large Arraytime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAmr 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.InputThe 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.OutputOutput 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)Input51 1 2 2 1Output1 5Input51 2 2 3 1Output2 3Input61 2 2 1 1 2Output1 5NoteA 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
数组的美丽值是出现次数最多的某些元素的出现次数,求最短的子数组,使他跟原数组美丽值相同。不会就乱写,当时思维比较混乱,WA了好几发,后来看了题解,才明白最优解法。先贴自己写的,在贴看了题解之后写的。
#include<map>#include<vector>#include<cstdio>#include<iostream>#include<cstring>#include<string>#include<algorithm>#include<cmath>#include<stack>#include<queue>#include<set>#define inf 0x3f3f3f3f#define mem(a,x) memset(a,x,sizeof(a))using namespace std;typedef long long ll;typedef pair<int,int> pii;inline int in(){ int res=0;char c; while((c=getchar())<'0' || c>'9'); while(c>='0' && c<='9')res=res*10+c-'0',c=getchar(); return res;}int a[100010];map<int,int> mp;vector<int> v[1000010];int main(){ int n=in(); for(int i=0;i<n;i++) { a[i]=in(); mp[a[i]]++; } int mx=0; set<int> s; map<int,int>::iterator it; for(it=mp.begin();it!=mp.end();it++) { if(it->second>=mx) { mx=it->second; } } for(it=mp.begin();it!=mp.end();it++) { if(it->second==mx) { s.insert(it->first); } } for(int i=0;i<n;i++) { if(s.find(a[i])!=s.end()) { v[a[i]].push_back(i); } } set<int>::iterator itt; int ans1,ans2; int mn=inf; for(itt=s.begin();itt!=s.end();itt++) { int len=v[*itt].size()-1; if(v[*itt][len]-v[*itt][0]<mn) { mn=v[*itt][len]-v[*itt][0]; ans1=v[*itt][0]; ans2=v[*itt][len]; } } cout<<ans1+1<<" "<<ans2+1; return 0;}l数组记录每个数的第一次出现位置,m数组记录每个数的出现个数。
#include<map>#include<vector>#include<cstdio>#include<iostream>#include<cstring>#include<string>#include<algorithm>#include<cmath>#include<stack>#include<queue>#include<set>#define inf 0x3f3f3f3f#define mem(a,x) memset(a,x,sizeof(a))using namespace std;typedef long long ll;typedef pair<int,int> pii;inline int in(){ int res=0;char c; while((c=getchar())<'0' || c>'9'); while(c>='0' && c<='9')res=res*10+c-'0',c=getchar(); return res;}int m[1000010];int l[1000010];int main(){ int n=in(); int x,ans1,ans2; int mx=0; int mn=inf; for(int i=0;i<n;i++) { x=in(); if(m[x]==0) { m[x]=1; l[x]=i; } else m[x]++; if(m[x]>mx) { mx=m[x]; ans1=l[x]; ans2=i; mn=ans2-ans1; } else if(m[x]==mx && i-l[x]<mn) { ans1=l[x]; ans2=i; mn=ans2-ans1; } } cout<<ans1+1<<" "<<ans2+1; return 0;}
0 0
- codeforcesB. Amr and The Large Array
- Amr and The Large Array
- Amr and The Large Array
- 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 #312 B B. 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
- codeforce 558B Amr and The Large Array 思维题
- 【36.86%】【codeforces 558B】Amr and The Large Array
- Codeforces 558B Amr and The Large Array 数组美丽值
- CF 558B Amr and The Large Array(暴力法)
- Codeforces Round #312 (Div. 2) B. Amr and The Large Array
- Genymotion模拟器离线安装 无需登陆
- Mybatis绑定错误的原因
- java中遍历MAP的几种方法
- kindeditor的跨域上传文件
- 在Java项目中拥抱Nodejs — 使用gruntjs编译typescript,并将生成的js合并、压缩
- codeforcesB. Amr and The Large Array
- Python3中遇到的错误和解决方法
- android 开源框架xUtils
- Linux软连接必须写绝对路径
- Android 最火的快速开发框架AndroidAnnotations使用详解
- OC知识点
- 2013下半年软件评测师真题考试分析-2
- 缘语言 V1.0 发布!
- Java学习——传说中的13个规范