hdu 1800 - Flying to the Mars

来源:互联网 发布:java电信项目 编辑:程序博客网 时间:2024/06/03 13:50

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1800

题意:

有n个士兵每个人有一个能力值d,现在士兵要去学习如何飞到火星。规定如下,能力值大的可以教能力值小的,

每个人只能够教一个人。规定一起学习的人的书本是一样的,问最少需要几本书?

思路:

根据题目的意思是我们可以知道最终要求的就是有几个递减的序列,也就是找到最多重复的值。比如2 3 4 3 4 就是两个递减序列4 3 2 和 4 3。重复的数有3,4;即输出2个。

这道题就变成寻找一组数中重复出现的数的个数。

#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include<stack>using namespace std;int a[3010];int main(){    int n;    while(~scanf("%d",&n))    {        memset(a,0,sizeof(a));        int i,j;        for(i=0;i<n;i++)        {            scanf("%d",&a[i]);        }        sort(a,a+n);        int count=1;        int MAX=1;        for(i=1;i<n;i++)        {                            if(a[i]>a[i-1])            {                count=1;                            }            else            {                count++;                if(count>MAX)                {                    MAX=count;                 }            }                }        printf("%d\n",MAX);            }    return 0;}


 

 

0 0
原创粉丝点击