Codeforces 37A - Towers
A. Towers
time limit per test
2 secondsmemory limit per test
256 megabytesinput
standard inputoutput
standard outputLittle Vasya has received a young builder’s kit. The kit consists of several wooden bars, the lengths of all of them are known. The bars can be put one on the top of the other if their lengths are the same.
Vasya wants to construct the minimal number of towers from the bars. Help Vasya to use the bars in the best way possible.
Input
The first line contains an integer N (1 ≤ N ≤ 1000) — the number of bars at Vasya’s disposal. The second line contains N space-separated integers li — the lengths of the bars. All the lengths are natural numbers not exceeding 1000.
Output
In one line output two numbers — the height of the largest tower and their total number. Remember that Vasya should use all the bars.
Examples
input
3
1 2 3
output
1 3
input
4
6 5 6 7
output
2 3
题意:给出n个栅栏,相同长度的叠放在一起,求最大的塔的高度及有多少个塔。
很容易想到用hash来做,一次AC!
代码如下:
#include<iostream> #include<algorithm> #include<cstring> using namespace std; int main() { int n; while (cin >> n) { int i,a[1005], hash[1005]; memset(hash, 0, sizeof(hash)); for (i = 0; i < n; i++) { cin >> a[i]; hash[a[i]]++; } int max=1, tot=0; for (i = 0; i < 1005; i++) { if (hash[i]) { tot++; if (hash[i] > max) max = hash[i]; } } cout << max << " " << tot << endl; } return 0; }