zoukankan      html  css  js  c++  java
  • HDU 1800 Flying to the Mars

    Flying to the Mars

    Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 22122    Accepted Submission(s): 7104


    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
     
    Recommend
    lcy   |   We have carefully selected several similar problems for you:  1075 1671 1052 1251 1050 
     

    分析:

    对于给定的士兵级别都不相同,那么只需要一把扫帚即可。这样,问题就转化成了找出给出的数里重复的次数,重复次数最多的数的个数即为所求。将每个数当做字符串插入trie树,记录每个数出现的次数。最后找出重复出现次数的最大值即为所求。注意044都表示4,我们在插入时不将0插入。

     

     1 #include <iostream>
     2 #include <string>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <cstdio>
     6 using namespace std;
     7 typedef struct nn
     8 {
     9     int num;
    10     struct nn *nxt[10];
    11 }node;
    12 
    13 node *builde()
    14 {
    15     node *p = (node*)malloc(sizeof(node));
    16     p->num = 0; 
    17     for (int i = 0; i<10; i++)
    18         p->nxt[i] = NULL;
    19     return p;
    20 }
    21 node *root;
    22 
    23 int insert(char s[])
    24 {
    25     node *p = root;
    26     int i = 0;
    27     while (s[i] == '0') i++;
    28     while (s[i] != '')
    29     {
    30         if (p->nxt[s[i] - '0'] == NULL)
    31             p->nxt[s[i] - '0'] = builde();
    32         p = p->nxt[s[i] - '0'];
    33         i++;
    34     }
    35     p->num++;
    36     return p->num;
    37 }
    38 
    39 int main()
    40 {
    41     int n, max;
    42     char c[35];
    43     while (cin >> n)
    44     {
    45         max = 0;
    46         root = builde();
    47         while (n--)
    48         {
    49             cin >> c;
    50             int t = insert(c);
    51             if (t > max) max = t;
    52         }
    53         if (max == 0) max = 1;
    54         printf("%d
    ", max);
    55     }
    56     return 0;
    57 }

    也可以用map更方便

     1 #include <iostream>
     2 #include <cstring>
     3 #include <string>
     4 #include <algorithm>
     5 #include <map>
     6 using namespace std;
     7 int main()
     8 {
     9     int n;
    10     int level;
    11     while (cin >> n)
    12     {
    13         map<int, int>mp;
    14         int max = 0;
    15         for (int i = 0; i <n; i++)
    16         {
    17             scanf("%d", &level);
    18             mp[level]++;
    19             if (mp[level]>max)
    20                 max = mp[level];
    21         }
    22         printf("%d
    ", max);
    23     }
    24     return 0;
    25 }
    View Code
  • 相关阅读:
    AC日记——接苹果 洛谷 P2690
    AC日记——友好城市 洛谷 P2782
    AC日记——栈 洛谷 P1044
    AC日记——L国的战斗之间谍 洛谷 P1916
    AC日记——[USACO1.1]坏掉的项链Broken Necklace 洛谷 P1203
    AC日记——[USACO1.5]数字三角形 Number Triangles 洛谷 P1216
    Codevs 1048 石子归并
    Codevs 1138 聪明的质监员 2011年NOIP全国联赛提高组
    HDU 1575 Tr A
    Codevs 5059 一起去打CS
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/13271205.html
Copyright © 2011-2022 走看看