CodeForces 628C Bear and String Distance (水题)

Bear and String Distance
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 usegets/scanf/printf instead of getline/cin/cout in C++, prefer to use BufferedReader/PrintWriter instead ofScanner/System.out in Java.

Input

The first line contains two integers n and k (1 ≤ n ≤ 1050 ≤ 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 .

Examples
input
4 26
bear
output
roar
input
2 7
af
output
db
input
3 1000
hey
output

-1

水题

看总共最多有多少可以修改的,然后之前的全部修改成差值最大的,后面的不变即可。

#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
typedef long long LL;
const int low(int x){ return x&-x; }
const int mod = 1e9 + 7;
const int maxn = 1e4 + 10;
int n, k;
char s[300005];
int main()
{
	int i, pr;
	while (scanf("%d%d%s",&n,&k, s) != EOF)
	{
		int l = strlen(s),ans=0;
		for (i = 0; i < l; i++)
		{
			ans += max(s[i] - 'a', 'z' - s[i]);
		}
		if (ans < k)
		{
			printf("-1\n");
			continue;
		}
		for (i = 0; i < l; i++)
		{
			int now;
			if (s[i] - 'a'>'z' - s[i])
			{
				now = s[i] - 'a';
				if (now >= k)  s[i] -= k,k=0;
				else s[i] = 'a', k -= now;
			}
			else
			{
				now = 'z' - s[i];
				if (now >= k) s[i] += k,k=0;
				else s[i] ='z', k -= now;
			}
		}
		printf("%s\n", s);
	}
}


你可能感兴趣的:(CodeForces 628C Bear and String Distance (水题))