D - Flying to the Mars

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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
 

思路:统计输入的数的众数,小于众数的数是众数的学生,大于众数的数是众数的老师,故而众数就是答案。

输入的数不多于30digits,64位整数存放不下,32位更不行,但是很多人用32位或者64位做出来了,原因应该不是数据太弱,而是就算溢出也不影响众数的统计。以下程序用的是大数。另一程序用的是字符串哈希,让字符串映射一个整数。

AC Code:

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <queue>
  5. #include <map>
  6. #include <string>
  7. #include <cstdio>
  8. #include <cmath>
  9. #include <cstring>
  10.  
  11. using namespace std;
  12.  
  13. const int maxn = ;
  14. struct Level
  15. {
  16. char l[];
  17. int len;
  18. }a[maxn];
  19. int n;
  20.  
  21. bool cmp(const Level& x, const Level& y)
  22. {
  23. if(x.len != y.len) return x.len < y.len;
  24. for(int i = ; i >= - x.len; i--)
  25. {
  26. if(x.l[i] > y.l[i]) return false;
  27. return true;
  28. }
  29. }
  30.  
  31. int main()
  32. {
  33. char c;
  34. while(scanf("%d%c", &n, &c) != EOF)
  35. {
  36. for(int i = ; i < n; i++)
  37. {
  38. memset(a[i].l, '', sizeof(a[i].l));
  39. a[i].l[] = '\0';
  40. a[i].len = ;
  41. for(int j = ; scanf("%c", &c) && c != '\n';)
  42. {
  43. if(j == && c == '') continue;
  44. a[i].l[j] = c;
  45. a[i].len++;
  46. j--;
  47. }
  48. }
  49. sort(a, a + n, cmp);
  50. int max = -;
  51. for(int i = ; i < n; )
  52. {
  53. int j;
  54. for(j = i + ; j < n; j++)
  55. {
  56. if(strcmp(a[i].l, a[j].l)) break;
  57. }
  58. if(max < j - i) max = j - i;
  59. i = j;
  60. }
  61. printf("%d\n", max);
  62. }
  63. return ;
  64. }

方法二:

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <queue>
  5. #include <map>
  6. #include <string>
  7. #include <cstdio>
  8. #include <cmath>
  9. #include <cstring>
  10.  
  11. using namespace std;
  12.  
  13. const int MOD = ;
  14. const int MAXN = ;
  15. const int LEN = ;
  16. const int seed[] = {, };
  17. int level[MAXN];
  18. char s[LEN];
  19.  
  20. int Hash()
  21. {
  22. int res = ;
  23. int i;
  24. for(i = ; s[i] == ''; i++){}
  25. for(; s[i] != '\0'; i++)
  26. {
  27. res += ((res * seed[s[i]&] + s[i]) % MOD);
  28. }
  29. return res;
  30. }
  31.  
  32. int main()
  33. {
  34. int n;
  35. while(scanf("%d", &n) != EOF)
  36. {
  37. for(int i = ; i < n; i++)
  38. {
  39. scanf("%s", s);
  40. level[i] = Hash();
  41. }
  42. sort(level, level + n);
  43. int max = ;
  44. for(int i = ; i < n;)
  45. {
  46. int j;
  47. for(j = i + ; j < n && level[j] == level[i]; j++){}
  48. if(max < j - i)
  49. max = j - i;
  50. i = j;
  51. }
  52. printf("%d\n", max);
  53. }
  54. return ;
  55. }

Flying to the Mars的更多相关文章

  1. hdu---(1800)Flying to the Mars(trie树)

    Flying to the Mars Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  2. hdu 1800 Flying to the Mars

    Flying to the Mars 题意:找出题给的最少的递增序列(严格递增)的个数,其中序列中每个数字不多于30位:序列长度不长于3000: input: 4 (n) 10 20 30 04 ou ...

  3. (贪心 map) Flying to the Mars hdu1800

    Flying to the Mars Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  4. 杭电 1800 Flying to the Mars(贪心)

    http://acm.hdu.edu.cn/showproblem.php?pid=1800 Flying to the Mars Time Limit: 5000/1000 MS (Java/Oth ...

  5. HDOJ.1800 Flying to the Mars(贪心+map)

    Flying to the Mars 点我挑战题目 题意分析 有n个人,每个人都有一定的等级,高等级的人可以教低等级的人骑扫帚,并且他们可以共用一个扫帚,问至少需要几个扫帚. 这道题与最少拦截系统有异 ...

  6. HDU 1800——Flying to the Mars——————【字符串哈希】

    Flying to the Mars Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  7. HDU1800 Flying to the Mars 【贪心】

    Flying to the Mars Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  8. --hdu 1800 Flying to the Mars(贪心)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1800 Ac code: #include<stdio.h> #include<std ...

  9. hdu 1800 Flying to the Mars(简单模拟,string,字符串)

    题目 又来了string的基本用法 //less than 30 digits //等级长度甚至是超过了int64,所以要用字符串来模拟,然后注意去掉前导零 //最多重复的个数就是答案 //关于str ...

随机推荐

  1. Java中static关键字的作用和用法详细介绍

    static表示“全局”或者“静态”的意思,用来修饰成员变量和成员方法,也可以形成静态static代码块,但是Java语言中没有全局变量的概念. 被static修饰的成员变量和成员方法独立于该类的任何 ...

  2. 将 Spring 和 Hibernate 与 WebSphere Application Server 一起使用

    本文摘要 如果您考虑将 Spring 或 Hibernate 与 IBM® WebSphere® Application Server 一起使用,则本文向您阐述了如何配置这些框架,以适用于 WebSp ...

  3. 1 RabbitMQ 安装,配置

    1:安装 yum install -y rabbitmq-server   2:主要程序介绍 # 管理插件的程序 /usr/sbin/rabbitmq-plugins # 服务程序 /usr/sbin ...

  4. jQuery之基本选择器

    1. 是什么? - 有特定格式的字符串2. 作用 - 用来查找特定页面元素3. 基本选择器 - #id : id选择器 - element : 元素选择器 - .class : 属性选择器 - * : ...

  5. Java String简单知识点总结

    1.字符串的比较 public void run(){ //str1在池中 String str1 = new String("String"); //str2,str3 存在于堆 ...

  6. 关于jquery中on绑定click事件在苹果手机失效的问题(巨坑啊)

    用一个div当做了一个按钮来使用. <div class="button"> <div class=" next_button button_left ...

  7. Struts2文件的上传和下载实现

    <一>简述: Struts2的文件上传其实也是通过拦截器来实现的,只是该拦截器定义为默认拦截器了,所以不用自己去手工配置,<interceptor name="fileUp ...

  8. Java 调用 google 翻译

    1.Java代码 public class Translator { public String translate(String langFrom, String langTo, String wo ...

  9. CSS中可以和不可以继承的属性【转】

    一.无继承性的属性 1.display:规定元素应该生成的框的类型 2.文本属性: vertical-align:垂直文本对齐 text-decoration:规定添加到文本的装饰 text-shad ...

  10. angular 延迟更新方法

    失去焦点后更新: <input ng-model="name" ng-model-options="{updateOn:'blur'}" />{{n ...