Codeforces Beta Round #91 (Div. 2 Only) D. Lucky Transformation
2016-08-19 15:00
477 查看
原题链接
D. Lucky Transformation
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7.
For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are
not.
Petya has a number consisting of n digits without leading zeroes. He represented it as an array of digits without leading
zeroes. Let's call it d. The numeration starts with 1,
starting from the most significant digit. Petya wants to perform the following operation k times:
find the minimum x (1 ≤ x < n) such
that dx = 4 and dx + 1 = 7,
if x is odd, then to assign dx = dx + 1 = 4,
otherwise to assign dx = dx + 1 = 7.
Note that if no x was found, then the operation counts as completed and the array doesn't change at all.
You are given the initial number as an array of digits and the number k. Help Petya find the result of completing k operations.
Input
The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 109) —
the number of digits in the number and the number of completed operations. The second line contains n digits without spaces representing the array of
digits d, starting with d1.
It is guaranteed that the first digit of the number does not equal zero.
Output
In the single line print the result without spaces — the number after the k operations are fulfilled.
Examples
input
output
input
output
Note
In the first sample the number changes in the following sequence: 4727447 → 4427447 → 4427477 → 4427447 → 4427477.
In the second sample: 4478 → 4778 → 4478.
447(第一个4为奇数位), 477(4为奇数位)在这两种方式下操作会无限循环
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <map>
#include <vector>
#include <cstring>
#define maxn 100005
using namespace std;
typedef long long ll;
int num[maxn];
int main(){
// freopen("in.txt", "r", stdin);
int n, k;
scanf("%d%d", &n, &k);
for(int i = 1; i <= n; i++)
scanf("%1d", num+i);
for(int i = 1; i < n && k; i++){
if(num[i] == 4 && num[i+1] == 7){
if(i % 2 == 0 && num[i-1] == 4){
if(k % 2){
num[i] = 7;
}
break;
}
if(i % 2 == 1 && i + 2 <= n && num[i+2] == 7){
if(k % 2){
num[i+1] = 4;
}
break;
}
if(i % 2 == 0)
num[i] = 7;
else
num[i+1] = 4;
k--;
}
}
for(int i = 1; i <= n; i++)
printf("%d", num[i]);
puts("");
return 0;
}
D. Lucky Transformation
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7.
For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are
not.
Petya has a number consisting of n digits without leading zeroes. He represented it as an array of digits without leading
zeroes. Let's call it d. The numeration starts with 1,
starting from the most significant digit. Petya wants to perform the following operation k times:
find the minimum x (1 ≤ x < n) such
that dx = 4 and dx + 1 = 7,
if x is odd, then to assign dx = dx + 1 = 4,
otherwise to assign dx = dx + 1 = 7.
Note that if no x was found, then the operation counts as completed and the array doesn't change at all.
You are given the initial number as an array of digits and the number k. Help Petya find the result of completing k operations.
Input
The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 109) —
the number of digits in the number and the number of completed operations. The second line contains n digits without spaces representing the array of
digits d, starting with d1.
It is guaranteed that the first digit of the number does not equal zero.
Output
In the single line print the result without spaces — the number after the k operations are fulfilled.
Examples
input
7 4 4727447
output
4427477
input
4 2 4478
output
4478
Note
In the first sample the number changes in the following sequence: 4727447 → 4427447 → 4427477 → 4427447 → 4427477.
In the second sample: 4478 → 4778 → 4478.
447(第一个4为奇数位), 477(4为奇数位)在这两种方式下操作会无限循环
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <map>
#include <vector>
#include <cstring>
#define maxn 100005
using namespace std;
typedef long long ll;
int num[maxn];
int main(){
// freopen("in.txt", "r", stdin);
int n, k;
scanf("%d%d", &n, &k);
for(int i = 1; i <= n; i++)
scanf("%1d", num+i);
for(int i = 1; i < n && k; i++){
if(num[i] == 4 && num[i+1] == 7){
if(i % 2 == 0 && num[i-1] == 4){
if(k % 2){
num[i] = 7;
}
break;
}
if(i % 2 == 1 && i + 2 <= n && num[i+2] == 7){
if(k % 2){
num[i+1] = 4;
}
break;
}
if(i % 2 == 0)
num[i] = 7;
else
num[i+1] = 4;
k--;
}
}
for(int i = 1; i <= n; i++)
printf("%d", num[i]);
puts("");
return 0;
}
相关文章推荐
- Codeforces Beta Round #75 (Div. 1 Only) B. Queue 线段树+二分
- Codeforces Beta Round #91 (Div. 2 Only)-E. Lucky Permutation
- Codeforces Beta Round #77 (Div. 2 Only)——A,B,C
- 【dfs找环】Codeforces Beta Round #80 (Div. 1 Only)——B. Cthulhu
- Codeforces Beta Round #78 (Div. 2 Only)——C Help Victoria the Wise
- Codeforces Beta Round #4 (Div. 2 Only) D. Mysterious Present 记忆化搜索
- Codeforces Beta Round #83 (Div. 1 Only) E.Darts 凸多边形面积交
- Codeforces Beta Round #25 (Div. 2 Only)D. Roads not only in Berland
- Codeforces Round #437 (Div. 2 C. Ordering Pizza 贪心 only two types of pizza
- 【CS Round #48 (Div. 2 only)】Game of Chance
- 【CS Round #39 (Div. 2 only) D】Seven-segment Display
- Codeforces Beta Round #6 (Div. 2 Only) E. Exposition
- Codeforces Beta Round #91 (Div. 1 Only) E. Lucky Array
- Codeforces Beta Round #75 (Div. 1 Only) A题
- Codeforces Beta Round #87 (Div. 1 Only)B Lawnmower(贪心)
- Codeforces Beta Round #83 (Div. 1 Only) E.Darts 凸多边形面积交
- Codeforces Beta Round #25 (Div. 2 Only)E. Test
- Codeforces Beta Round #4 (Div. 2 Only)
- Codeforces Round #437 (Div. 2 C. Ordering Pizza 贪心 only two types of pizza
- 【CS Round #39 (Div. 2 only) C】Reconstruct Sum