WLS的数列 |
难度级别:A; 运行时间限制:1000ms; 运行空间限制:51200KB; 代码长度限制:2000000B |
试题描述
|
WLS喜欢数学,有一天,老师给了他一个长度为N的数列A,问他有多少不同的数。WLS觉得数列太长了,你能不帮他吗?
|
输入
|
第一行为一个正整数N,表示数列的长度。
第二行为N个正整数表示这个数列。 |
输出
|
输出数列中有多少不同的数。
|
输入示例
|
5 1 1 4 2 1 |
输出示例
|
3
|
其他说明
|
1<=N<=1000
1<=Ai<=10000 |
//============================================================================
// Name : hashtable.cpp
// Author : judyge
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<ctime>
#include<cmath>
#include<string>
using namespace std;
#define N 12350
#define MAX 12345
#define MAXSUM 12500000
#define CLR(arr, what) memset(arr, what, sizeof(arr))
template<class T>
class Hash
{
private:
int Key[N], Head[N], Next[N], Same[N];
int top;
public:
int count;
void search( int x);
void push(int x);
bool pre(int x);
void clear();
};
template<class T>
inline void Hash<T>::clear()
{
top = 0;
count = 0;
CLR(Head, -1);
CLR(Next, -1);
CLR(Same, 0);
}
template<class T>
inline bool Hash<T>::pre(int x)
{
int temp;
temp = abs(x) % MAX;
for(int i = Head[temp]; i != -1; i = Next[i]) //记录重复
{
if(x == Key[i])
{
Same[i]++;
return true;
}
}
return false;
}
template<class T>
inline void Hash<T>::push(int x)
{
if(pre(x) == true) //出现过,Same记录
return ;
else //没出现过
{
int temp;
temp = abs(x) % MAX;
Key[top] = x;
Next[top] = Head[temp];
Head[temp] = top;
Same[top] = 1;
top++;
}
}
template<class T>
inline void Hash<T>::search(int x)
{
int temp;
temp = abs(x) % MAX;
for(int i = Head[temp]; i != -1; i = Next[i])
{
if(x == -Key[i])
count += Same[i];
}
}
int main()
{
int m[100005];
Hash<int> h;
h.clear();
int n;
cin>>n;
for(int i=0;i<n;i++)
{
int j;
cin>>j;
m[i]=j;
h.push(j);
}
for(int k=0;k<n;k++)
{
h.search(m[k]);
}
cout<<h.count;
return 0;
}