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. 自动化测试常用断言的使用方法(python)-(转载@zhuquan0814

    自动化测试中寻找元素并进行操作,如果在元素好找的情况下,相信大家都可以较熟练地编写用例脚本了,但光进行操作可能还不够,有时候也需要对预期结果进行判断. 这里介绍几个常用断言的使用方法,可以一定程度上帮 ...

  2. build temu error about SDL

    1. 安装sdl2 sudo apt-get install libsdl2-dev 2. 将configure文件中与SDL有关的地方改成SDL2 if test -z "$sdl&quo ...

  3. Raft Paper 简译

      本文是对 Raft Paper 核心部分的意译,不包括原文中的如下章节:<3 Paxos 的优缺点论述>.<4 Raft 的易理解性介绍>.<9 Raft 算法的易理 ...

  4. 发送xml数据

  5. PostgreSQL的学习-序列

    序列对象(也叫序列生成器)都是用CREATE SEQUENCE创建的特殊的单行表.一个序列对象通常用于为行或者表生成唯一的标识符.下面序列函数,为我们从序列对象中获取最新的序列值提供了简单和并发读取安 ...

  6. 使用ubuntu的一些操作笔记20191203

    前言 环境: virtualbox + Ubuntu 16.04 情况: 可以进入虚拟机中Ubuntu系统的桌面,但是外部可以访问到 ssh,输入正确的用户名和密码无法登录 无法正常启动 Apache ...

  7. pip飞起来了

    这里说下Windows下的修改方法,看了网上很多的教程发现都不行,尝试了好久终于发现了可行的方法. 找到python安装目录下的:\Lib\site-packages\pip\models\index ...

  8. python print 连续输出变量加字符串

    a=1 b=2 print(a,'+',b,'=',a+b) 输出:1+2=3

  9. Vue项目中导入excel文件读取成js数组

    1. 安装组件 cnpm install xlsx --save 2. 代码 <template> <span> <input class="input-fil ...

  10. spring boot 依赖配置

    虽然springboot号称是零配置 配置文件确实不需要,但是 依赖还是要有的 <parent>        <groupId>org.springframework.boo ...