M. Make Cents?
time limit per test

6.0 s

memory limit per test

256 MB

input

standard input

output

standard output

Every year, an elephant qualifies to the Arab Collegiate Programming Competition. He graduated this year, but that’s irrelephant. What’s important is that the location of the competition might not have been the same every year. Therefore, after every trip, he always has leftover money in the currency of the country he visited.

Now he wants to see how much Jordanian Dinars he has after all those competitions. Can you help him convert the leftover money from all competitions to Jordanian Dinar, if that makes any cents?

Input

The first line of input is T – the number of test cases.

The first line of each test case contains C and N (1 ≤ C, N ≤ 100000), the number of currency types and the number of competitions, respectively.

The next C lines each contain the name of the currency Ci of maximum length 10 in lowercase and/or uppercase letters, and the value Vi of that currency in Jordanian Dinar (0 < Vi ≤ 1000). The names are case-sensitive.

The next N lines each contains an amount left over from each competition (0 ≤ Ni ≤ 1000), and the name of the currency of that amount (it is guaranteed that the name was either given in the input or is “JD”).

Output

For each test case, print on a single line the total amount of money he has in Jordanian Dinar(JD) rounded to 6 decimal digits.

Example
Input

Copy

13 5dollar 0.71euro 0.76turkish 0.175.1 dollar6 dollar7 turkish3 euro1.1 JD

Output

Copy

12.451000

这道题当时要么WA在第二组样例,,要么T在第二组样例,既可以说是自己的思路不够缜密,又可以说是存在侥幸心理,,,在输入中给的汇率有可能货币种类数要大于此人所拥有的货币种类数!

再说一下这道题带来的收获吧,会了一个知识点,就是将两层嵌套的循环O(n*m)改为时间复杂度为

O(n+m)。
对于每一个b[i].name都要找到相对应的汇率,第一个思路当然是两层循环结束,现在想第二个思路,在这个题中,有一个特点就是:对于每一个循环过的汇率将不会再次使用。于是用一个下标p来控制第几个汇率,必要时p++,否则不动。
AC代码:
#include <iostream>
#include<algorithm>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<deque>
#include<stack>
using namespace std;
struct A
{char name[50];double w;
}a[100010],b[100100];
bool cmp(struct A a,struct A b)
{return strcmp(a.name,b.name)<0;
}
int main()
{int T;int n,m,i,j,p,k,w;char flag[]={"zzzzzzzzzz"};double ans;scanf("%d",&T);while(T--){ans=k=0;scanf("%d%d",&n,&m);for(i=0;i<n;i++){scanf("%s %lf",&a[i].name,&a[i].w);}for(i=0;i<m;i++){scanf("%lf %s",&b[i].w,&b[i].name);}sort(a,a+n,cmp);sort(b,b+m,cmp);w=0;for(i=1;i<m;i++){if(strcmp(b[i-1].name,b[i].name)==0){b[i].w+=b[i-1].w;strcpy(b[i-1].name,flag);w++;}}sort(b,b+m,cmp);p=0;for(i=0;i<m-w;i++)                         //重点在这里开始{if(strcmp(b[i].name,a[p].name)==0)    {ans+=b[i].w*a[p].w;}else if(strcmp(b[i].name,"JD")==0){ans+=b[i].w;}else{p++;i--;}}printf("%.6lf\n",ans);}return 0;
}

CF Make Cents?相关推荐

  1. 『参考』.net CF组件编程(4)——为自定义组件添加工具箱图标!

    前言: 在前三篇的文章中,和大家一起创建了一个用于TCP连接检测的小组件,如果你记不得了,可以通过以下链接去回顾一下: 『参考』.net CF组件编程(1)--基础之后 『参考』.net CF组件编程 ...

  2. OC对象 vs CF对象

    2019独角兽企业重金招聘Python工程师标准>>> OC对象 vs CF对象 在ARC场景下,对象所有权没有转换 使用__bridge关键字即可实现CF对象和OC对象之间的自由转 ...

  3. CF 990A. Commentary Boxes【数学/模拟】

    [链接]:CF [题意]:对于一个数n,每次加一的代价是a,每次减一的代价是b,求被m整除时的最小代价. [分析]:分情况讨论,自己多举几个栗子. [代码]: #include<cstdio&g ...

  4. 推荐算法——基于协同过滤CF

    https://www.toutiao.com/a6643326861214482957/ 2019-01-06 18:21:09 前边我们已经介绍了推荐算法里的基于内容的推荐算法CB,今天我们来介绍 ...

  5. 索引贴——移动开发(.Net CF 停止更新)

    这是关于本人博客的技术索引贴,希望能方便的让您阅读到相关技术文章--不断更新中.一整理才发现,好多啊,哈哈- 一..Net CF技巧:搜集.转载一些和CF开发相关的辅助文章,比较适合初学者.开发入门者 ...

  6. 解答:CF截图保存在哪

    为什么80%的码农都做不了架构师?>>>    大家玩CF(穿越火线)的时候遇到精彩的画面总希望截图保存下来,然而有些游戏玩家截图后却不知道CF截图保存在哪!这不得不说是个悲剧,但是 ...

  7. CF里面的资源载入问题

    前一段时间已经发现CF在载入资源的时候会怪怪的,但是这一段时间都不曾记起要对这个问题研究一下.最近又发现这个问题了,实在是恼火.俗话说择日不如撞日(粤语),唉,就今天啦.这个问题是在VS2k5里面调试 ...

  8. [CF.Skills]播放嵌入资源的声音文件

    [CF.Skills]播放嵌入资源的声音文件 摘要:本文阐述了在Windows Mobile中如何播放潜入资源的声音文件KeywordsPlaySound, Windows Mobile, Embed ...

  9. CF#190DIV.1

    1 /* 2 CF#190DIV.1-C 3 题意:给你n个结点的树,给这些结点标记字母AB..Z,对于标记相同的结点路径上 4 的结点的标记必须有一个是大于该标记的:问是否可以标记(A是最大标记) ...

  10. 汇编SF、CF、 OF 、ZF、 PF

    SF=0(最高位是什么,ZF就是什么) CF=1(最高位有进位或借位就置1) ZF=0(结果为0,ZF就置1) OF=1(两个负数相加变成正数,溢出) PF=0(反映结果"1"的个 ...

最新文章

  1. Scrum 冲刺博客第二篇
  2. java 跨域_springboot解决跨域CROS问题,用注解@CrossOrigin
  3. 增强QQ空间的统计功能
  4. 从原理到实践手动拼凑一个Linux系统
  5. 给Win7光盘添加PE3.0
  6. structure101_使用structure101分析软件包的依赖关系
  7. 机器学习笔记(三十二):集成学习、随机森林
  8. PhotoShop中批量导出图片
  9. 北京市地铁线路及站点数据
  10. 获取电脑的唯一识别码_无锡电脑办公,office软件培训,学会为止
  11. Altium未连接的网络DRC检查不出的问题
  12. 编译错误:找不到工程或库
  13. GOOGLE搜索秘籍--高级搜索:site,link,inurl,allinurl,intitle,allintitle
  14. MongoDB 3.2.7 for rhel6.4 副本集-分片集群部署
  15. 解决word文件由于扩展名不匹配问题
  16. 女生适合学UI设计吗
  17. android涂鸦实现
  18. Android开发未来的出路何在,android学习路线图
  19. 基于TLE6220GP的开关电磁阀驱动电路
  20. [NOIP 2018 T3]摆渡车

热门文章

  1. latex利用bibmap生成双语对照的文献表
  2. vue3安装全家桶教程
  3. keil5中输入中文并且美化字体
  4. 浅谈基于过程与基于对象
  5. local variable referenced before assignment 原因及解决办法
  6. 当成人网站遇上机器学习
  7. OJ期末刷题 问题 B: 求三角形面积-gyy
  8. 用键盘输入一位整数,当输入1~7时,显示对应的英文星期名称的缩写。
  9. 深入理解操作系统实验——bomb lab(phase_5)
  10. 完成端口(CompletionPort)详解 - 手把手教你玩转网络编程系列之三_zzjlzx-ChinaUnix博客...