zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    jQuery禁用或启用
    ASP.NET MVC一次删除多笔记录
    在你的ASP.NET MVC中使用查找功能
    Get radio selected value
    绑定一个值给radio
    ASP.NET MVC实现权限控制
    为Guid数据类型的属性(property)赋值
    Razor语法中绑定一个值给checkbox
    判断IEnumerable<T>集合中是否包含有T对象
    SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5204845.html
Copyright © 2011-2022 走看看