Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:我能告诉你我只要30+行代码吗:

Posted by FreagleX at 2014-08-02 20:46:34 on Problem 3320
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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator