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:

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstring> using namespace std; const int maxn = ;
struct Level
{
char l[];
int len;
}a[maxn];
int n; bool cmp(const Level& x, const Level& y)
{
if(x.len != y.len) return x.len < y.len;
for(int i = ; i >= - x.len; i--)
{
if(x.l[i] > y.l[i]) return false;
return true;
}
} int main()
{
char c;
while(scanf("%d%c", &n, &c) != EOF)
{
for(int i = ; i < n; i++)
{
memset(a[i].l, '', sizeof(a[i].l));
a[i].l[] = '\0';
a[i].len = ;
for(int j = ; scanf("%c", &c) && c != '\n';)
{
if(j == && c == '') continue;
a[i].l[j] = c;
a[i].len++;
j--;
}
}
sort(a, a + n, cmp);
int max = -;
for(int i = ; i < n; )
{
int j;
for(j = i + ; j < n; j++)
{
if(strcmp(a[i].l, a[j].l)) break;
}
if(max < j - i) max = j - i;
i = j;
}
printf("%d\n", max);
}
return ;
}

方法二:

 #include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstring> using namespace std; const int MOD = ;
const int MAXN = ;
const int LEN = ;
const int seed[] = {, };
int level[MAXN];
char s[LEN]; int Hash()
{
int res = ;
int i;
for(i = ; s[i] == ''; i++){}
for(; s[i] != '\0'; i++)
{
res += ((res * seed[s[i]&] + s[i]) % MOD);
}
return res;
} int main()
{
int n;
while(scanf("%d", &n) != EOF)
{
for(int i = ; i < n; i++)
{
scanf("%s", s);
level[i] = Hash();
}
sort(level, level + n);
int max = ;
for(int i = ; i < n;)
{
int j;
for(j = i + ; j < n && level[j] == level[i]; j++){}
if(max < j - i)
max = j - i;
i = j;
}
printf("%d\n", max);
}
return ;
}

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. 随机生成30道四则运算-NEW

    补充:紧跟上一个随机生成30道四则运算的题目,做了一点补充,可以有真分数之间的运算,于是需要在原来的基础上做一些改进. 首先指出上一个程序中的几个不足:1.每次执行的结果都一样,所以不能每天给孩子出3 ...

  2. 软件工程 speedsnail 第二次冲刺10

    20150527 完成任务:蜗牛碰到线后速度方向的调整:已经基本实现多方向的反射: 遇到问题: 问题1 反射角的问题 解决1 利用tan()三角函数 明日任务: 大总结.找到新问题.布置下一次冲刺方案

  3. 引用百度bcebos jar 503问题

    最近使用jeecms管理公司各个站点,关于附件部分采用bos进行上传处理. 在引用bosjar的时候,出现503问题,打断点打印异常堆栈信息,也获取不到.后来使用watch,发现BosClientCo ...

  4. 01.1 Windows环境下JDK安装与环境变量配置详细的图文教程

    01.1 Windows环境下JDK安装与环境变量配置详细的图文教程 本节内容:JDK安装与环境变量配置 以下是详细步骤 一.准备工具: 1.JDK JDK 可以到官网下载 http://www.or ...

  5. 201621123037 《Java程序设计》第3周学习总结

    #Week03-面向对象入门 1. 本周学习总结 初学面向对象,会学习到很多碎片化的概念与知识.尝试学会使用思维导图将这些碎片化的概念.知识点组织起来.请使用工具画出本周学习到的知识点及知识点之间的联 ...

  6. Linux的压缩/解压缩文件处理 zip & unzip

    Linux的压缩/解压缩命令详解及实例 压缩服务器上当前目录的内容为xxx.zip文件 zip -r xxx.zip ./* 解压zip文件到当前目录 unzip filename.zip 另:有些服 ...

  7. iOS 关于MVC和MVVM设计模式的那些事

    一.概述 在 iOS 开发中,MVC(Model View Controller)是构建iOS App的标准模式,是苹果推荐的一个用来组织代码的权威范式.Apple甚至是这么说的.在MVC下,所有的对 ...

  8. 【ASP.NET Core 】ASP.NET Core 源码学习之 Logging[1]:Introduction

    在ASP.NET 4.X中,我们通常使用 log4net, NLog 等来记录日志,但是当我们引用的一些第三方类库使用不同的日志框架时,就比较混乱了.而在 ASP.Net Core 中内置了日志系统, ...

  9. 【uoj#225】[UR #15]奥林匹克五子棋 构造

    题目描述 两个人在 $n\times m$ 的棋盘上下 $k$ 子棋,问:是否存在一种平局的情况?如果存在则输出一种可能的最终情况. 输入 第一行三个正整数 $n,m,k$ ,意义如前所述. 输出 如 ...

  10. TortoiseSVN的基本使用方法

    TotoiseSVN的基本使用方法 来源 https://blog.csdn.net/hecongzhen/article/details/37879801 在 项目管理实践教程一.工欲善其事,必先利 ...