Codeforces Round 473-2B题解报告

题目

http://codeforces.com/contest/959/problem/B

分析

思路:
每一个单词对应着在原句子中的索引号,每个索引号对应着组号,每个组号对应着cost值。

用第一个例子来分析:

groupindexcost
11100
231
32min(1, 10) = 1
35min(1, 10) = 1
445

所求句子为”i am the second”
“i”在原句子中的索引号为1,对应第一组,cost为100
“am”在原句子中的索引号为3,对应第二组,cost为1
“the”在原句子中的索引号为4,对应第四组,cost为5
“second”在原句子中的索引号为5,对应第三组,cost为1
所以,结果为100 + 1 + 5 + 1 = 107

代码

#include <iostream>
#include <map>
using namespace std;

map<string,int> mp;
int a[100005],cost[100005],group[100005];

int main()
{
    int n,k,m;
    // The 1st line
    cin >> n >> k >> m;

    // The 2nd line
    for (int i=1;i<=n;i++)
    {
        string s;
        cin >> s;
        mp[s]=i;
    }

    // The 3rd line
    for (int i=1;i<=n;i++)
    {
        cin >> a[i];
        // initialize a big number, 2^30
        cost[i]=(1<<30);
    }

    // The next k lines
    for (int groupI=1;groupI<=k;groupI++)
    {
        int x;
        cin >> x;
        while(x--)
        {
            int index;
            cin >> index;
            group[index]=groupI;
            cost[groupI]=min(cost[groupI],a[index]);
        }
    }

    long long ans=0;
    // The last line
    while(m--)
    {
        string s;
        cin >> s;
        // mp[s] means index
        ans+=cost[group[mp[s]]];
    }

    cout << ans;
}


Codeforces & TopCoder QQ交流群:648202993
更多内容请关注微信公众号
1.jpg

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值