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. RS-232C

    RS-232C标准(协议)的全称是EIA-RS-232C标准,定义是"数据终端设备(DTE)和数据通讯设备(DCE)之间串行二进制数据交换接口技术标准".它是在1970年由美国电子 ...

  2. 【译】写个好的 CLI 程序

    写个好的 CLI 程序 Write a Good CLI Program 译文 原文链接:https://qiita.com/tigercosmos/items/678f39b1209e60843cc ...

  3. php设计模式之观察者模式实例代码

    php提供的两个接口,一个被观察者接口SplSubject,一个或多个观察者接口SPLObserver,和一个可以储存对象的类SplObjectStorage.被观察者有三个方法,需要实现这三个方法, ...

  4. 阻塞队列BlockingQueue之ASynchronousQueue

    一.SynchronousQueue简介 Java 6的并发编程包中的SynchronousQueue是一个没有数据缓冲的BlockingQueue,生产者线程对其的插入操作put必须等待消费者的移除 ...

  5. Ehab and a Special Coloring Problem

    You're given an integer nn. For every integer ii from 22 to nn, assign a positive integer aiai such ...

  6. Iris请求方式和数据返回类型

    1. Iris起服务 package main import "github.com/kataras/iris" func main() { //1.创建app结构体对象 app ...

  7. 【New】简•导航 正式上线

    [New]简•导航 正式上线 一个简单的导航 链接:http://huangenet.gitee.io/simple/ 欢迎访问⊙ω⊙ 代码托管在码云,访问速度更快哦!

  8. pyodbc-的一些说明

    cursor的description 可以获得一些关于表的信息 info=cursor.description 此时表中有多少列就有多少个元素,一个元素就是一列的信息(格式是tuple),所以这里的i ...

  9. 【转载】Hadoop自定义RecordReader

    转自:http://www.linuxidc.com/Linux/2012-04/57831.htm 系统默认的LineRecordReader是按照每行的偏移量做为map输出时的key值,每行的内容 ...

  10. Yii2 框架下 session跨域共享互通

    在项目实施过程中,往往把一个大项目进行分拆成几个独立的项目,项目用完全独立的域名和文件,可以放到不同的服务器上的独立分项目. 几个子项目共用一个登录点. 原理简单来说就是服务端session 共享, ...