B. Vitamins

题目链接:https://codeforces.com/contest/1042/problem/B

题意:

给出几种药,没种可能包含一种或多种(最多三种)维生素,现在问要吃到这三种维生素买药最少花费是多少。

题解:

嗯...可以直接暴力:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5+;
int n;
int c[N];
char s[N][];
int mn[];
int main(){
scanf("%d",&n);
for(int i=;i<;i++) mn[i]=;
string s1="BA",s2="AB",s3="AC",s4="CA";
for(int i=;i<=n;i++){
scanf("%d %s",&c[i],s[i]);
if(strlen(s[i])==){
if(s[i][]=='A') mn[]=min(mn[],c[i]);
else if(s[i][]=='B') mn[]=min(mn[],c[i]);
else mn[]=min(mn[],c[i]);
}else if(strlen(s[i])==){
if(s[i]==s1 || s[i]==s2)
mn[]=min(mn[],c[i]);
else if(s[i]==s3 || s[i]==s4){
mn[]=min(mn[],c[i]);
}else{
mn[]=min(mn[],c[i]);
}
}else{
mn[]=min(mn[],c[i]);
}
}
int ans = ;
ans=min(ans,mn[]+mn[]+mn[]);
ans=min(ans,mn[]+mn[]);
ans=min(ans,mn[]+mn[]);
ans=min(ans,mn[]+mn[]);
for(int i=;i<=;i++){
for(int j=;j<=;j++){
if(i==j) continue ;
ans=min(ans,mn[i]+mn[j]);
}
}
ans=min(ans,mn[]);
if(ans==) cout<<"-1";
else cout<<ans;
return ;
}

但是还有一种更简单的方法:dp,利用位运算来做。

代码如下:

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = ;
int dp[],a[N];
char s[N];
int n;
int main(){
scanf("%d",&n);
memset(dp,INF,sizeof(dp));
dp[]=;
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
scanf("%s",s);
int len = strlen(s),x=;
for(int j=;j<len;j++) x|=(<<(s[j]-'A'));
for(int j=;j<;j++) dp[x|j]=min(dp[x|j],dp[j]+a[i]);
}
if(dp[]==INF) cout<<-;
else cout<<dp[];
return ;
}

Codeforces Round #510 (Div. 2) B. Vitamins的更多相关文章

  1. Codeforces Round #510 (Div. 2)

    Codeforces Round #510 (Div. 2) https://codeforces.com/contest/1042 A 二分 #include<iostream> usi ...

  2. Codeforces Round #510 (Div. 2) D. Petya and Array(离散化+反向树状数组)

    http://codeforces.com/contest/1042/problem/D 题意 给一个数组n个元素,求有多少个连续的子序列的和<t (1<=n<=200000,abs ...

  3. Codeforces Round #510 (Div. 2) D. Petya and Array(树状数组)

    D. Petya and Array 题目链接:https://codeforces.com/contest/1042/problem/D 题意: 给出n个数,问一共有多少个区间,满足区间和小于t. ...

  4. Codeforces Round #510 (Div. 2) A&B By cellur925

    第一次CF祭== 由于太菜了只做了前两题== 因为在第一题上耗费时间太多了,我还是太菜了==. A. Benches time limit per test 1 second memory limit ...

  5. Codeforces Round #510 (Div. 2)(C)

    传送门:Problem C https://www.cnblogs.com/violet-acmer/p/9682082.html 题意: 给你n个数,定义有两种操作 ① 1 i j : (i != ...

  6. Codeforces Round #510 (Div. 2)(B)

    传送门:Problem B https://www.cnblogs.com/violet-acmer/p/9682082.html 题意: 如果可以通过喝果汁将维生素A,B,C全部摄取,求最小花费,如 ...

  7. Codeforces Round #510 (Div. 2)(A)

    传送门:Problem A https://www.cnblogs.com/violet-acmer/p/9682082.html 题意: 公园里有n个沙滩,a[i]表示第i个沙滩初始人数,现有m个人 ...

  8. codeforces 1042d//Petya and Array// Codeforces Round #510 (Div. 2)

    题意:给出一个数组,求其中和小于t的区间数. 先计算前缀和数组sum[i].对当前的sum[i],查询树状数组中有几个比(sum[i]-t)大的数,那么用sum[i]减它就是一个合法区间.再将当前的s ...

  9. codeforces 1042c// Array Product// Codeforces Round #510(Div. 2)

    题意:给出一个数组,2种操作:.1:x*y然后x消失,2:除掉x(2操作最多只能进行一次).问最大的结果的一种操作方式.逻辑题,看能不能想全面. 1先数好0,正,负的数量,zero,pos,neg.如 ...

随机推荐

  1. Windows10 快捷键

    windows 10快捷键: F1 打开帮助 F2 重命名 F3 打开搜索文件和文件夹 F4 打开地址栏常用的地址 F5 刷新 F11   全屏 选择文件和内容: shift + 上下左右键选择连续的 ...

  2. [HDU1512]Monkey King(左偏树)

    用并查集维护猴子们的关系,强壮值用左偏树维护就行了 Code #include <cstdio> #include <algorithm> #include <cstri ...

  3. 6.JAVA知识点归纳整理

    一.jdk初识与HelloWord: 二.java基础: 2.1 标识符_关键字_数据类型 2.2 数据类型转换 2.3 程序编写格式 2.4 运算符 2.5 分支与for循环 2.6 while_b ...

  4. Python的import导入与时间

    一.模块与包 模块,在Python可理解为对应于一个文件.在创建了一个脚本文件后,定义了某些函数和变量.你在其他需要这些功能的文件中,导入这模块,就可重用这些函数和变量.一般用module_name. ...

  5. spring、spring-data-redis整合使用

    一.Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API. 从2010年3月15日起,Redis的开发工作由VMwa ...

  6. Hackerrank - The Grid Search

    https://www.hackerrank.com/challenges/the-grid-search/forum 今天碰见这题,看见难度是Moderate,觉得应该能半小时内搞定. 读完题目发现 ...

  7. jmeter使用BeanShell断言

    1. 首先存储一个接口的响应结果,如在http请求的BeanShell PostProcessor: import java.io.UnsupportedEncodingException; Syst ...

  8. 商业地产 招商 招租 CRM 意向 洽谈 合同 复用商铺商户管理系统

    适用场合 本软件适合商业地产的对招商的全流程管理,包括商铺信息,商户信息,洽谈信息,意向签订,合同管理等. 软件有试用版可供下载试用. 联系方式 QQ:2417158658 Tel:130251102 ...

  9. Linux-OpenSUSE折腾-1(Qt安装,Chrome安装)

    先上图,大蜥蜴还是不错的,偶然看到了大蜥蜴这个系统,我就觉得又可以折腾几天了,先上图 OpenSUSE有一个入门介绍的网站写的相当不错,感兴趣的可以连接过去:https://lug.ustc.edu. ...

  10. Python 3基础教程19-模块导入语法

    本文开始介绍模块导入的一些基本语法,我们现在还在Python自带的IDLE编辑器里写Python代码,如果你要需要一个功能,例如build-in的模块,那么你就需要先导入这个模块,然后才能使用这个模块 ...