Codeforces 1042 B

博客围绕Codeforces 1042B题目展开,该题涉及三种维生素A、B、C,Berland店有多种含不同维生素组合的果汁,需计算Petya摄入三种维生素的最小花费。给出输入输出要求、示例及大意,最后提到博主AC的代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

B. Vitamins Codeforces 1042B
time limit per test:2 seconds
memory limit per test:256 megabytes
inputstandard input
outputstandard output
Berland shop sells n kinds of juices. Each juice has its price ci. 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 (1≤n≤1000) — the number of juices.

Each of the next n lines contains an integer ci (1≤ci≤100000) and a string si — the price of the i-th juice and the vitamins it contains. String si contains from 1 to 3 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 si. The order of letters in strings si 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=15 and obtains all three vitamins. He can also buy just the third juice and obtain three vitamins, but its cost is 16, which isn’t optimal.
In the second example Petya can’t obtain all three vitamins, as no juice contains vitamin “C”.
大意:一共有三种元素A,B,C。一瓶饮料中含有一种以上(含一种)的元素。Petya要购买这种饮料来摄入这三种元素,现在需要求出Petya每天摄入这三种元素所花费的最小金额是多少。
下面是鶸(指我)AC的代码。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<queue>
#include<cmath>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=100000+10;
int main()
{
    int n,x,ans;
    int v[7];
    char y[5];
    while(scanf("%d",&n)!=EOF)
    {
        ans=INF;
        memset(v,INF,sizeof(v));
        for(int i=0;i<n;i++)
        {
            scanf("%d %s",&x,&y);
            int l=strlen(y);
            sort(y,y+l);
            if(y[0]=='A'&&y[1]=='B'&&y[2]=='C'&&v[6]>x)
                v[6]=x;
            else if(y[0]=='B'&&y[1]=='C'&&v[5]>x)
                v[5]=x;
            else if(y[0]=='A'&&y[1]=='C'&&v[4]>x)
                v[4]=x;
            else if(y[0]=='A'&&y[1]=='B'&&v[3]>x)
                v[3]=x;
            else if(y[0]=='C'&&v[2]>x)
                v[2]=x;
            else if(y[0]=='B'&&v[1]>x)
                v[1]=x;
            else if(y[0]=='A'&&v[0]>x)
                v[0]=x;
        }
        if(v[0]!=INF&&v[1]!=INF&&v[2]!=INF&&ans>(v[0]+v[1]+v[2]))
            ans=v[0]+v[1]+v[2];
        if(v[0]!=INF&&v[5]!=INF&&ans>(v[0]+v[5]))
            ans=v[0]+v[5];
        if(v[1]!=INF&&v[4]!=INF&&ans>(v[1]+v[4]))
            ans=v[1]+v[4];
        if(v[2]!=INF&&v[3]!=INF&&ans>(v[2]+v[3]))
            ans=v[2]+v[3];
        if(v[3]!=INF&&v[4]!=INF&&ans>(v[3]+v[4]))
            ans=v[3]+v[4];
        if(v[3]!=INF&&v[5]!=INF&&ans>(v[3]+v[5]))
            ans=v[3]+v[5];
        if(v[5]!=INF&&v[4]!=INF&&ans>(v[5]+v[4]))
            ans=v[5]+v[4];
        if(v[6]!=INF&&ans>v[6])
            ans=v[6];
        if(ans==INF)
            printf("-1\n");
        else
            printf("%d\n",ans);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值