C. Efim and Strange Grade

Efim just received his grade for the last test. He studies in a special school and his grade can be equal to any positive decimal fraction. First he got disappointed, as he expected a way more pleasant result. Then, he developed a tricky plan. Each second, he can ask his teacher to round the grade at any place after the decimal point (also, he can ask to round to the nearest integer).

There are t seconds left till the end of the break, so Efim has to act fast. Help him find what is the maximum grade he can get in no more than t seconds. Note, that he can choose to not use all t seconds. Moreover, he can even choose to not round the grade at all.

In this problem, classic rounding rules are used: while rounding number to the n-th digit one has to take a look at the digit n + 1. If it is less than 5 than the n-th digit remain unchanged while all subsequent digits are replaced with 0. Otherwise, if the n + 1 digit is greater or equal to 5, the digit at the position n is increased by 1 (this might also change some other digits, if this one was equal to 9) and all subsequent digits are replaced with 0. At the end, all trailing zeroes are thrown away.

For example, if the number 1.14 is rounded to the first decimal place, the result is 1.1, while if we round 1.5 to the nearest integer, the result is 2. Rounding number 1.299996121 in the fifth decimal place will result in number 1.3.

Input

The first line of the input contains two integers n and t (1 ≤ n ≤ 200 000, 1 ≤ t ≤ 109) — the length of Efim's grade and the number of seconds till the end of the break respectively.

The second line contains the grade itself. It's guaranteed that the grade is a positive number, containing at least one digit after the decimal points, and it's representation doesn't finish with 0.

Output

Print the maximum grade that Efim can get in t seconds. Do not print trailing zeroes.

Examples
input
6 1
10.245
output
10.25
input
6 2
10.245
output
10.3
input
3 100
9.2
output
9.2
Note

In the first two samples Efim initially has grade 10.245.

During the first second Efim can obtain grade 10.25, and then 10.3 during the next second. Note, that the answer 10.30 will be considered incorrect.

In the third sample the optimal strategy is to not perform any rounding at all.

*******************************************************************

Solution:

  Here is a simple solution for this implementation problem. Just reducing unused functions and mining processes to avoid TLE. Even it's third problem but it is really simple for stronger me!

  For the trick , Just recognizing that there is no need loop the carrying position for every second t, because if u carried in pos i , then u can insure that from 0 to i-1 has no element greater than '4'! So the next carry pos may be i -1, if not thus can conclude that carrying work is done!

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <stdlib.h> using namespace std; int n, t;
string grade; void numPlusOne(string & integer)
{
int car = ;
for(int i = integer.length() - ; i >= ; i --){
if(integer[i] == ''){
integer[i] = '';
}else{
integer[i] ++;
car = ;
break;
}
}
if(car){
integer = "" + integer;
}
}
int main()
{
cin>>n>>t;
cin>>grade;
int pos = ;
for(int i = ; i < grade.length(); i ++){
if(grade[i] == '.'){
pos = i;
break;
}
}
string integer =grade.substr(, pos);
string dec = grade.substr(pos + , grade.length() - pos - );
bool integerPlusOne = false;
int len = dec.length();
int carPos = - ;
for(int i = ; i < len; i ++){
if(dec[i] >= ''){
carPos = i;
break;
}
}
bool haveDec = true;
while(t --){
if(carPos == -){
break;
}
else if(carPos == ){
integerPlusOne = true;
haveDec = false;
break;
}else{
dec[carPos - ] ++;
len = carPos;
if(dec[carPos - ] < '') carPos = -;
else carPos --;
}
}
if(integerPlusOne){
numPlusOne(integer);
}
if(!haveDec){
cout<<integer<<endl;
}else{
cout<<integer<<".";
for(int i = ; i < len; i ++)
printf("%c", dec[i]);
}
return ;
}

【Codeforces】Codeforces Round #373 (Div. 2) -C的更多相关文章

  1. 【Codeforces】CF Round #592 (Div. 2) - 题解

    Problem - A Tomorrow is a difficult day for Polycarp: he has to attend \(a\) lectures and \(b\) prac ...

  2. 【二分】CF Round #587 (Div. 3)E2 Numerical Sequence (hard version)

    题目大意 有一个无限长的数字序列,其组成为1 1 2 1 2 3 1.......1 2 ... n...,即重复的1~1,1~2....1~n,给你一个\(k\),求第\(k(k<=10^{1 ...

  3. Codeforces Round #373 (Div. 2)A B

    Codeforces Round #373 (Div. 2) A. Vitya in the Countryside 这回做的好差啊,a想不到被hack的数据,b又没有想到正确的思维 = = [题目链 ...

  4. Codeforces Round #373 (Div. 1)

    Codeforces Round #373 (Div. 1) A. Efim and Strange Grade 题意 给一个长为\(n(n \le 2 \times 10^5)\)的小数,每次可以选 ...

  5. 【题解】Codeforces 961G Partitions

    [题解]Codeforces 961G Partitions cf961G 好题啊哭了,但是如果没有不小心看了一下pdf后面一页的提示根本想不到 题意 已知\(U=\{w_i\}\),求: \[ \s ...

  6. Codeforces 716A Crazy Computer 【模拟】 (Codeforces Round #372 (Div. 2))

    A. Crazy Computer time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  7. Codeforces 716B Complete the Word【模拟】 (Codeforces Round #372 (Div. 2))

    B. Complete the Word time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  8. 【Codeforces】Codeforces Round #551 (Div. 2)

    Codeforces Round #551 (Div. 2) 算是放弃颓废决定好好打比赛好好刷题的开始吧 A. Serval and Bus 处理每个巴士最早到站且大于t的时间 #include &l ...

  9. 【codeforces】Codeforces Round #277 (Div. 2) 解读

    门户:Codeforces Round #277 (Div. 2) 486A. Calculating Function 裸公式= = #include <cstdio> #include ...

随机推荐

  1. css3的基础知识

    transfrom的应用: 1.旋转:transform: rotate(30deg): 2.阴影效果:box-shadow: 10px 10px 5px #888888: 3.鼠标移入放大:tran ...

  2. 详解proxy_pass、upstream与resolver

    详解proxy_pass.upstream与resolver boldcautious 关注 2018.06.04 10:48 字数 1204 阅读 1434评论 0喜欢 2 应用场景 这里列举几个应 ...

  3. 【剑指Offer】66、机器人的运动范围

      题目描述:   地上有一个m行和n列的方格.一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子. 例如,当k为18时 ...

  4. [forward] cmake, CMakeLists.txt梳理

    cmake intro 原文请见 cmake使用总结(转)-工程主目录CMakeList文件编写 在 Linux 下进行开发很多人选择编写 makefile 文件进行项目环境搭建,而makefile ...

  5. 面向对象:classmethod、staticmethod、property

    一.classmethod(类方法).staticmethod(静态方法) 方法包括:普通方法.类方法和静态方法,三种方法在内存中都归属于类,区别在于调用方式不同. # 普通方法 由对象调用,至少一个 ...

  6. 8.mysql执行语句的顺序

    mysql执行语句的顺序     一.group by + where group by 字句和where条件语句结合在一起使用,where在前,group by 在后.即先对select xx fr ...

  7. Python - 模块(一)

    目录 Python - 模块(一) 模块的引用方式 常用模块 random(随机模块) os模块 sys 序列化模块 hashlib subprocess optparse struct Python ...

  8. noip模拟赛 括号序列

    题目描述LYK有一个括号序列,但这个序列不一定合法.一个合法的括号序列如下:()是合法的括号序列.若A是合法的括号序列,则(A)是合法的括号序列.若A和B分别是合法的括号序列,则AB是合法的括号序列. ...

  9. [bzoj2226][Spoj5971]LCMSum_欧拉函数_线性筛

    LCMSum bzoj-2226 Spoj-5971 题目大意:求$\sum\limits_{i=1}^nlcm(i,n)$ 注释:$1\le n\le 10^6$,$1\le cases \le 3 ...

  10. linux sh 脚本调用外部命令

    参考:http://blog.csdn.net/lhb_blog/article/details/22083649 ------------------------------------------ ...