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

题目描述

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.

思路

这题不是很难,贪心算法,求出最大连续的数的个数就是答案了。

坑在数据范围,士兵等级数长度小于30位。。。我以为是小于30级。。然后就一直异常访问。

这题会卡cin/cout,需要写一个读取函数,来存放大数据,配合map。

AC代码

/*************************************************************************
    > File Name: 1800.cpp
    > Author: Netcan
    > Mail: 1469709759@qq.com 
    > Created Time: 2015/6/19 16:28:19
 ************************************************************************/

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<map>
using namespace std;

void load(char *num) { // 读取大数据,存到数组num中
    char c;
    int i = 0;
    bool zero = true; // 是否前导零
    while((c=getchar())!='\n') {
        if(c!='0') // 不是
            zero = false;
        if(!zero) num[i++] = c; // 读取每一位
    }
    if(i==0) // 如果输入只有一个0的话
        num[i++] = '0';
    num[i] = 0; // 字符串结尾'\0'
}

int main() {
    int N;
    map<string, int> level; // 存放每个等级对应的次数
    char num[32]; // 存放每次读入的数据
    while(scanf("%d", &N) == 1) {
        int ans=0;
        getchar();
        for(int i=0; i<N; ++i)  {
            load(num); // 读取
            string Num(num); // 转换成string对象
            ++level[Num]; // 记录次数
            ans = max(level[Num], ans); // ans为最大连续数的出现次数
        }
        printf("%d\n", ans);
        level.clear(); // 记得清空
    }
    return 0;
}