CF Make Cents?

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
1
3 5
dollar 0.71
euro 0.76
turkish 0.17
5.1 dollar
6 dollar
7 turkish
3 euro
1.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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值