题意:有N个士兵,每个士兵有一个属于自己的等级,他们要学一项魔杖飞行技术,等级高的士兵可以教等级低的士兵,等级低的士兵不可以教等级高的士兵,一个士兵只能教一个士兵,一个士兵也只能被一个士兵教,能形成这样的教学关系的士兵组成一个教学组,需要一根魔杖,问最少需要多少根魔杖。
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1800
——>>一个严格递增的序列需要一根魔杖,那么有多少个严格递增的序列就需要多少根魔杖,也就是哪个数字重复次数最多,该数字重复的次数就是所需的魔杖数。
开始用数组统计次数,RE,接着,用multiset来统计,TLE,最后,用map过了。
map<int, int> ma;第一个int代表士兵的等级,第二个int代表这个等级的士兵的人数,输入所有士兵的等级后,遍历一次map,取第二个int的最大值就是答案。
#include <cstdio>
#include <map>
#include <algorithm>
using namespace std;
map<int, int> ma;
int main()
{
int N, p, i;
while(scanf("%d", &N) == 1)
{
if(!N) continue;
ma.clear();
for(i = 1; i <= N; i++)
{
scanf("%d", &p);
if(ma.find(p) == ma.end())
{
ma[p] = 1;
}
else
{
ma[p]++;
}
}
int maxx = -1;
for(map<int, int>::const_iterator t = ma.begin(); t != ma.end(); t++)
{
int cnt = t->second;
maxx = max(maxx, cnt);
}
printf("%d\n", maxx);
}
return 0;
}