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

O(n)做法:hash + 尺取法

Posted by Ioencgc at 2023-05-01 19:32:33 on Problem 3320
#include<iostream>
#include<stack>
#include<string.h>
#include<cmath>
#include<iomanip>
#include<algorithm>
#include<climits>
#include<cstdio>
#include<vector>
#include<sstream>
#include<ctype.h>
#include<set>
#include<map>
#include<ctime>
#include<stdlib.h>
#include<queue>
#include<bitset>
#define usi unsigned int
#define ull unsigned long long
using namespace std;
template<typename TTT>inline void mr(TTT& theNumberToRead)
{
	theNumberToRead = 0;
	bool prn = false;
	char c = getchar();
	while (!isdigit(c))
	{
		if (c == '-')prn = true;
		c = getchar();
	}
	while (isdigit(c))
		theNumberToRead = 10 * theNumberToRead + (c ^ 48), c = getchar();
	if (prn)
		theNumberToRead = -theNumberToRead;
}
template<typename TTT>inline TTT mrr()
{
	TTT theNumberToRead = 0;
	bool prn = false;
	char c = getchar();
	while (!isdigit(c))
	{
		if (c == '-')prn = true;
		c = getchar();
	}
	while (isdigit(c))
		theNumberToRead = 10 * theNumberToRead + (c ^ 48), c = getchar();
	return prn ? -theNumberToRead : theNumberToRead;
}
template<typename T>void my_write(T x)
{
	if (x)
		my_write(x / 10),
		putchar(x % 10 ^ 48);
}
template<typename T>void mw(T x, char mid)
{
	if (x)
	{
		if (x < 0)
			putchar('-'),
			x = -x;
		my_write(x);
	}
	else putchar(48);
	if (mid)
		putchar(mid);
}
// ******************************************华丽的分割线******************************************
// ******************************************华丽的分割线******************************************
// ******************************************华丽的分割线******************************************
// ******************************************华丽的分割线******************************************
// ******************************************华丽的分割线******************************************
struct hash_map
{
	struct Data
	{
		int key, value, next;
		Data() {}
		Data(int k, int v, int n) :
			key(k), value(v), next(n) {}
	};
	Data a[1000001];
	static const int mod = 999983;
	int head[mod], n;
	inline int hash(int x)
	{
		return x % mod;
	}
	int& operator[](int x)
	{
		int h = hash(x);
		for (int i = head[h]; i; i = a[i].next)
			if (a[i].key == x)
				return a[i].value;
		a[++n] = Data(x, -1, head[h]), head[h] = n;
		return a[n].value;
	}
	hash_map()
	{
		n = 0;
		memset(head, 0, sizeof(head));
	}
}h;
int a[1000001];
int main()
{
	int p, n = 0, ans;
	mr(p);
	ans = p;
	for (int i = 0; i < p; ++i)
	{
		mr(a[i]);
		if (!~h[a[i]])
			++h[a[i]], ++n;
	}
	for (int l = 0, r = 0, sum = 0; ; ans = min(ans, r - l), sum -= !--h[a[l++]])
	{
		while (r < p && sum < n)
			if (!h[a[r++]]++)
				++sum;
		if (sum < n)
			break;
	}
	mw(ans, '\n');
	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