Shopping

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6348    Accepted Submission(s): 2215

Problem Description
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.
 
Input
One line contians a number n ( n<=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<=m<=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.
 
Output
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.
 
Sample Input
3
memory
kfc
wind
2
49 memory
49 kfc
48 wind
80 kfc
85 wind
83 memory
 
Sample Output
1
2
 
理解题意:
这个题的题意很明确,让输出“memory”这个商店每天的价格排名
 
解题思路:
首先,map是关联容器,他可以实现从键值到值的映射,类似函数中的一一对应;然后我们定义一个map<string,int>shop;用来存储商店的名字和每天的价格;注意给每个商店赋值价格的操作是shop[s] += p;但是如果用下面这种方法进行排名的话,时间就会超限

            int rank = ;
map<string,int>::iterator it;
for(it = shop.begin();it != shop.end(); it++){
if(it->second>shop["memory"])
rank++;
}
cout<<rank<<endl;

所以我经过查阅资料发现了另一种方法,用一个数组来把价格存起来,然后用sort排序后,再通过比较数组中的值是否和“memory”商店的价格相等来输出他的位置,即为他的排名

#include <iostream>
#include <string>
#include <map>
#include <algorithm> using namespace std; bool my_camp(int x,int y){
return x>y;
} int main()
{
int n,m,p;
int a[];
map<string,int>shop;
while(cin>>n){
string s;
for(int i = ;i <= n; i++)
cin>>s;
cin>>m;
while(m--){
for(int i = ;i <= n; i++){
cin>>p>>s;
shop[s] += p;
a[i] = shop[s];
}
/*int rank = 1;
map<string,int>::iterator it;
for(it = shop.begin();it != shop.end(); it++){
if(it->second>shop["memory"])
rank++;
}
cout<<rank<<endl; */
sort(a+,a+n+,my_camp);
for(int i = ;i <= n; i++){
if(a[i] == shop["memory"]){
cout<<i<<endl;
break;
}
}
}
shop.clear();
}
return ;
}

map的使用-Hdu 2648的更多相关文章

  1. hdu 2648 Shopping

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2648 纯暴力的方法T_T... 如下: #include<cstdio> #include ...

  2. STL复习之 map & vector --- disney HDU 2142

    题目链接: https://vjudge.net/problem/40913/origin 大致题意: 这是一道纯模拟题,不多说了. 思路: map模拟,vector辅助 其中用了map的函数: er ...

  3. HDU 2648(搜索题,哈希表)

    #include<iostream> #include<map> #include<string> #include<cstring> #include ...

  4. 第一周训练 | STL和基本数据结构

    A - 圆桌问题: HDU - 4841 #include<iostream> #include<vector> #include<stdio.h> #includ ...

  5. stl_map,set 用法

    set: 集合a,b加起来,去重 hdu 1406 #include <iostream> #include<cstdio> #include<set> using ...

  6. Hash算法入门指南(聊点不一样的算法人生)

    前言 很多人到现在为止都总是问我算法该怎么学啊,数据结构好难啊怎么的,学习难度被莫名的夸大了,其实不然.对于一个学计算机相关专业的人都知道,数据结构是大学的一门必修课,数据结构与算法是基础,却常常容易 ...

  7. HDU 4941 Magical Forest(map映射+二分查找)杭电多校训练赛第七场1007

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4941 解题报告:给你一个n*m的矩阵,矩阵的一些方格中有水果,每个水果有一个能量值,现在有三种操作,第 ...

  8. hdu 1075 (map)

    http://acm.hdu.edu.cn/showproblem.php?pid=1075 What Are You Talking About Time Limit: 10000/5000 MS ...

  9. hdu 1800 (map)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1800 Flying to the Mars Time Limit: 5000/1000 MS (Java/ ...

随机推荐

  1. JavaScript对象之属性标签

    本文介绍一下js对象的属性标签(configurable.writable.enumerable.value.get.set)的使用. 上图的要点为: 1.Object.getOwnPropertyD ...

  2. 1-Django2.2安装指南

    django快速安装指南 作为一个Python Web框架,Django需要Python环境.下面是Django需要对应的python版本. Django版本 python版本 1.11 2.7, 3 ...

  3. 松软科技Web课堂:AJAX 简介

    AJAX AJAX 并不是编程语言. AJAX 是一种从网页访问 Web 服务器的技术. AJAX 代表异步 JavaScript 和 XML. AJAX 实例解释 HTML 页面 <!DOCT ...

  4. spring_boot 加入 mybatis

    第一步: <!-- mybatis启动器 自动包含jdbc所以不需要再次引入jdbc依赖 --> <dependency> <groupId>org.mybatis ...

  5. Educational Codeforces Round 76 D

    这次的ABC三道题非常水,但是我就卡在这个D题上了QAQ 当时大概猜到了贪心,但是没有思路,后来看了一些题解才明白到底是什么意思 首先,假设我们已经处理好了前面的monsters,对于第i个monst ...

  6. js动态创建的select2标签样式加载不上解决办法

    js动态创建的select2标签样式加载不上:调用select2的select2()函数来初始化一下: js抛出了Uncaught query function not defined for Sel ...

  7. selenium选择框

    自动化测试中,会遇到选择框,针对该类元素,selenium提供类Select类来处理,使用select类先导入:from selenium.webdriver.support.select impor ...

  8. 【Python】蟒蛇绘制(三种方式+import用法)

    第一种方式不会出现函数重名问题,而第二种会.可以用第三种解决问题 方式一: #pythondraw.py import turtle #引用 绘制(海龟)库 turtle.setup(650,350, ...

  9. try catch 语句中有return 的各类情况

    在牛客上做java题时遇到过多到关于try catch语句的问题,看了很多答案解析,在这里记录一下. 首先给出一道题目: 下面代码的运行结果为? A.catch语句块 和是43 B.编译异常 C.fi ...

  10. Windows10_64位搭建WampServer(运行php代码)教程及问题

    Windows10_64位搭建WampServer(运行php代码)教程及问题    笔者最近学习PHP,想通过web页面的形式更加形象生动的了解php代码的原理.     于是,这次就通过WampS ...