UVa 11991 - Easy Problem from Rujia Liu?

本文介绍如何利用STL中的map进行数据离散化处理,以解决数值范围过大而无法使用传统数组标记的问题。通过两个示例展示了map在不同场景下的应用。

题目链接:UVa 11991 - Easy Problem from Rujia Liu?

map离散化。

先来看这样一个问题。

假如有m个数(m较小),但是每个数的取值范围很大(0<=ai<=1000000000)。我想记录哪些数字出现过,应该怎么离散呢?我知道最基本的作法是开一个标记的数组,哪个数字出现,就让那个数字对应的下标数组变成1,但是范围太大,没可能开1000000000的数组来存放标记,那要怎么办?这里就应该是用STL中的map。把ai当成key,value随便都行,那么查询是否有这个数的时候使用map的函数count(key)就可以了。

#include <iostream>
#include <map>
#include <vector>

using namespace std;

map <int,int> _map;
int n,m;

int main()
{
    while(cin >> n >> m)
    {
        int temp,a,b;
        _map.clear();
        for(int i = 1;i <= n;i++)
        {
            cin >> temp;
            _map[temp] = 1;//随便给一个值就行
        }
        for(int i = 1;i <= m;i++)
        {
            cin >> b;
            if(!_map.count(b))
                cout << 0 << endl;
            else
                cout << _map[b] << endl;
        }

    }
    return 0;
}


现在看看这道题,其实是很类似的,直接用一个数组搜过去估计得超时,那么整一个二维数组,第一维记录key(也就是题目给的值),第二维可以记录下标,速度倒是有了,但是这个数组太大了。那么可以想到使用map。

#include <iostream>
#include <map>
#include <vector>

using namespace std;

map <int,vector<int> > _map;
int n,m;

int main()
{
    while(cin >> n >> m)
    {
        int temp,a,b;
        _map.clear();
        for(int i = 1;i <= n;i++)
        {
            cin >> temp;
            //if(!_map.count(temp))
                //_map[temp] = vector<int>();
            _map[temp].push_back(i);
        }
        for(int i = 1;i <= m;i++)
        {
            cin >> a >> b;
            if(!_map.count(b) || _map[b].size() < a)
                cout << 0 << endl;
            else
                cout << _map[b][a - 1] << endl;
        }

    }
    return 0;
}


请帮我写一份c++代码,解决一下问题,要求符合输入输出样本There are n people (excluding myself) in my 30th birthday party. They sing the traditional “happy birthday” song: Happy birthday to you! Happy birthday to you! Happy birthday to Rujia! Happy birthday to you!!! Since I love music, I want to hear something more interesting, not that everyone sings together. Ah yes, I want one person to sing one word! For example, there are three people: Mom, Dad, Girlfriend, I’d like them to sing like this: Mom: Happy Dad: birthday Girlfriend: to Mom: you Dad: Happy Girlfriend: birthday Mom: to Dad: you Girlfriend: Happy Mom: birthday Dad: to Girlfriend: Rujia Mom: Happy Dad: birthday Girlfriend: to Mom: you Very nice, right? What if there are more than 16 people? That’s easy: repeat the song until everyone has sung at least once :) Please, don’t stop in the middle of the song.Input There is only one test case. The first line contains a single integer n (1 ≤ n ≤ 100). Then each of the next n lines contains a capitalized name (i.e. one upper-case letter followed by zero or more lowercase letters). Each name contains at most 100 characters and do not have whitespace characters inside. Output Output the song, formatted as above. Sample Input 3 Mom Dad Girlfriend Sample Output Mom: Happy Dad: birthday Girlfriend: to Mom: you Dad: Happy Girlfriend: birthday Mom: to Dad: you Girlfriend: Happy Mom: birthday Dad: to Girlfriend: Rujia Mom: Happy Dad: birthday Girlfriend: to Mom: you
最新发布
12-06
以下是实现生日派对上每人依次唱“生日快乐”歌一个单词的C++代码: ```cpp #include <iostream> #include <vector> #include <string> int main() { int n; std::cin >> n; std::cin.ignore(); // 忽略换行符 std::vector<std::string> names(n); for (int i = 0; i < n; ++i) { std::getline(std::cin, names[i]); } std::vector<std::string> song = { "Happy", "birthday", "to", "you", "Happy", "birthday", "to", "you", "Happy", "birthday", "to", "Rujia", "Happy", "birthday", "to", "you" }; int personIndex = 0; int songIndex = 0; while (personIndex < n || songIndex < song.size()) { if (personIndex >= n) { personIndex = 0; } if (songIndex >= song.size()) { songIndex = 0; } std::cout << names[personIndex] << ": " << song[songIndex] << std::endl; personIndex++; songIndex++; } return 0; } ``` ### 代码说明 1. **输入处理**:首先读取人数`n`,然后忽略换行符,接着读取`n`个人的名字并存储在`names`向量中。 2. **歌词存储**:将“生日快乐”歌的歌词按单词存储在`song`向量中。 3. **演唱循环**:使用两个索引`personIndex`和`songIndex`分别遍历人名和歌词。当一个遍历完后,将其索引重置为0,继续循环,直到所有人都至少唱了一次且不中断歌曲。 ### 输入输出示例 **输入**: ``` 3 Mom Dad Girlfriend ``` **输出**: ``` Mom: Happy Dad: birthday Girlfriend: to Mom: you Dad: Happy Girlfriend: birthday Mom: to Dad: you Girlfriend: Happy Mom: birthday Dad: to Girlfriend: Rujia Mom: Happy Dad: birthday Girlfriend: to Mom: you ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值