Codeforces Round 473-2B题解报告

曾经终败给现在 2022-05-28 11:48 320阅读 0赞

题目

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

代码

  1. #include <iostream>
  2. #include <map>
  3. using namespace std;
  4. map<string,int> mp;
  5. int a[100005],cost[100005],group[100005];
  6. int main()
  7. {
  8. int n,k,m;
  9. // The 1st line
  10. cin >> n >> k >> m;
  11. // The 2nd line
  12. for (int i=1;i<=n;i++)
  13. {
  14. string s;
  15. cin >> s;
  16. mp[s]=i;
  17. }
  18. // The 3rd line
  19. for (int i=1;i<=n;i++)
  20. {
  21. cin >> a[i];
  22. // initialize a big number, 2^30
  23. cost[i]=(1<<30);
  24. }
  25. // The next k lines
  26. for (int groupI=1;groupI<=k;groupI++)
  27. {
  28. int x;
  29. cin >> x;
  30. while(x--)
  31. {
  32. int index;
  33. cin >> index;
  34. group[index]=groupI;
  35. cost[groupI]=min(cost[groupI],a[index]);
  36. }
  37. }
  38. long long ans=0;
  39. // The last line
  40. while(m--)
  41. {
  42. string s;
  43. cin >> s;
  44. // mp[s] means index
  45. ans+=cost[group[mp[s]]];
  46. }
  47. cout << ans;
  48. }

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

发表评论

表情:
评论列表 (有 0 条评论,320人围观)

还没有评论,来说两句吧...

相关阅读