前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Codeforces Round 473-2B题解报告

Codeforces Round 473-2B题解报告

作者头像
海天一树
发布2018-04-17 11:01:10
4480
发布2018-04-17 11:01:10
举报
文章被收录于专栏:海天一树

题目

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

分析

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

用第一个例子来分析:

group

index

cost

1

1

100

2

3

1

3

2

min(1, 10) = 1

3

5

min(1, 10) = 1

4

4

5

所求句子为”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

代码

代码语言:javascript
复制
#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;
}

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2018-04-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 海天一树 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目
  • 分析
  • 代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档