hdu 1800 flying to the mars(贪心算法)

来源:互联网 发布:淘宝号被冻结解冻方法 编辑:程序博客网 时间:2024/05/18 13:29

问题描述:

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 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);

输出:

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

样本输入:

4

10 20 30 04

5

2 3 4 3 4


样本输出:

1

2

问题分析:

按理说这里的数据非常大,应该转换成字符串的形式来考虑,可是我对这方面接触还比较少,是用整形做的 但是确实感觉到对该题的处理欠妥当,另外提几个注意地方:前导0的处理,1和01和001都是同一个数字(字符串处理,不是整型,所以必须处理前导0)。如果不处理,则需要1把扫帚,处理后才是3把扫帚。

代码:

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
using namespace std;
int cmp(const void *a,const void *b)
{
return *(int *)b-*(int *)a;
}
int main()
{
    int n,i;
    while(cin>>n)
    {
        int s[3002]={0};//s数组存放的是存进去的数
        for(i=0;i<n;i++)
        {
            scanf("%d",&s[i]);//注意一下就是输入时用scanf输入,对大数据才不会出错,如果是cin就会报错
        }
qsort(s,n,sizeof(s[0]),cmp);//对数据进行排序
int max=1,num=1;//max是存放出现次数最多的 num也是表示出现次数的,但注意一下对它的初始化
for(i=1;i<n;i++)
{
if(s[i]==s[i-1])
{
num++;
if(max<num)
max=num;
}
else
{
num=1;//也就是当该值不与前面的值相等时 从这个数开始再找,num初始化为1
}
}
        cout<<max<<endl;
    }
    return 0;
}


0 0
原创粉丝点击