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' —  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 (≤k≤n≤) – 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 -, if it is impossible to build the rocket at all.

题面看这里

题目很简单,主要是自己WA的有点离谱鸭!

先放上错误的代码吧

 #include <iostream>
#include <algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = ;
char s[maxn];
int n, m;
int main()
{
cin >> n >> m >> s;
sort(s, s + n);
int has = ;
int ans = s[] - 'a' + ;
for (int i = ; i < n;i++)
{
if(has==m) break;
if(s[i]-s[i-]>)//就错在这里!!
{
ans += s[i] - 'a' + ;
has++;
}
else
{
continue;
}
if(has==m) break;
}
if(has==m) cout << ans;
else cout << -;
return ;
}

直接把前后两个字符做比较了,改的时候加了一个数来记录上一次选择的那个字符。

AC代码

 #include <iostream>
#include <algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = ;
char s[maxn];
int n, m;
int main()
{
cin >> n >> m >> s;
//cout << s << endl;
sort(s, s + n);
int has = ;
int ans = s[] - 'a' + ;
int last = ;
for (int i = ; i < n;i++)
{
if(has==m) break;
if(s[i]-s[last]>)
{
ans += s[i] - 'a' + ;
has++;
last = i;
}
else
{
continue;
}
if(has==m) break;
}
if(has==m) cout << ans;
else cout << -;
return ;
}

附上自己错的一组后台数据

以后还是要认真点!

50 13
qwertyuiopasdfghjklzxcvbnmaaaaaaaaaaaaaaaaaaaaaaaa

Codeforces Round #499 (Div. 2) Problem-A-Stages(水题纠错)的更多相关文章

  1. Codeforces Round #367 (Div. 2) A. Beru-taxi (水题)

    Beru-taxi 题目链接: http://codeforces.com/contest/706/problem/A Description Vasiliy lives at point (a, b ...

  2. Codeforces Round #334 (Div. 2) A. Uncowed Forces 水题

    A. Uncowed Forces Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/604/pro ...

  3. Codeforces Round #353 (Div. 2) A. Infinite Sequence 水题

    A. Infinite Sequence 题目连接: http://www.codeforces.com/contest/675/problem/A Description Vasya likes e ...

  4. Codeforces Round #343 (Div. 2)【A,B水题】

    A. Far Relative's Birthday Cake 题意: 求在同一行.同一列的巧克力对数. 分析: 水题~样例搞明白再下笔! 代码: #include<iostream> u ...

  5. Codeforces Round #327 (Div. 2) A. Wizards' Duel 水题

    A. Wizards' Duel Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/591/prob ...

  6. Codeforces Round #146 (Div. 1) A. LCM Challenge 水题

    A. LCM Challenge 题目连接: http://www.codeforces.com/contest/235/problem/A Description Some days ago, I ...

  7. Codeforces Round #335 (Div. 2) B. Testing Robots 水题

    B. Testing Robots Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.codeforces.com/contest/606 ...

  8. Codeforces Round #335 (Div. 2) A. Magic Spheres 水题

    A. Magic Spheres Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.codeforces.com/contest/606/ ...

  9. Codeforces Round #306 (Div. 2) A. Two Substrings 水题

    A. Two Substrings Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/550/pro ...

  10. Codeforces Round #188 (Div. 2) A. Even Odds 水题

    A. Even Odds Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/318/problem/ ...

随机推荐

  1. javascript:void()的理解

    href="javascript:void(0);"本身没有任何危害啊,表示这是一个空链接.如果想在网页上用a标签,但又不想产生页面实际跳转动作,就可以这么做.下面是一些用法对比: ...

  2. 安装SSH2拓展 PHP上传文件到远程服务器

    情景:客户端上传图片到服务器A,服务器A同步上传至另外一个静态资源服务器B 环境:php7 linux(ubuntu) 安装php的ssh2扩展 -dev sudo apt-get install p ...

  3. 如何获得一个干净的 gnome 开发环境?

    下载 stage3-amd64-systemd-xxxxxxxx.tar.bz2 eselect profile set default/linux/amd64/17.0/desktop/gnome/ ...

  4. jQ无法设置checkbox变成选中状态

    设置以后checkbox并没有变成选中状态,用chrome调试看了一下,checkbox中确实有checked属性,针对这个问题,大家可以参考下本文 代码如下: $("input" ...

  5. mysql 用户及权限管理 允许远程连接

    mysq,功能强大的关系型数据库,它的用户管理在开发过程中当然也尤其重要,接下来就看看mysql的用户管理 1.登录数据库 mysql -uroot -p 回车 输入密码... 回车 2.登录成功后, ...

  6. spring cloud 使用Eureka作为服务注册中心

    什么是Eureka?  Eureka是在AWS上定位服务的REST服务. Eureka简单示例,仅作为学习参考 在pom文件引入相关的starter(起步依赖) /*定义使用的spring cloud ...

  7. Flink分布式缓存Distributed Cache

    1 分布式缓存 Flink提供了一个分布式缓存,类似于hadoop,可以使用户在并行函数中很方便的读取本地文件,并把它放在taskmanager节点中,防止task重复拉取. 此缓存的工作机制如下:程 ...

  8. 二、python基础之列表、元组

    一.列表 列表的概念: 列表由一系列按特定顺序排列的元素组成.你可以创建包含字母表中所有字母.数字0-9或所有家庭成员姓名的列表:也可以将任何东西加入列表中,其中的元素之间没有任何关系.鉴于列表通常包 ...

  9. IIS 部署网站本地可访问,外网无法访问

    1,检查防火墙入站规则,查看本地端口状态 cmd 命令:netstat -na 2:远程连接测试 cmd 命令:telnet IP Port ,如:telnet 127.0.0.1 135 ,连接成功 ...

  10. C#设计模式V2(1)——单例模式

    出自:https://www.cnblogs.com/zhili/p/SingletonPatterm.html 一.引言 最近在设计模式的一些内容,主要的参考书籍是<Head First 设计 ...