文章

246

粉丝

0

获赞

1179

访问

74.6k

头像
排序后贪心即可:
P1683 中南大学机试题
发布于2026年3月23日 16:49
阅读数 222

#include<iostream>
#include<algorithm>
using namespace std;

const int inf=0x3f3f3f3f;

int n,k;
string s;

int main()
{
    while(cin>>n>>k)
    {
        cin>>s;
        int res=inf;
        sort(s.begin(),s.end());
        for(int i=0;i<s.size();i++)
        {
            int w=s[i]-'a'+1,cnt=1,pre=i;
            for(int j=i+1;j<s.size();j++)
            {
                if(s[j]-s[pre]>=2)
                {
                    cnt++;
                    w+=s[j]-'a'+1;
                    pre=j;
                    if(cnt==k)
                    break;
                }
            }
            if(cnt==k)
            res=min(res,w);
        }
        if(res==inf)
        puts("-1");
        else
        cout<<res<<endl;
    }
    return 0;
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发