加载中...

信息奥赛题解|Shopping


信息奥赛题解|Shopping


🚀 题目浏览

【题目描述】

Every girl likes shopping, so does dandelion. Now she finds the shop is increasing the price every day because the Spring Festival is coming. She is fond of a shop which is called “memory”. Now she wants to know the rank of this shop’s price after the change of everyday.

【输入】

Multiple test cases, please process to the End Of File.

One line contians a number $n ( n \le 10000)$, stands for the number of shops.

Then n lines, each line contains a string (the length is short than 31 and only contains lowercase letters and capital letters.) stands for the name of the shop.

Then a line contians a number $m (1 \le m \le 50)$, stands for the days .

Then m parts , every parts contians $n$ lines , each line contians a number $s$ and a string $p$ , stands for this day ,the shop p’s price has increased $s$.

【输出】

Contains $m$ lines ,In the ith line print a number of the shop “memory” ‘s rank after the ith day.

We define the rank as :If there are t shops’ price is higher than the “memory” , than its rank is $t+1$.

【输入样例】

3
memory
kfc
wind
2
49 memory
49 kfc
48 wind
80 kfc
85 wind
83 memory

【输出样例】

1
2

【原题链接】

https://vjudge.net/problem/HDU-2648


☘️ 题解分析

C++ STL容器 map 的经典使用


🧑🏻‍💻 C++ 代码

#include<bits/stdc++.h>

using namespace std;
typedef long long LL;
int n, m, p;
string s;

int main() {
    ios::sync_with_stdio(false);  //cin读入优化
    cin.tie(0);

    while (cin >> n) {
        // 读入店铺
        map<string, int> mp;
        for (int i = 1; i <= n; i++) {
            cin >> s;
            mp[s] = 0;
        }

        // m天数据
        cin >> m;
        for (int i = 1; i <= m; i++) {
            // 读入一天的数据
            for (int j = 1; j <= n; j++) {
                cin >> p >> s;
                mp[s] += p;
            }
            // 输出当前排名
            int cnt = 1;
            for (auto t: mp) {
                if (t.second > mp["memory"]) cnt++;
            }
            cout << cnt << endl;
        }
    }

    return 0;
}

文章作者: Rickyの水果摊
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Rickyの水果摊 !
  目录