Hrbust 1456 Matryoshka Dolls【思维】

本文探讨了一道经典的俄罗斯套娃问题,通过最优组装减少外露套娃数量的方法。利用最小路径覆盖思想,结合高效算法解决大规模数据集问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Matryoshka Dolls
Time Limit: 1000 MSMemory Limit: 65536 K
Total Submit: 16(13 users)Total Accepted: 11(11 users)Rating: Special Judge: No
Description

Adam just got a box full of Matryoshka dolls (Russian traditional) from his friend Matryona. When he opened the box, tons of dolls poured out of the box with a memo from her:

Hi Adam, I hope you enjoy these dolls. But sorry, I didn't have enough time to sort them out. You'll notice that each doll has a hollow hole at the bottom which allows it to contain a smaller doll inside.

...

Yours,

Matryona

Adam, who already has so many things in his showcase already, decides to assemble the dolls to reduce the number of outermost dolls. The dolls that Matryona sent have the same shape but di erent sizes. That

is, doll i can be represented by a single number hi denoting its height. Doll i can  t inside another doll j if and only if hi < hj . Also, the dolls are designed such that each doll may contain at most one doll right

inside it. While Adam is stacking his gigantic collection of Matryoshka dolls, can you write a program to compute the minimum number of outermost dolls so that he can  gure out how much space he needs in his showcase?

Input

The input consists of multiple test cases. Each test case begins with a line with an integer N, 1 <= N <= 10^5,

denoting the number of Matryoshka dolls. This is followed by N lines, each with a single integer hi, 1 <=  hi <= 10^9, denoting the height of the ith doll in cm. Input is followed by a single line with N = 0, which should not be processed. 

For example:

4

5

7

7

3

3

10

10

10

3

10

999999999

100000000

0

Output

For each test case, print out a single line that contains an integer representing the minimum number of outermost dolls that can be obtained by optimally stacking the given dolls. 

For example:

2

3

1

Sample Input

4

5

7

7

3

3

10

10

10

3

10

999999999

100000000

0

Sample Output

2

3

1

Source
2010 Stanford Local ACM Programming Contest

题目大意:


一共有N个娃娃,如果满足:hi<hj的两个娃娃i,j,i就可以被j包含进去(但是j只能包含一个娃娃),问最少露在外边的数量是多少个,。


思路:


1、很显然的最小路径覆盖问题,然而N很大,显然跑最大匹配==最小路径覆盖数是肯定要TLE的.


2、那么考虑思维,对于露在外边最少的数量,其实就是在问最多出现的大小有多少个。

那么接下来我们只要map计数一波即可。


Ac代码:


#include<stdio.h>
#include<string.h>
#include<map>
using namespace std;
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        if(n==0)break;
        map<int ,int >s;
        int ans=0;
        int maxn=-0x3f3f3f3f;
        for(int i=1;i<=n;i++)
        {
            int x;
            scanf("%d",&x);
            s[x]++;
            ans=max(s[x],ans);
        }
        printf("%d\n",ans);
    }
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值