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个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
随机推荐
- APP端测试与web端测试的区别
想要知道APP端测试与web端测试的区别 ,那么我们就要先来了解,web和app的区别. web项目,一般都是b/s架构,基于浏览器的,而app则是c/s的,必须要有客户端.那么在系统测试测试的时候就 ...
- Node初识笔记 1第一周
#下载安装好node > https://nodejs.org/en/ # 打开cmd 调整好执行路径 . 1.js是JS文件名,cd调招路径,‘node’+空格 +JS文件名(带上扩展名) ...
- Java 文件下载功能 解决中文乱码
Html部分 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <ti ...
- java.lang.NoSuchMethodError: org.hibernate.integrator.internal.IntegratorServiceImpl.<init>(Ljava/util/LinkedHashSet;Lorg/hibernate/boot/registry/classloading/spi/ClassLoaderService;)
需要:4.3及以上的版本才能用StandardServiceRegistryBuilder() hibernate-core-4.3.11.Final.jar version:4.3 ServiceR ...
- ie6 ie7 userdata 本地存储 引发的惨案.
我使用 documentElement 作为userdata 作为本地存储的载体. document.documentElement.addBehavior("#default#userda ...
- python的N个小功能(图片预处理:打开图片,滤波器,增强,灰度图转换,去噪,二值化,切割,保存)
############################################################################################# ###### ...
- CyclicBarrier用法
CyclicBarrier和CountDownLatch一样,都是关于线程的计数器. 用法略有不同,测试代码如下: 1 public class TestCyclicBarrier { 2 3 pri ...
- elasticsearch使用More like this实现基于内容的推荐
基于内容的推荐通常是给定一篇文档信息,然后给用户推荐与该文档相识的文档.Lucene的api中有实现查询文章相似度的接口,叫MoreLikeThis.Elasticsearch封装了该接口,通过Ela ...
- PHP 字符串数组按照拼音排序的问题
拼音排序的规则: 字符串包括特殊字符.数字.英文字符.中文字符等等,排序结果要求,特殊字符排在第一梯队,将其按照首个字符ascii码表进行排序,数字字符排在第二梯队,将首个字符数字按照数字大小排序,英 ...
- 【LOJ6089】小Y的背包计数问题(动态规划)
[LOJ6089]小Y的背包计数问题(动态规划) 题面 LOJ 题解 神仙题啊. 我们分开考虑不同的物品,按照编号与\(\sqrt n\)的关系分类. 第一类:\(i\le \sqrt n\) 即需要 ...