Flying to the Mars(map初体验)

Problem Description:

这里写图片描述
In the year 8888, the Earth is ruled by the PPF Empire . As the population growing , PPF needs to find more land for the newborns . Finally , PPF decides to attack Kscinow who ruling the Mars . Here the problem comes! How can the soldiers reach the Mars ? PPF convokes his soldiers and asks for their suggestions . “Rush … ” one soldier answers. “Shut up ! Do I have to remind you that there isn’t any road to the Mars from here!” PPF replies. “Fly !” another answers. PPF smiles :“Clever guy ! Although we haven’t got wings , I can buy some magic broomsticks from HARRY POTTER to help you .” Now , it’s time to learn to fly on a broomstick ! we assume that one soldier has one level number indicating his degree. The soldier who has a higher level could teach the lower , that is to say the former’s level > the latter’s . But the lower can’t teach the higher. One soldier can have only one teacher at most , certainly , having no teacher is also legal. Similarly one soldier can have only one student at most while having no student is also possible. Teacher can teach his student on the same broomstick .Certainly , all the soldier must have practiced on the broomstick before they fly to the Mars! Magic broomstick is expensive !So , can you help PPF to calculate the minimum number of the broomstick needed .
For example :
There are 5 soldiers (A B C D E)with level numbers : 2 4 5 6 4;
One method :
C could teach B; B could teach A; So , A B C are eligible to study on the same broomstick.
D could teach E;So D E are eligible to study on the same broomstick;
Using this method , we need 2 broomsticks.
Another method:
D could teach A; So A D are eligible to study on the same broomstick.
C could teach B; So B C are eligible to study on the same broomstick.
E with no teacher or student are eligible to study on one broomstick.
Using the method ,we need 3 broomsticks.
……

After checking up all possible method, we found that 2 is the minimum number of broomsticks needed.

Input:

Input file contains multiple test cases. In a test case,the first line contains a single positive number N indicating the number of soldiers.(0<=N<=3000) Next N lines :There is only one nonnegative integer on each line , indicating the level number for each soldier.( less than 30 digits);

Output:

For each case, output the minimum number of broomsticks on a single line.

Sample Input:

4
10
20
30
04
5
2
3
4
3
4

Sample Output:

1
2

Author
PPF@JLU

题目来源: http://acm.hdu.edu.cn/showproblem.php?pid=1800

翻译:

问题描述:

在8888年,地球由PPF帝国统治。随着人口的增长,PPF需要为新生儿寻找更多的土地。最后,PPF决定攻击统治火星的Kscinow。问题来了!士兵怎么能到达火星?PPF召唤他的士兵并询问他们的建议。“匆匆……”一名士兵回答。“闭嘴 !我是否必须提醒你,这里没有通往火星的道路!“PPF回复道。“飞!”另一个答案。PPF笑道:“聪明的家伙!虽然我们没有翅膀,但我可以从HARRY POTTER购买一些魔法扫帚来帮助你。“现在,是时候学会用扫帚飞行了!我们假设一名士兵有一个等级数字表示他的学位。具有较高等级的士兵可以教低级,也就是说前者的等级>后者。但是较低的不能教更高。一名士兵最多只能有一名教师,当然,没有老师也是合法的。同样地,一名士兵最多只能有一名学生,而没有学生也是可能的。老师可以用同样的扫帚教他的学生。当然,所有的士兵都必须在扫帚上练习才能飞到火星上!魔法扫帚是昂贵的!所以,你能帮助PPF计算所需的最小扫帚数量。
例如:
有5名士兵(ABCDE),等级编号:2 4 5 6 4;
一种方法:
C可以教B; B可以教A; 因此,ABC有资格在相同的扫帚上学习。
D可以教E;所以DE有资格在同一个扫帚上学习;
使用这种方法,我们需要2把扫帚。
另一种方法:
D可以教A; 所以AD有资格在同一个扫帚上学习。
C可以教B; 所以BC有资格在同一把扫帚上学习。
没有老师或学生的E有资格在一把扫帚上学习。
使用这种方法,我们需要3把扫帚。
……

在检查了所有可能的方法后,我们发现2是所需扫帚的最小数量。

输入:

输入文件包含多个测试用例。
在测试用例中,第一行包含一个正数N,表示士兵数量。(0 <= N <= 3000)
接下来N行:每行只有一个非负整数,表示每个士兵的等级数(少于30位数);

输出:

对于每种情况,在一行输出最小数量的扫帚。

样本输入:

4
10
20
30
04
5
2
3
4
3
4

样本输出:

1
2

题意概括:

假设一名士兵有一个等级数字表示他的学位。具有较高等级的士兵可以教低级,但是较低的不能教更高。一名士兵最多只能有一名教师,当然,没有老师也是合法的。同样地,一名士兵最多只能有一名学生,而没有学生也是可能的。老师可以用同样的扫帚教他的学生。当然,所有的士兵都必须在扫帚上练习才能飞到火星上!

分析:

1.只需要找到 士兵等级中数目最多的那个士兵数 该数目即为所求
2.可以用map,等级和该等级的士兵数一一对应即可。

代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string>
#include<cstring>
#include<map>
using namespace std;
int main()
{   
    long long  n,i,s,max;//因为等级数目很大,所以用 long long
    while(scanf("%lld",&n)!=EOF)//接下来要输入的等级数目
    {
    map<int,int>m; //等级和该等级的士兵一一对应

        for(i=0;i<n;i++)
        {
            scanf("%lld",&s);//输入士兵等级
            if(!m[s])       //如果士兵等级之前没有出现  对应值为1
                m[s]=1;
            else m[s]++; //之前出现过,值+1
        }
         map<int,int>::iterator it =m.begin();
         max=0;
        for(it=m.begin();it!=m.end();it++)//找到等级数目最多的士兵数,即为所求
            if(it->second>max)
                max=it->second;
        printf("%lld\n",max);

  }
  return 0;
}

运行结果:

这里写图片描述

总结:

最开始想的时数组,可是等级数会很大就放弃这个思路,选择用map。选对思路很重要,事半功倍。

猜你喜欢

转载自blog.csdn.net/shuisebeihuan/article/details/81161658