“玲珑杯”ACM比赛 Round #5 B -- Private Value【STL-map】

本文介绍了一种求解私有值问题的算法,即在一个整数数组中找到出现频率最低的元素,并按升序输出这些元素。使用C++ map进行高效计数,通过迭代找出最小频次并输出所有符合条件的元素。

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

B -- Private Value

Time Limit:1s Memory Limit:128MByte

Submissions:526Solved:174

DESCRIPTION

The "private value" is the value that occurs, but most infrequent.

An array of integers is given, and you need print the private values of the array.

Please notice that, there might be several private values, and you need print them in increasing order.

INPUT
The first line is an integer T(1 <= T <= 4), indicating the number of test cases.For each case, the first line contains an integer n (n <= 100000).The second line contains n integers a_i (0 <= a_i <= 10^9),
OUTPUT
For each case, print all private values in increasing order in a line.
SAMPLE INPUT
2
7
1 1 2 2 3 3 3
5
5 3 2 4 1
SAMPLE OUTPUT
1 2
1 2 3 4 5

原题链接:http://www.ifrog.cc/acm/problem/1057

题意:输出一个序列中出现次数最少的元素,数组可能会超时,类似于桶排序的方法估计也会超时,我用map就不用考虑那么多了。

AC代码:

/**
* 行有余力,则来刷题!
  * 博客链接:http://blog.youkuaiyun.com/hurmishine
  *
*/
#include <iostream>
#include <map>
using namespace std;
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        int n;
        cin>>n;
        map<int,int>mp;
        mp.clear();
        int x;

        for(int i=0; i<n; i++)
        {
            cin>>x;
            mp[x]++;
        }
        map<int,int>::iterator  iter;
        int minn=1e9;
        for(iter=mp.begin(); iter!=mp.end(); iter++)
        {
            if(iter->second<minn)
                minn=iter->second;
        }
        bool first=true;
        for(iter=mp.begin(); iter!=mp.end(); iter++)
        {
            if(iter->second==minn)
            {
                if(!first)
                    cout<<" "<<iter->first;
                else
                {
                    first=false;
                    cout<<iter->first;
                }
            }
        }
        cout<<endl;
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值