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 ...
随机推荐
- DL开源框架Caffe | 模型微调 (finetune)的场景、问题、技巧以及解决方案
转自:http://blog.csdn.net/u010402786/article/details/70141261 前言 什么是模型的微调? 使用别人训练好的网络模型进行训练,前提是必须和别人 ...
- erlang init:stop()不起效
http://blog.equanimity.nl/blog/2015/03/15/erlang-one-weird-trick-goodiebag/ http://erlang.org/piperm ...
- 复利计算C转java版
import java.util.Scanner; public class Compound_int { public static void main(String[] args) { tip() ...
- 蜗牛慢慢爬 LeetCode 1.Two Sum [Difficulty: Easy]
题目 Given an array of integers, return indices of the two numbers such that they add up to a specific ...
- 初学Objective - C语法之代码块(block)
一.block声明 1.无参数,无返回值: void (^sayHi)(); 2.有参数,有返回值: NSInteger (^operateOfValue)(NSInteger num); block ...
- 太平洋网络ip地址查询接口使用,返回json格式,默认返回jsonp
http://whois.pconline.com.cn/ipJson.jsp?json=true
- Kafka及Spring Cloud Stream
安装 下载kafka http://mirrors.hust.edu.cn/apache/kafka/2.0.0/kafka_2.11-2.0.0.tgz kafka最为重要三个配置依次为:broke ...
- P2766 最长不下降子序列问题
题目描述 «问题描述: 给定正整数序列x1,...,xn . (1)计算其最长不下降子序列的长度s. (2)计算从给定的序列中最多可取出多少个长度为s的不下降子序列. (3)如果允许在取出的序列中多次 ...
- VSS2005设置不输入密码直接登录VSS
1.登录管理员 2.Tools-->Options-->General -->Use network name for automatic user log in 去掉勾选不自动登 ...
- 【刷题】BZOJ 2434 [Noi2011]阿狸的打字机
Description 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母. 经阿狸研究发现,这个打字机是这样工作的 ...