hdu 1800 Flying to the Mars

http://acm.hdu.edu.cn/showproblem.php?pid=1800

soldiers学习使用魔法扫帚,水平高的可以教水平在他之下的,每个老师有而只有一个学生,并且可以共用一个魔法扫帚练习,要求每个soldier都要有练习魔法扫帚的机会,求最少需要多少个魔法扫帚。

如士兵A B C D E水平分别为2 4 5 6 4

方法可以是:C教B,B教A,D教E,这样ABC共用一把扫帚,DE共用一把扫帚;只用两把扫帚即可。而结果也只需要两把就可以了。

先对数列排序,然后筛选出相等元素,这样循环筛选,每筛选一次代表扫帚增一,直到无可筛选的元素。

 

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.

#include <iostream>

#include <queue>

#include <string>

using namespace std;
main()
{
      int num,i,j,h,n,p[3005];
      while(scanf("%d",&n)!=EOF)
      {
           for(i=0;i<n;i++)
           scanf("%d",&p[i]);
           sort(p,p+i);
           h=i;
           num=0;
           while(h)  //当筛选出0个元素时结束。
           {
                num++;j=0;
                for(i=0;i<h-1;i++)
                if(p[i]==p[i+1])
                {
                     p[j]=p[i+1];  //筛选出相等元素。
                     j++;
                }
                h=j;
           }
           printf("%d\n",num);
      }
}
          

posted @ 2011-03-30 18:51  CoderZhuang  阅读(103)  评论(0编辑  收藏  举报