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
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
Sample Input
10
20
30
04
5
2
3
4
3
4
Sample Output
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的更多相关文章
- 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 ...
- hdu 1800 Flying to the Mars
Flying to the Mars 题意:找出题给的最少的递增序列(严格递增)的个数,其中序列中每个数字不多于30位:序列长度不长于3000: input: 4 (n) 10 20 30 04 ou ...
- (贪心 map) Flying to the Mars hdu1800
Flying to the Mars Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- 杭电 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 ...
- HDOJ.1800 Flying to the Mars(贪心+map)
Flying to the Mars 点我挑战题目 题意分析 有n个人,每个人都有一定的等级,高等级的人可以教低等级的人骑扫帚,并且他们可以共用一个扫帚,问至少需要几个扫帚. 这道题与最少拦截系统有异 ...
- HDU 1800——Flying to the Mars——————【字符串哈希】
Flying to the Mars Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- HDU1800 Flying to the Mars 【贪心】
Flying to the Mars Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- --hdu 1800 Flying to the Mars(贪心)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1800 Ac code: #include<stdio.h> #include<std ...
- hdu 1800 Flying to the Mars(简单模拟,string,字符串)
题目 又来了string的基本用法 //less than 30 digits //等级长度甚至是超过了int64,所以要用字符串来模拟,然后注意去掉前导零 //最多重复的个数就是答案 //关于str ...
随机推荐
- 阿帕奇web服务器下载部署安装运行
链接: https://jingyan.baidu.com/album/d8072ac47baf0eec95cefdca.html?picindex=4 1.apache服务安装成功可是启动失败“wi ...
- Alpha 冲刺(8/10)
队名 火箭少男100 组长博客 林燊大哥 作业博客 Alpha 冲鸭鸭鸭鸭鸭鸭鸭鸭! 成员冲刺阶段情况 林燊(组长) 过去两天完成了哪些任务 协调各成员之间的工作 多次测试软件运行 学习OPENMP ...
- openssl 基本加密
openssl命令行工具详解(openssl的命令众多,请酌情处理与记忆) 在命令行输入:openssl asdf,可以显示openssl的命令说明 1:Standard commands(标准命 ...
- ubuntu通过apt-get方式搭建lnmp环境以及php扩展安装
v 一直是在用的lnmp的集成安装包搭建lnmp环境,因为工作需要需要安装ldap扩展,在网上怎么都找不到源码安装包,只能卸载掉原来的lnmp环境,用ubuntu的php5-ldap扩展, 在安装中遇 ...
- c语言----程序记录
1.结构体写入文件,读取 #include <stdio.h> #include <string.h> #include <stdlib.h> #define ma ...
- arp_filter的验证,使用net namespace
使用网络命名空间:net namespace 在namespace ns1中增加了两个网卡 sudo ip netns add ns1 sudo ip link add veth0 type veth ...
- Java Servlet简介
一.了解Servlet的概念 Servlet定义 Servlet是基于Java技术的Web组件,由容器管理并产生动态的内容.Servlet引擎作为WEB服务器的扩展提供支持Servlet的功能.Se ...
- Ubuntu的IP地址配置
概况和需求: 我的主机上有两块网卡,识别后分别是eth0和eth1.eth0配置需要为静态ip,eth1配置为使用动态主机协议获取ip地址. 步骤: 首先碰到的一个问题就是不知道eth0和eth1对应 ...
- webpack打包css样式出错
有两个组件home和search 两个组件中都有class为footer的元素 但是search的footer比home的多一条background的样式 本地开发的时候没问题,但是打包之后,home ...
- 【刷题】洛谷 P3806【模板】点分治1
题目背景 感谢hzwer的点分治互测. 题目描述 给定一棵有n个点的树 询问树上距离为k的点对是否存在. 输入输出格式 输入格式: n,m 接下来n-1条边a,b,c描述a到b有一条长度为c的路径 接 ...