贪心

A. Stages
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Natasha is going to fly to Mars. She needs to build a rocket, which consists of several stages in some order. Each of the stages is defined by a lowercase Latin letter. This way, the rocket can be described by the string — concatenation of letters, which correspond to the stages.

There are
n
stages available. The rocket must contain exactly
k
of them. Stages in the rocket should be ordered by their weight. So, after the stage with some letter can go only stage with a letter, which is at least two positions after in the alphabet (skipping one letter in between, or even more). For example, after letter ‘c’ can’t go letters ‘a’, ‘b’, ‘c’ and ‘d’, but can go letters ‘e’, ‘f’, …, ‘z’.

For the rocket to fly as far as possible, its weight should be minimal. The weight of the rocket is equal to the sum of the weights of its stages. The weight of the stage is the number of its letter in the alphabet. For example, the stage ‘a ‘weighs one ton,’ b ‘weighs two tons, and’ z’ —
26
tons.

Build the rocket with the minimal weight or determine, that it is impossible to build a rocket at all. Each stage can be used at most once.

Input
The first line of input contains two integers —
n
and
k
(
1

k

n

50
) – the number of available stages and the number of stages to use in the rocket.

The second line contains string
s
, which consists of exactly
n
lowercase Latin letters. Each letter defines a new stage, which can be used to build the rocket. Each stage can be used at most once.

Output
Print a single integer — the minimal total weight of the rocket or -1, if it is impossible to build the rocket at all.

Examples
inputCopy
5 3
xyabd
outputCopy
29
inputCopy
7 4
problem
outputCopy
34
inputCopy
2 2
ab
outputCopy
-1
inputCopy
12 1
abaabbaaabbb
outputCopy
1
Note
In the first example, the following rockets satisfy the condition:

“adx” (weight is
1
+
4
+

24

29
);
“ady” (weight is
1
+
4
+

25

30
);
“bdx” (weight is
2
+
4
+

24

30
);
“bdy” (weight is
2
+
4
+

25

31
).
Rocket “adx” has the minimal weight, so the answer is
29
.

In the second example, target rocket is “belo”. Its weight is
2
+
5
+
12
+

15

34
.

In the third example,

n

k

2
, so the rocket must have both stages: ‘a’ and ‘b’. This rocket doesn’t satisfy the condition, because these letters are adjacent in the alphabet. Answer is -1.

题意是说a到z每个字符的值是1到26,给一串字符串,让我们从其中取指定数字的字符,使得这个字符的值是所取的指定的数字所有字符中值最小的,这个取法规定是相邻的不能取,比如说a后面有bc,只能取c,不能取b,另一个要求是不能取这个字母之前的数,例如bac,b之后只能取c,不能取a;
思路:
先从小到大排好序,(第二个要求就满足了)第一个字母肯定是要算上的,往后只要不是和这个数相邻的就行
自己的思路差不多,但是我纠结在了不能相邻上了,没有从大方向上看这个题,思路很乱,不能相邻,其实就对与自己选定的这个不能相邻就是了

#include<algorithm>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int a[60];
int main()
{
    int n,k;
    scanf("%d %d\n",&n,&k);
    for(int i=1;i<=n;i++)
    {
       char ch;
       scanf("%c",&ch);
       a[i]=ch-'a'+1;
    }
    sort(a+1,a+n+1);
    int pre=-1,sum=0;
    for(int i=1;i<=n;i++)
    {
        if(pre==-1)
        {
            pre=a[i];
            sum+=a[i];
            k--;
        }//把第一个放进去
        else
        {
            if(a[i]-pre>=2)//不能与选定的相邻,这里选定的就是赋值给pre的,不能是a[i-1];
            {
                sum+=a[i];
                pre=a[i];//切换
                k--;
            }
        }
        if(k==0)break;
    }
    if(k==0)printf("%d\n",sum);
    else printf("-1\n");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值