首页 > ACM题库 > HDU-杭电 > HDU 2756-HOJ-Unique Snowflakes[解题报告]C++
2014
02-14

HDU 2756-HOJ-Unique Snowflakes[解题报告]C++

Unique Snowflakes

问题描述 :

Emily the entrepreneur has a cool business idea: packaging and selling snowflakes. She has devised a machine that captures snowflakes as they fall, and serializes them into a stream of snowflakes that flow, one by one, into a package. Once the package is full, it is closed and shipped to be sold.

The marketing motto for the company is "bags of uniqueness." To live up to the motto, every snowflake in a package must be different from the others. Unfortunately, this is easier said than done, because in reality, many of the snowflakes flowing through the machine are identical. Emily would like to know the size of the largest possible package of unique snowflakes that can be created. The machine can start filling the package at any time, but once it starts, all snowflakes flowing from the machine must go into the package until the package is completed and sealed.

输入:

The first line of each test chunk contains an integer specifying the number of test cases in this chunk to follow. Each test case begins with a line containing an integer n, the number of snowflakes processed by the machine. The following n lines each contain an integer (in the range 0 to 10^9, inclusive) uniquely identifying a snowflake. Two snowflakes are identified by the same integer if and only if they are identical. The input will contain no more than one million total snowflakes.
Please process to the end of the data file.

输出:

The first line of each test chunk contains an integer specifying the number of test cases in this chunk to follow. Each test case begins with a line containing an integer n, the number of snowflakes processed by the machine. The following n lines each contain an integer (in the range 0 to 10^9, inclusive) uniquely identifying a snowflake. Two snowflakes are identified by the same integer if and only if they are identical. The input will contain no more than one million total snowflakes.
Please process to the end of the data file.

样例输入:

1
5
1
2
3
2
1
1
5
1
2
3
2
1

样例输出:

3
3

#include <cstdio>
#include <set>
using namespace std;
template<class T> inline void checkmax(T &a, T b)
{if (b > a) a = b;}

int v[1000005];
int T, n;
set<int> S;

void work()
{
	scanf("%d", &n);
	for (int i = 1; i <= n; ++i) scanf("%d", &v[i]);
	int l = 1;
	int ans = 0;
	S.clear();
	for (int i = 1; i <= n; ++i) {
		int tmp = S.count(v[i]);
		if (tmp == 0) {
			S.insert(v[i]);
			checkmax(ans, i - l + 1);
		} else {
			while (v[l] != v[i]) {
				S.erase(v[l]);
				++l;
			}
			++l;
			checkmax(ans, i - l + 1);
		}
	}
	printf("%d\n", ans);
}

int main()
{
	while (scanf("%d", &T) != EOF)
		while (T--) work();
		
}

  1. 我没看懂题目
    2
    5 6 -1 5 4 -7
    7 0 6 -1 1 -6 7 -5
    我觉得第一个应该是5 6 -1 5 4 输出是19 5 4
    第二个是7 0 6 -1 1 -6 7输出是14 7 7
    不知道题目例子是怎么得出来的