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 ...
随机推荐
- Python:三元运算
result=值1 if 条件 else 值2 如果条件为真,result=值1 如果条件为假,result=值2 例子: a,b,c=1,3,5 d=a if a>b else c print ...
- HDU 5861 Road 线段树区间更新单点查询
题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5861 Road Time Limit: 12000/6000 MS (Java/Othe ...
- 使用git下载编译erlang
git clone https://github.com/erlang/otp cd otp git tag git checkout -b OTP- OTP- ./otp_build all exp ...
- WPF和Expression Blend开发实例:Adorner(装饰器)应用实例
装饰器-- 表示用于修饰 UIElement 的 FrameworkElement 的抽象类 简单来说就是,在不改变一个UIElement结构的情况下,将一个Visual对象加到它上面. 应用举例: ...
- Laravel 框架集成 UEditor 编辑器的方法
㈠. 背景 在项目开发的过程中,免不了使用修改功能,而富文本编辑器是极为方便的一种推荐,当然,个人认为 MarkDown 更为简单,但是感觉暂时只适合程序猿 此文介绍如何在 Laravel5.5 ...
- Web前端JQuery基础
JQuery知识汇总 一.关于Jquery简介 jQuery是一个快速.简洁的JavaScript框架,是继Prototype之后又一个优秀的JavaScript代码库(或JavaS ...
- Spring学习-1 框架总览
Spring 是一个开源框架,是为了解决企业应用程序开发复杂性而创建的.框架的主要优势之一就是其分层架构,分层架构允许您选择使用哪一个组件,同时为 J2EE 应用程序开发提供集成的框架. spring ...
- ROC曲线【转】
受试者工作特征曲线(receiver operating characteristic curve, 简称ROC曲线),又称为感受性曲线(sensitivity curve).得此名的原因在于曲线上各 ...
- Day21-自定义simple_tag/filter
一. 在模板里面对用户发过来的数据进行二次加工.想办法把Python的函数放到模板语言里面使用. 有2种方法:filter与simple_tag 二. 实例 {{name|lower}} trunca ...
- 【刷题】BZOJ 5249 [2018多省省队联测]IIIDX
Description [题目背景] Osu听过没?那是Konano最喜欢的一款音乐游戏,而他的梦想就是有一天自己也能做个独特酷炫的音乐游戏.现在,他在世界知名游戏公司KONMAI内工作,离他的梦想也 ...