Educational Codeforces Round 8 C. Bear and String Distance 贪心
C. Bear and String Distance
题目连接:
http://www.codeforces.com/contest/628/problem/C
Description
Limak is a little polar bear. He likes nice strings — strings of length n, consisting of lowercase English letters only.
The distance between two letters is defined as the difference between their positions in the alphabet. For example, , and .
Also, the distance between two nice strings is defined as the sum of distances of corresponding letters. For example, , and .
Limak gives you a nice string s and an integer k. He challenges you to find any nice string s' that . Find any s' satisfying the given conditions, or print "-1" if it's impossible to do so.
As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use gets/scanf/printf instead of getline/cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.
Input
The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 106).
The second line contains a string s of length n, consisting of lowercase English letters.
Output
If there is no string satisfying the given conditions then print "-1" (without the quotes).
Otherwise, print any nice string s' that .
Sample Input
4 26
bear
Sample Output
roar
Hint
题意
现在定义了一个dis(a,b),dis(a,b) = abs(a-b),等于这两个数的ASCII码的距离
然后现在给你一个串,让你构造另外一个串,使得这两个串之间的距离和恰好等于k
题解:
贪心,我们暴力向最远的地方靠就好了
代码
#include<bits/stdc++.h>
using namespace std;
string s,s1;
int main()
{
int n,k;
cin>>n>>k;
cin>>s;
for(int i=0;i<s.size();i++)
{
int dis1 = 'z'-s[i];
int dis2 = s[i]-'a';
if(dis1>dis2)
{
int ddd = min(dis1,k);
k-=ddd;
s1+=s[i]+ddd;
}
else
{
int ddd = min(dis2,k);
k-=ddd;
s1+=s[i]-ddd;
}
}
if(k)return puts("-1");
else cout<<s1<<endl;
}
Educational Codeforces Round 8 C. Bear and String Distance 贪心的更多相关文章
- Educational Codeforces Round 9 C. The Smallest String Concatenation —— 贪心 + 字符串
题目链接:http://codeforces.com/problemset/problem/632/C C. The Smallest String Concatenation time limit ...
- [Educational Codeforces Round 16]E. Generate a String
[Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...
- Educational Codeforces Round 16 E. Generate a String dp
题目链接: http://codeforces.com/problemset/problem/710/E E. Generate a String time limit per test 2 seco ...
- Educational Codeforces Round 9 C. The Smallest String Concatenation 排序
C. The Smallest String Concatenation 题目连接: http://www.codeforces.com/contest/632/problem/C Descripti ...
- Educational Codeforces Round 8 F. Bear and Fair Set 最大流
F. Bear and Fair Set 题目连接: http://www.codeforces.com/contest/628/problem/F Description Limak is a gr ...
- Educational Codeforces Round 16 E. Generate a String (DP)
Generate a String 题目链接: http://codeforces.com/contest/710/problem/E Description zscoder wants to gen ...
- Educational Codeforces Round 40 I. Yet Another String Matching Problem
http://codeforces.com/contest/954/problem/I 给你两个串s,p,求上一个串的长度为|p|的所有子串和p的差距是多少,两个串的差距就是每次把一个字符变成另一个字 ...
- Educational Codeforces Round 9 C. The Smallest String Concatenation(字符串排序)
You're given a list of n strings a1, a2, ..., an. You'd like to concatenate them together in some or ...
- Educational Codeforces Round 62 (Rated for Div. 2) C 贪心 + 优先队列 + 反向处理
https://codeforces.com/contest/1140/problem/C 题意 每首歌有\(t_i\)和\(b_i\)两个值,最多挑选m首歌,使得sum(\(t_i\))*min(\ ...
随机推荐
- 超级rtmp服务器和屌丝wowza
超级rtmp服务器和屌丝wowza http://blog.csdn.net/win_lin/article/details/11927973
- 动画基础--基于Core Animation(3)
参考:https://zsisme.gitbooks.io/ios-/content/ 前面的文章动画基础--基于Core Animation(1),动画基础--基于Core Animation(2) ...
- [bugfix]copy属性参数将NSMutableArray变为NSArray类型
问题:NSMutableArray 声明为 copy 属性参数后即使接受NSMutableArray变量依然为NSArray变量 测试: 属性申明为: 1 @property (nonatomic, ...
- mongo数据库基本操作--python篇
连接数据库 MongoClient VS Connection class MongoClient(pymongo.common.BaseObject) | Connection to MongoDB ...
- python_迭代器和生成器
迭代器和生成器 1.迭代器 特点: 访问者不需要关心迭代器内部的结构,仅需通过next()方法不断去取下一个内容 不能随机访问集合中的某个值 ,只能从头到尾依次访问 访问到一半时不能往回退 便于循环比 ...
- Freemarker中JS取Data-model的值的问题
不能在表达式中使用js变量,比如: var key = 1; var value = "${stateMap[key]}"; 因为Freemarker是在服务器端进行解析的,此时j ...
- 配置文件中的mime-mapping元素(ofbiz/framework/catalina/config/mime-type)(
mime-mapping元素将mime类型映射到扩展名. extension元素用来描述扩展名.mime-type元素则为MIME类型. <?xml version="1.0" ...
- 微信小程序 - video组件poster无效 / 视频播放列表
在做有关微信小程序有关视频播放页面的时候,遇到video组件设置poster无效果,然后查了下poster属性:视频封面的图片网络资源地址,如果 controls 属性值为 false 则设置 pos ...
- Android----APP性能优化
性能优化的目标 快 如何让 app 在运行过程过不卡顿,运行流畅,速度快,也就是说如何解决卡顿呢?我们先看看那些因素影响卡顿? UI,包括ui的绘制,刷新等 启动,包括冷启动,热启动,温启动等 跳转, ...
- Ubuntu 虚拟机增强包下载
https://www.virtualbox.org/wiki/Download_Old_Builds_5_1 Extension Pack All Platforms 安装方法详情:https:// ...