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. response.setHeader();小结

    response.setHeader():1. HTTP消息头 (1)通用信息头 即能用于请求消息中,也能用于响应信息中,但与被传输的实体内容没有关系的信息头,如Data,Pragma 主要: Cac ...

  2. VLAN基础配置及Access接口

    一.实验目的 二.实验拓扑图 三.实验编址 四.实验步骤 1.基础配置 按照PC1的方式依次配好IP 打开所有设备 用ping命令测试连通性 更改主机名称 2.创建VLAN 在S1上使用两条命令创建V ...

  3. 刚安装的程序要卸载,如何Ubuntu查看程序安装记录

    如果新装一个程序,突然发现需要卸载,又忘记了程序名字,怎么解决呢? /var/log/apt/history.log /var/log/apt/term.log /var/log/aptitude 看 ...

  4. rabbitmq-5-案例2-简单的案例+exchange

    Exchange交换机: sendMessage端,发送消息到Exchage1.2, 然后交换机通过路由键,将消息转发给队列queue中,最后客户端从队列中获取消息 交换属性: name:名称 typ ...

  5. 45.Sort List(链表排序)

    Level:   Medium 题目描述: Sort a linked list in O(n log n) time using constant space complexity. Example ...

  6. 【记录】解决uni-app 用nginx反向代理出现Invalid Host header问题

    之前解决过一次,后来给忘记了,今天又遇到这个问题,现记录一下 修改uni-app的manifest.json文件  - >源码视图 添加以下代码: "disableHostCheck& ...

  7. go语言从例子开始之Example10.map(字典)

    map 是 Go 内置关联数据类型(在一些其他的语言中称为哈希 或者字典 ) package main import "fmt" func main() { 要创建一个空 map, ...

  8. 转载:jQuery的deferred对象详解

    一.什么是deferred对象? 开发网站的过程中,我们经常遇到某些耗时很长的javascript操作.其中,既有异步的操作(比如ajax读取服务器数据),也有同步的操作(比如遍历一个大型数组),它们 ...

  9. BZOJ 2238: Mst DFS序+KDtree

    明明可以用二维数点来做啊,网上为什么都是树剖+线段树呢 ? code: #include <cstdio> #include <cstring> #include <al ...

  10. C++ 浅析调试,内存重叠查看

    这里举个例子查看内存, 环境为:vs 2017 测试为strcpy[因为测试老api,需要在 预处理中 添加 _CRT_SECURE_NO_WARNINGS ] 测试问题:内存溢出 源码: #incl ...