CodeForces - 1042B

Posted daybreaking

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 n(1n1000)(1≤n≤1000) — the number of juices.

Each of the next nn lines contains an integer cici (1ci100000)(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

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".

 

你猜我的代码错在哪了,找找看?

#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(1);
const int N = 100009;


int main()
{
    char kind[5];
    LL i,p,j;
    LL n,x;
    LL a,b,c,ab,ac,bc,abc;
    LL flag=0;
    a=b=c=ab=ac=bc=abc=100009;
    scanf("%lld",&n);
    for(i=1;i<=n;i++)
    {
        scanf("%lld %s",&x,kind);
        if((strcmp(kind,"BA")==0)||(strcmp(kind,"AB")==0))
        {
            if(x<ab)
                ab=x;
        }
        else if((strcmp(kind,"AC")==0)||(strcmp(kind,"CA")==0))
        {
            if(x<ac)
                ac=x;
        }
        else if((strcmp(kind,"BC")==0)||(strcmp(kind,"CB")==0))
        {
            if(x<bc)
                bc=x;
        }
        else if(kind[0]==A)
        {
            flag++;
            if(x<a)
                a=x;
        }
        else if(kind[0]==B)
        {
            flag++;
            if(x<b)
                b=x;
        }
        else if(kind[0]==C)
        {
            flag++;
            if(x<c)
                c=x;
        }
        else
        {
            if(x<abc)
                abc=x;
        }
        kind[0]=0;
    }

    LL head=99999999999;
    if(a<100009&&b<100009&&c<100009)
        head=a+b+c;
    if(bc<100009&&a<100009&&(a+bc<head))
        head=a+bc;
    if(abc<100009&&a<100009&&(a+abc<head))
        head=a+abc;
    if(abc<100009&&b<100009&&(b+abc<head))
        head=b+abc;
    if(abc<100009&&c<100009&&(c+abc<head))
        head=c+abc;
    if(ac<100009&&b<100009&&(b+ac<head))
        head=b+ac;
    if(ab<100009&&c<100009&&(c+ab<head))
        head=c+ab;
    if(ac<100009&&ab<100009&&(ac+ab<head))
        head=ac+ab;
    if(ab<100009&&bc<100009&&(ab+bc<head))
        head=ab+bc;
    if(ac<100009&&bc<100009&&(ac+bc<head))
        head=ac+bc;
    if(abc<100009&&abc<head)
        head=abc;

    if(head!=99999999999)
        printf("%lld
",head);
    else
        printf("-1
");
    return 0;

}

if else if else if if if .......因为这种问题错过几遍了,心里没有点Number  B 吗?!!

明明"ABC"的串还没有比过,你就直接比较kind[0]???  脑子呢???

写if else 的时候能不能心里想着逻辑点,能不能走点心?

 

正确代码:

技术分享图片
  1 #include<cstdio>
  2 #include<cstdlib>
  3 #include<cstring>
  4 #include<string>
  5 #include<cmath>
  6 #include<algorithm>
  7 #include<queue>
  8 #include<stack>
  9 #include<deque>
 10 #include<map>
 11 #include<iostream>
 12 using namespace std;
 13 typedef long long  LL;
 14 const double pi=acos(-1.0);
 15 const double e=exp(1);
 16 const int N = 100009;
 17 
 18 
 19 int main()
 20 {
 21     char kind[5];
 22     LL i,p,j;
 23     LL n,x;
 24     LL a,b,c,ab,ac,bc,abc;
 25     LL flag=0;
 26     a=b=c=ab=ac=bc=abc=100009;
 27     scanf("%lld",&n);
 28     for(i=1;i<=n;i++)
 29     {
 30         scanf("%lld %s",&x,kind);
 31         if((strcmp(kind,"BA")==0)||(strcmp(kind,"AB")==0))
 32         {
 33             if(x<ab)
 34                 ab=x;
 35         }
 36         else if((strcmp(kind,"AC")==0)||(strcmp(kind,"CA")==0))
 37         {
 38             if(x<ac)
 39                 ac=x;
 40         }
 41         else if((strcmp(kind,"BC")==0)||(strcmp(kind,"CB")==0))
 42         {
 43             if(x<bc)
 44                 bc=x;
 45         }
 46         else if(strcmp(kind,"A")==0)
 47         {
 48             flag++;
 49             if(x<a)
 50                 a=x;
 51         }
 52         else if(strcmp(kind,"B")==0)
 53         {
 54             flag++;
 55             if(x<b)
 56                 b=x;
 57         }
 58         else if(strcmp(kind,"C")==0)
 59         {
 60             flag++;
 61             if(x<c)
 62                 c=x;
 63         }
 64         else
 65         {
 66             if(x<abc)
 67                 abc=x;
 68         }
 69         kind[0]=0;
 70     }
 71 
 72     LL head=99999999999;
 73     if(a<100009&&b<100009&&c<100009)
 74         head=a+b+c;
 75     if(bc<100009&&a<100009&&(a+bc<head))
 76         head=a+bc;
 77     if(abc<100009&&a<100009&&(a+abc<head))
 78         head=a+abc;
 79     if(abc<100009&&b<100009&&(b+abc<head))
 80         head=b+abc;
 81     if(abc<100009&&c<100009&&(c+abc<head))
 82         head=c+abc;
 83     if(ac<100009&&b<100009&&(b+ac<head))
 84         head=b+ac;
 85     if(ab<100009&&c<100009&&(c+ab<head))
 86         head=c+ab;
 87     if(ac<100009&&ab<100009&&(ac+ab<head))
 88         head=ac+ab;
 89     if(ab<100009&&bc<100009&&(ab+bc<head))
 90         head=ab+bc;
 91     if(ac<100009&&bc<100009&&(ac+bc<head))
 92         head=ac+bc;
 93     if(abc<100009&&abc<head)
 94         head=abc;
 95 
 96     if(head!=99999999999)
 97         printf("%lld
",head);
 98     else
 99         printf("-1
");
100     return 0;
101 
102 }
View Code

 




















以上是关于CodeForces - 1042B的主要内容,如果未能解决你的问题,请参考以下文章

[Codeforces Round #522 (Div. 2, based on Technocup 2019 Elimination Round 3)][C. Playing Piano](代码片段

c_cpp Codeforces片段

Codeforces 86C Genetic engineering(AC自动机+DP)

CodeForces 1005D Polycarp and Div 3(思维贪心dp)

(Incomplete) Codeforces 394 (Div 2 only)

CodeForces 931F Teodor is not a liar!