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. 测开之路五十三:unittest运行参数

    Fixture:进行测试前的准备工作和测试后的清理操作.例如创建临时或是代理数据库,目录,服务进程等.用例(Case):最小的测试单元,检车特定输入的响应.TestCase作为所有用例的基类,测试ca ...

  2. mybatis 查询一对一

    官方文档 Mapper接口 public interface UserMapper { User getUser(int userId); } public interface ArticleMapp ...

  3. CSS 6种完全居中最佳实践(整理)

    2016年4月28日 1.最佳法: .Absolute-Center { width: 50%; height: 50%; overflow: auto; margin: auto; position ...

  4. 转:inline-block 前世今生

    曾几何时,display:inline-block 已经深入「大街小巷」,随处可见 「display:inline-block; *display:inline; *zoom:1; 」这样的代码.如今 ...

  5. div中的div上下居中

    方法1: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 .parent {           width:800px;           height:500px;       ...

  6. Python - zipfile 乱码问题解决

    最近使用zipfile进行解包过程中遇到了很不舒服的问题,解包之后文件名是乱码的.下面进行简单总结: 首先,乱码肯定是因为解码方式不一样了,zipfile使用的是utf-8和cp437这两种编码方式, ...

  7. python介绍(变量,if,while)

    python介绍(变量,if,while): python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹 打发时间,决心开发一个新 ...

  8. 【问题解决方案】本地仓库删除远程库后添加到已有github仓库时仓库地址找不到的问题(github仓库SSH地址)

    参考: 我参考我自己.jpg 背景: 想添加一下远程库,github主页找了半天,Google搜索了半天,都没有找到,所以这里写一个,记录一下 1-格式分析:git@github.com:用户名/仓库 ...

  9. 17.Priority优先级

    /** * 优先级 */ public class PriorityDemo { public static class HightPriority extends Thread{ static in ...

  10. js 对象属性和使用方法学习笔记

    对象的引用 在ECMAScriipt中,不能访问对象的物理表示,只能访问对象的引用.每次创建对象,存储在变量中的都是对象的引用,而不是对象本身. 对象的废除 ECMASript拥有无用存储单元收集程序 ...