Codeforces Round #499 (Div. 2) Problem-A-Stages(水题纠错)
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(水题纠错)的更多相关文章
- Codeforces Round #367 (Div. 2) A. Beru-taxi (水题)
Beru-taxi 题目链接: http://codeforces.com/contest/706/problem/A Description Vasiliy lives at point (a, b ...
- 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 ...
- Codeforces Round #353 (Div. 2) A. Infinite Sequence 水题
A. Infinite Sequence 题目连接: http://www.codeforces.com/contest/675/problem/A Description Vasya likes e ...
- Codeforces Round #343 (Div. 2)【A,B水题】
A. Far Relative's Birthday Cake 题意: 求在同一行.同一列的巧克力对数. 分析: 水题~样例搞明白再下笔! 代码: #include<iostream> u ...
- 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 ...
- Codeforces Round #146 (Div. 1) A. LCM Challenge 水题
A. LCM Challenge 题目连接: http://www.codeforces.com/contest/235/problem/A Description Some days ago, I ...
- 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 ...
- 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/ ...
- 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 ...
- 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/ ...
随机推荐
- apache 安装配置 (centos)
1. 使用yum包安装Apache软件 [root@Apache ~]# yum -y install httpd* [root@Apache ~]# rpm -qa | grep httpd --查 ...
- 安装vue开发环境→安装淘宝镜像的时候报错
问题: npm WARN deprecated socks@1.1.10: If using 2.x branch, please upgrade to at least 2.1.6 to avoid ...
- python函数装饰器详解
python装饰器(fuctional decorators)简单来说就是修改其他函数的函数. 这样的函数需要满足两个个条件: 1.不能修改原函数的源代码 2.不能改变原函数的调用方式 需要达到的效果 ...
- ubantu18.04中mysql8.0设置远程连接的问题
在mysql8.0中的配置文件中默认是没有绑定地址的,但是可以自己配置,在my.cnf中 这里使用另一种方式: 首先先连接到自己的数据库执行: use mysql; select host,user ...
- SpringBoot集成Swagger,Postman,newman,jenkins自动化测试.
环境:Spring Boot,Swagger,gradle,Postman,newman,jenkins SpringBoot环境搭建. Swagger简介 Swagger 是一款RESTFUL接口的 ...
- Python之str型转成int型
str转int: def fn(x,y): return x*10+y def char2num(s): ':9}[s] # 特别注意这里,后面还有个 [s] ')))) '))) 输出如下: < ...
- picker多级选择器的使用————小程序
picker多级选择器的使用----小程序 picker是选择器来着,既然选择了,就希望可以获取选择的数据. index.html <view>picker获取数据</view> ...
- 【记录】解决uni-app 用nginx反向代理出现Invalid Host header问题
之前解决过一次,后来给忘记了,今天又遇到这个问题,现记录一下 修改uni-app的manifest.json文件 - >源码视图 添加以下代码: "disableHostCheck& ...
- oracle10G rac 10.2.0.1升级10.2.0.4
前言 ocr版本查询指令: ocrcheck vote盘路径查询指令: crsctl query css votedisk 相关指令参考来源:http://hzhg12345.blog.163.co ...
- github gist 查看html
gist GitHub Gist 指南 https://blog.csdn.net/yz18931904/article/details/80482166 通过修改hosts解决gist.github ...