| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
Re:我能告诉你我只要30+行代码吗:In Reply To:分享自己满意的一段代码(hash+heap) Posted by:wanba at 2011-01-23 19:59:43 我能告诉你我只要30+行代码吗:
#include <cstdlib>
#include <cstdio>
#include <set>
#include <map>
const int NMAX = 1000000;
int P;
int a[NMAX];
int main()
{
while(scanf("%d", &P) != EOF)
{
std::set<int> point_num;
for(int i = 0; i < P; i++)
scanf("%d", &a[i]), point_num.insert(a[i]);
int n = point_num.size();
std::map<int, int> count;
int res = P;
int t = 0, s = 0, num = 0;
while(true)
{
while(t < P && num <n)
if(count[a[t++]] ++ == 0)
num ++;
if(num < n)
break;
res = std::min(res, t-s);
if(--count[a[s++]] == 0)
-- num;
}
printf("%d\n", res);
}
return 0;
}
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator