【CodeForces - 1042B】Vitamins(去重方法,二进制或stlmap,水题)

题干:

Berland shop sells nn kinds of juices. Each juice has its price cici. Each juice includes some set of vitamins in it. There are three types of vitamins: vitamin "A", vitamin "B" and vitamin "C". Each juice can contain one, two or all three types of vitamins in it.

Petya knows that he needs all three types of vitamins to stay healthy. What is the minimum total price of juices that Petya has to buy to obtain all three vitamins? Petya obtains some vitamin if he buys at least one juice containing it and drinks it.

Input

The first line contains a single integer nn (1≤n≤1000)(1≤n≤1000) — the number of juices.

Each of the next nn lines contains an integer cici (1≤ci≤100000)(1≤ci≤100000) and a string sisi — the price of the ii-th juice and the vitamins it contains. String sisi contains from 11to 33 characters, and the only possible characters are "A", "B" and "C". It is guaranteed that each letter appears no more than once in each string sisi. The order of letters in strings sisi is arbitrary.

Output

Print -1 if there is no way to obtain all three vitamins. Otherwise print the minimum total price of juices that Petya has to buy to obtain all three vitamins.

Examples

Input

4
5 C
6 B
16 BAC
4 A

Output

15

Input

2
10 AB
15 BA

Output

-1

Input

5
10 A
9 BC
11 CA
4 A
5 B

Output

13

Input

6
100 A
355 BCA
150 BC
160 AC
180 B
190 CA

Output

250

Input

2
5 BA
11 CB

Output

16

Note

In the first example Petya buys the first, the second and the fourth juice. He spends 5+6+4=155+6+4=15 and obtains all three vitamins. He can also buy just the third juice and obtain three vitamins, but its cost is 1616, which isn't optimal.

In the second example Petya can't obtain all three vitamins, as no juice contains vitamin "C".

解题报告:

   别忘了考虑  ‘AB’和‘BC’这类的三种情况,‘AB’和‘ABC’这种就不需要了,因为还不如直接'ABC'呢。

AC代码:

#include<bits/stdc++.h>using namespace std;
int n,m;
map<string,int> mp;
int main()
{int n;string s;int tmp;cin>>n; for(int i = 1; i<=n; i++) {cin>>tmp>>s;sort(s.begin(),s.end());if(mp.find(s) == mp.end()) mp[s] = tmp;else mp[s] = min(mp[s],tmp);}int minn = 0x3f3f3f3f;if(mp["A"]>0 && mp["B"]>0 && mp["C"]>0)	minn = min(minn,mp["A"] + mp["B"] + mp["C"]);if(mp["AB"]>0 && mp["C"]>0) minn = min(minn,mp["AB"] + mp["C"]);if(mp["A"]>0 && mp["BC"]>0) minn = min(minn,mp["A"] + mp["BC"]);if(mp["AC"]>0 && mp["B"]>0) minn = min(minn,mp["AC"] + mp["B"]);if(mp["ABC"]>0) minn = min(minn,mp["ABC"]);if(mp["AB"] && mp["AC"]) minn = min(minn,mp["AB"] + mp["AC"]); if(mp["AB"] && mp["BC"]) minn = min(minn,mp["AB"] + mp["BC"]); if(mp["AC"] && mp["BC"]) minn = min(minn,mp["AC"] + mp["BC"]); if(minn == 0x3f3f3f3f) puts("-1");else printf("%d\n",minn); return 0;
}

其他的一堆AC代码:(有用dp的,有无限更新的)

#include <bits/stdc++.h>/*
unsigned seed1 = std::chrono::system_clock::now().time_since_epoch().count();
mt19937 g1.seed(seed1);ios_base::sync_with_stdio(false);
cin.tie(NULL);
*/
using namespace std;int dp[8];
int n;char buf[10];int main() {scanf("%d", &n);for(int i = 1; i < 8; i++) {dp[i] = 1e9;}while(n--) {int s;scanf("%d %s", &s, buf);int m = 0;for(int i = 0; i < strlen(buf); i++) {m |= 1 << (buf[i] - 'A');}for(int i = 0; i < 8; i++) {dp[i|m] = min(dp[i|m], dp[i] + s);}}if(dp[7] == 1e9) dp[7] = -1;printf("%d\n", dp[7]);
}#include<bits/stdc++.h> 
using namespace std;
typedef long long ll;int N;
int m[8];int main() {ios_base::sync_with_stdio(0);cin.tie(NULL);for (int i = 0; i < 8; i++)m[i] = 1e6;cin >> N;for (int i = 0; i < N; i++) {int c;string s;cin >> c >> s;int cc = 0;for (int j = 0; j < s.length(); j++)cc += (1 << (s[j] - 'A'));m[cc] = min (m[cc], c);}int mans = 1e9;for (int i = 0; i < 256; i++) {int b = 0, cc = 0;for (int j = 0; j < 8; j++) {if (i & (1 << j)) {b |= j;cc += m[j];}}if (b == 7)mans = min (mans, cc);}if (mans >= 1e6)cout << "-1\n";elsecout << mans << "\n";
}#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int inf=1e9;
int n, c, bst[8];
char buf[8];
int main() {for(int i=1; i<8; i++) bst[i]=inf;scanf("%d", &n);while(n--) {scanf("%d%s", &c, buf);int msk=0;for(int i=0; buf[i]; i++)msk|=(1<<(buf[i]-'A'));bst[msk]=min(bst[msk], c);}for(int rnd=0; rnd<514; rnd++)for(int i=0; i<8; i++)for(int j=0; j<8; j++)bst[i|j]=min(bst[i|j], bst[i]+bst[j]);if(bst[7] == inf) bst[7]=-1;cout<<bst[7]<<endl;
}#include <bits/stdc++.h>
using namespace std;const int INF = 123456789;int p[8];int main () {int n;cin >> n;for (int i=0; i<8; ++i) {p[i] = INF;}while (n--) {int x;string s;cin >> x >> s;int mask = 0;for (char i:s) {mask |= (1<<(i-'A'));}p[mask] = min(p[mask],x);}int cost = INF;for (int i=0; i<8; ++i) {if (i == 7) {cost = min(cost,p[i]);}}for (int i=0; i<8; ++i) {for (int j=0; j<8; ++j) {if ((i|j) == 7) {cost = min(cost,p[i]+p[j]);}}}for (int i=0; i<8; ++i) {for (int j=0; j<8; ++j) {for (int k=0; k<8; ++k) {if ((i|j|k) == 7) {cost = min(cost,p[i]+p[j]+p[k]);}}}}if (cost == INF) cout << -1 << endl;else cout << cost << endl;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441575.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

android 中断处理流程,Android P的native crash处理流程

一、概述Android系统有监控程序异常退出的机制&#xff0c;这便是本文要讲述得debuggerd守护进程。当发生native crash或者主动调用debuggerd时&#xff0c;会输出进程相关的状态信息到文件或者控制台。输出的debuggerd数据 保存在文件/data/tombstones/tombstone_XX&#xff0…

数论中的无数公式 总结

斯特林公式是一条用来取n阶乘近似值的数学公式。一般来说&#xff0c;当n很大的时候&#xff0c;n阶乘的计算量十分大&#xff0c;所以斯特灵公式十分好用&#xff0c;而且&#xff0c;即使在 n很小的时候&#xff0c;斯特灵公式的取值已经十分准确。 公式为&#xff1a; 以下…

计算机怎样连接网络适配器,win10 本地连接共享internet时, 会将承载网络适配器IP设置成192.168.137.1但不成功...

您好&#xff0c;谢谢您&#xff01;我后来参考了http://www.ishanarora.com/2009/07/29/windows-7-as-a-wireless-access-point/ 中的 Jenny提到的方案COMPLETE SOLUTION:### create virtual vwlan hosted port, set AP ssid to advertise, and its password- [cmd] netsh wla…

【POJ - 1001 】Exponentiation (Java大数,高精度)

题干&#xff1a; Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt is a taxing experience for many computer systems. This problem requires that you write…

用计算机唱出惊雷,除了《惊雷》还有多少喊麦神曲?这十首神作你一定听过!...

一首《惊雷》可以说是火遍了大江南北&#xff0c;一时间风头无二。而这两天杨坤对《惊雷》的diss、惊雷原唱六道的回应更是成为全网热搜&#xff0c;这也让以《惊雷》为首的“喊麦文化”再次进入了公众的视线。事实上除了《惊雷》&#xff0c;还有不少脍炙人口的喊麦神曲&#…

ACM算法 -- 数论 -- 开灯关灯问题(数论,整数分解,因子个数,公式推导)

有编号1~100个灯泡&#xff0c;起初所有的灯都是灭的。有100个同学来按灯泡开关&#xff0c;如果灯是亮的&#xff0c;那么按过开关之后&#xff0c;灯会灭掉。如果灯是灭的&#xff0c;按过开关之后灯会亮。 现在开始按开关。 第1个同学&#xff0c;把所有的灯泡开关都按一次…

计算机编程是考研什么专业,程序员考研该不该继续选择计算机专业

首先&#xff0c;近些年来确实有不少程序员会选择通过读研来突破自身的岗位发展瓶颈&#xff0c;大部分程序员在读研后也都获得了岗位升级&#xff0c;还有一部分程序员会继续读博&#xff0c;从而进入到科研和教育领域发展。从当前的技术发展趋势和人才需求趋势来看&#xff0…

【牛客 - 185D】星光晚餐(数论,结论,思维,模型)

题干&#xff1a; Johnson和Nancy要在星光下吃晚餐。这是一件很浪漫的事情。 为了增加星光晚餐那浪漫的氛围&#xff0c;他拿出了一个神奇的魔法棒&#xff0c;并且可以按照一定的规则&#xff0c;改变天上星星的亮暗。 Johnson想考考Nancy&#xff0c;在他挥动魔法棒后&#x…

vue 模板 html 表达式,Vue 模板template、指令directive、修饰符

模板 templatetemplate的三种写法写法一&#xff1a;Vue完整版&#xff0c;写在HTML里//html{{n}}1//vuenew Vue({el: #xxx,data(){return{n:0}},methods:{add(){}}})复制代码写法二&#xff1a;Vue完整版&#xff0c;写在选项里//html//vuenew Vue({template: {{n}}1,data(){r…

黄冈学计算机的学校怎么样,广元市黄冈学校怎么样、好不好

问&#xff1a;广元市黄冈学校怎么样、好不好?答&#xff1a;办学四年多来&#xff0c;学校在上级主管部门考核中一直名列前茅&#xff0c;得到广大学生家长的认可&#xff0c;多次受到上级表彰&#xff0c;被评为“民办教育先进集体”&#xff0c;学校环境优美&#xff0c;是…

【 HDU - 1215 】七夕节(数论,约数和公式)

题干&#xff1a; 七夕节那天,月老来到数字王国,他在城门上贴了一张告示,并且和数字王国的人们说:"你们想知道你们的另一半是谁吗?那就按照告示上的方法去找吧!" 人们纷纷来到告示前,都想知道谁才是自己的另一半.告示如下: 数字N的因子就是所有比N小又能被N整除的…

计算机专业小三门要求,最新小三门选科要求有啥变化?附对比表

原标题&#xff1a;最新小三门选科要求有啥变化&#xff1f;附对比表刚刚&#xff0c;上海市教育考试院公布了2020年拟在沪招生普通高校、军队院校本科专业选考科目要求&#xff0c;让我们今年的选科范围有哪些变化&#xff1f;说明&#xff1a;1 选考范围为不限&#xff0c;说…

【51Nod - 1215 】数组的宽度 (单调栈 或 分治 或 单调队列,算贡献,需去重)

题干&#xff1a; N个整数组成的数组&#xff0c;定义子数组aii..ajj的宽度为&#xff1a;max(ai..aj) - min(ai..aj)&#xff0c;求所有子数组的宽度和。 Input 第1行&#xff1a;1个数N&#xff0c;表示数组的长度。(1 < N < 50000) 第2 - N 1行&#xff1a;每行1…

云昆明理工大学 计算机复试,2012年昆明理工大学计算机考研复试试题(回忆版)...

2012年昆明理工大学计算机考研复试试题(年昆明理工大学计算机考研复试试题(回忆版)回忆版)本试题由http://doc.xuehai.net网友kenden23提供笔试C(总共30分)一、40题选择题(20分)1.派生类调用构造函数时&#xff0c;基类的构造函数先调用&#xff1b;2. int a1,b1,c1,d1,e1;if((…

*【CodeForces - 122C 】Lucky Sum (bfs记录状态,二分查找,有坑)(或分块)

题干&#xff1a; Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Let next(x) be the…

模拟量使用计算机电缆,DJYPVRP计算机电缆型号规格

计算机电缆用途&#xff1a;本产品适用于额定电压30/500v及以下防干扰性能要求较高的电子计算机、检测仪器、仪表的连接。使用条件&#xff1a; 计算机电缆(电压等级&#xff1a;0.45/0.75KV&#xff0c;企标)本产品使用于交流额定电压为300/500及以下&#xff0c;对于防干扰性…

【CodeForces - 155C】Hometask (字符串,思维,贪心,熟悉句式)(总结)

题干&#xff1a; Sergey attends lessons of the N-ish language. Each lesson he receives a hometask. This time the task is to translate some sentence to the N-ish language. Sentences of the N-ish language can be represented as strings consisting of lowercas…

html 报表插件,轻量级图形报表插件JSCharts

插件描述&#xff1a;JScharts是一个用于在浏览器直接绘制图表的JavaScript工具包。JScharts支持柱状图、圆饼图以及线性图&#xff0c;可以直接将这个图插入网页&#xff0c;JScharts图的数据可以来源于XML文件、JSON文件或JavaScript数组变量。2017-05-09更新&#xff1a;改为…

算法讲解 -- 二分图之 匈牙利算法

匈牙利算法是由匈牙利数学家Edmonds于1965年提出&#xff0c;因而得名。匈牙利算法是基于Hall定理中充分性证明的思想&#xff0c;它是部图匹配最常见的算法&#xff0c;该算法的核心就是寻找增广路径&#xff0c;它是一种用增广路径求二分图最大匹配的算法。 -------等等&…

html 表格文字颜色 css,CSS 表格-JavaScript中文网-JavaScript教程资源分享门户

使用 CSS 可以使 HTML 表格更美观。CompanyContactCountryAlfreds FutterkisteMaria AndersGermanyBerglunds snabbkpChristina BerglundSwedenCentro comercial MoctezumaFrancisco ChangMexicoErnst HandelRoland MendelAustriaIsland TradingHelen BennettUKKniglich EssenP…