sort排序或者set的应用

Stages(水题)

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 nn stages available. The rocket must contain exactly kk 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’ — 2626 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.
娜塔莎将飞往火星。她需要建造一个火箭,它由几个阶段按某种顺序组成。每个阶段都由小写的拉丁字母定义。这样,火箭就可以用字符串连接的字母来描述,这些字母对应于各个阶段。
有nn个阶段可用。火箭必须包含其中的kk。火箭的级应按重量排序。所以,在用了一些字母之后,只能用一个字母来表示,这至少是字母表中后面的两个位置(中间跳过一个字母,甚至更多)。例如,字母“c”不能字母“a”、“b”、“c”和“d”,但可以字母“e”、“f”、“f”。“z”。为了让火箭飞得尽可能远,它的重量应该是最小的。火箭的重量等于它各阶段的重量之和。舞台的重量是字母表中字母的数量。例如,a级重1吨,b级重2吨,z级重26吨。用最小的重量或确定的方法建造火箭,根本不可能造火箭。每个阶段最多可以使用一次。

Input
The first line of input contains two integers — nn and kk (1≤k≤n≤501≤k≤n≤50) – the number of available stages and the number of stages to use in the rocket.

The second line contains string ss, which consists of exactly nn 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.
输入的第一行包含两个整数- nn和kk k(1≤≤n≤501 k≤≤n≤50)——数量的阶段和阶段中使用火箭的数量。
第二行包含字符串ss,它由n个小写拉丁字母组成。每个字母都定义了一个新的阶段,可以用来建造火箭。每个阶段最多可以使用一次。


Output
Print a single integer — the minimal total weight of the rocket or -1, if it is impossible to build the rocket at all.
打印一个整数-火箭的最小总重量或-1,如果根本不可能建造火箭。


Examples
Input
5 3
xyabd
Output
29
Input
7 4
problem
Output
34
Input
2 2
ab
Output
-1
Input
12 1
abaabbaaabbb
Output
1


例子解释
In the first example, the following rockets satisfy the condition:
“adx” (weight is 1 + 4 + 24 = 291 + 4 + 24 = 29 );
“ady” (weight is 1 + 4 + 25 = 301 + 4 + 25 = 30 );
“bdx” (weight is 2 + 4 + 24 = 302 + 4 + 24 = 30 );
“bdy” (weight is 2 + 4 + 25 = 312 + 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 = 342 + 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.


一:使用sort

#include<stdio.h>
#include<cstdio>
#include<string>
#include<string.h>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
#include<math.h>
using namespace std;
//const int MAX=60000;
char a[55];
int main()
{   int n,k,m,i,sum,t,j;
    scanf("%d %d",&n,&k);
    m=0;
    t=1;
    getchar();
    scanf("%s",a);
    m=strlen(a);
    sort(a,a+m);
    sum=a[0]-'a'+1;
    if(k==1)
    {
        printf("%d\n",sum);
        return 0;
    }
    for(i=1;i<m;i++)
    {
        if(a[i]-a[0]>1) //更新 
        {   sum+=a[i]-'a'+1;
            a[0]=a[i];
            t++;
            if(t==k)
            {   printf("%d\n",sum);
                return 0;
            }
        }
        else
        {

        }
    }
    printf("-1\n");
    return 0;
 } 

二使用set

#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
#include<set>
char a[100003],b[100002];
int main()
{

    int n,m,i,j,k,mx=1,sum;
    set<char>s;
    set<char>::iterator in;
    scanf("%d %d\n",&n,&m);
    gets(a);
    for(i=0;i<n;i++)
    {
        s.insert(a[i]);
    }
    j=0;
    for(in=s.begin();in!=s.end();in++)
        b[j++]=*in;
        sum=b[0]-'a'+1;
        if(m==1)
            {
                printf("%d\n",sum);
                    return 0;
            }
            for(k=1;k<j;k++)
            {
                if(b[0]<=b[k]-2)
                {
                    b[0]=b[k];
                    mx++;
                    sum+=(b[0]-'a'+1);
                }
                if(mx==m)
                {
                    printf("%d\n",sum);
                    return 0;
                }
            }
            printf("-1\n");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/shf1730797676/article/details/81260871