CodeForces - 1042B
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 11 to 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
4
5 C
6 B
16 BAC
4 A
15
2
10 AB
15 BA
-1
5
10 A
9 BC
11 CA
4 A
5 B
13
6
100 A
355 BCA
150 BC
160 AC
180 B
190 CA
250
2
5 BA
11 CB
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".
你猜我的代码错在哪了,找找看?
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<deque>
#include<map>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0);
const double e=exp();
const int N = ; int main()
{
char kind[];
LL i,p,j;
LL n,x;
LL a,b,c,ab,ac,bc,abc;
LL flag=;
a=b=c=ab=ac=bc=abc=;
scanf("%lld",&n);
for(i=;i<=n;i++)
{
scanf("%lld %s",&x,kind);
if((strcmp(kind,"BA")==)||(strcmp(kind,"AB")==))
{
if(x<ab)
ab=x;
}
else if((strcmp(kind,"AC")==)||(strcmp(kind,"CA")==))
{
if(x<ac)
ac=x;
}
else if((strcmp(kind,"BC")==)||(strcmp(kind,"CB")==))
{
if(x<bc)
bc=x;
}
else if(kind[]=='A')
{
flag++;
if(x<a)
a=x;
}
else if(kind[]=='B')
{
flag++;
if(x<b)
b=x;
}
else if(kind[]=='C')
{
flag++;
if(x<c)
c=x;
}
else
{
if(x<abc)
abc=x;
}
kind[]=;
} LL head=;
if(a<&&b<&&c<)
head=a+b+c;
if(bc<&&a<&&(a+bc<head))
head=a+bc;
if(abc<&&a<&&(a+abc<head))
head=a+abc;
if(abc<&&b<&&(b+abc<head))
head=b+abc;
if(abc<&&c<&&(c+abc<head))
head=c+abc;
if(ac<&&b<&&(b+ac<head))
head=b+ac;
if(ab<&&c<&&(c+ab<head))
head=c+ab;
if(ac<&&ab<&&(ac+ab<head))
head=ac+ab;
if(ab<&&bc<&&(ab+bc<head))
head=ab+bc;
if(ac<&&bc<&&(ac+bc<head))
head=ac+bc;
if(abc<&&abc<head)
head=abc; if(head!=)
printf("%lld\n",head);
else
printf("-1\n");
return ; }
if else if else if if if .......因为这种问题错过几遍了,心里没有点Number B 吗?!!
明明"ABC"的串还没有比过,你就直接比较kind[0]??? 脑子呢???
写if else 的时候能不能心里想着逻辑点,能不能走点心?
正确代码:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<deque>
#include<map>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0);
const double e=exp();
const int N = ; int main()
{
char kind[];
LL i,p,j;
LL n,x;
LL a,b,c,ab,ac,bc,abc;
LL flag=;
a=b=c=ab=ac=bc=abc=;
scanf("%lld",&n);
for(i=;i<=n;i++)
{
scanf("%lld %s",&x,kind);
if((strcmp(kind,"BA")==)||(strcmp(kind,"AB")==))
{
if(x<ab)
ab=x;
}
else if((strcmp(kind,"AC")==)||(strcmp(kind,"CA")==))
{
if(x<ac)
ac=x;
}
else if((strcmp(kind,"BC")==)||(strcmp(kind,"CB")==))
{
if(x<bc)
bc=x;
}
else if(strcmp(kind,"A")==)
{
flag++;
if(x<a)
a=x;
}
else if(strcmp(kind,"B")==)
{
flag++;
if(x<b)
b=x;
}
else if(strcmp(kind,"C")==)
{
flag++;
if(x<c)
c=x;
}
else
{
if(x<abc)
abc=x;
}
kind[]=;
} LL head=;
if(a<&&b<&&c<)
head=a+b+c;
if(bc<&&a<&&(a+bc<head))
head=a+bc;
if(abc<&&a<&&(a+abc<head))
head=a+abc;
if(abc<&&b<&&(b+abc<head))
head=b+abc;
if(abc<&&c<&&(c+abc<head))
head=c+abc;
if(ac<&&b<&&(b+ac<head))
head=b+ac;
if(ab<&&c<&&(c+ab<head))
head=c+ab;
if(ac<&&ab<&&(ac+ab<head))
head=ac+ab;
if(ab<&&bc<&&(ab+bc<head))
head=ab+bc;
if(ac<&&bc<&&(ac+bc<head))
head=ac+bc;
if(abc<&&abc<head)
head=abc; if(head!=)
printf("%lld\n",head);
else
printf("-1\n");
return ; }
CodeForces - 1042B的更多相关文章
- 2018SDIBT_国庆个人第三场
A - A CodeForces - 1042A There are nn benches in the Berland Central park. It is known that aiai peo ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
- CodeForces - 696B Puzzles
http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
随机推荐
- sublinme 快捷键格式
{"keys": ["ctrl+shift+f"], "command": "reindent" , "arg ...
- swusec的构想,顺便送开学福利——校园网一号多登录演示
前言: 我不是什么大牛,我只想通过我的努力,打造swu网络安全爱好者的圈子.期待你加入. swusec是什么? swusec (SouthWestUniversity SecurityTeam),西南 ...
- day02--Python基础二(基础数据类型)
一.数据与数据类型 1 什么是数据? x=10,10是我们要存储的数据 2 为何数据要分不同的类型 数据是用来表示状态的,不同的状态就应该用不同的类型的数据去表示 3 数据类型 数字(int) 字符串 ...
- linux下tomcat、jenkins环境搭建
1.安装JDK 我不列出来了,自行百度 java -version 2.安装tomcat (1)将下载的tomcat压缩包 tar -zxvf apache-tomcat-8.5.29.tar.gz ...
- BZOJ5254 FJWC2018红绿灯(线段树)
注意到一旦在某个路口被红灯逼停,剩下要走的时间是固定的.容易想到预处理出在每个路口被逼停后到达终点的最短时间,这样对于每个询问求出其最早在哪个路口停下就可以了.对于预处理,从下一个要停的路口倒推即可. ...
- BZOJ 3357: [Usaco2004]等差数列
3357: [Usaco2004]等差数列 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 338 Solved: 160[Submit][Statu ...
- Alpha 完结撒花 —— 事后诸葛亮
写在前面 林燊大哥 一路走来,好不容易,终于完结了. 设想和目标 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 解决的问题 用户在进店之前无法得知店铺的优劣,通过 ...
- Alpha 冲刺 —— 十分之五
队名 火箭少男100 组长博客 林燊大哥 作业博客 Alpha 冲鸭鸭鸭鸭鸭! 成员冲刺阶段情况 林燊(组长) 过去两天完成了哪些任务 协调各成员之间的工作 协助测试的进行 测试项目运行的服务器环境 ...
- 使用SUID二进制文件进行Linux权限升级技巧
0x00 基础知识 众所周知,在Linux中一切都以文件存在,包括具有允许或限制三个执行操作(即读/写/执行)权限的目录和设备.因此,当给任何文件设置权限时,应该需要了解允许的Linux用户或限制 ...
- NAT ------ 为什么手动设置NAT端口映射(转发)不成功,导致访问不了局域网服务器
手动设置端口映射成功的条件是路由器WAN口接的是外网IP,而不是网络提供商的路由器NAT之后的IP.假如有个外网的客户端,连的服务器IP一定要是外网IP(假设IP_A),如果自己的路由器WAN口接的是 ...