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. Lua练习题集嚢

    --1.table.sort() am = {"cc","nn","ll","dd"} arr = function ( ...

  2. POJ-1655 Balancing Act(树的重心)

    Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the t ...

  3. 3.filter原理(bitset机制与caching机制)

    主要知识点: 一次filter执行顺序 filter和query的特点     一.一次filter执行顺序     1.在倒排索引中查找搜索串,获取document list 以一下date数据来举 ...

  4. 《hello-world》第八次团队作业:Alpha冲刺-Scrum Meeting 1

    项目 内容 这个作业属于哪个课程 2016级计算机科学与工程学院软件工程(西北师范大学) 这个作业的要求在哪里 实验十二 团队作业8:软件测试与Alpha冲刺 团队名称 <hello--worl ...

  5. Ubuntu | Flask + Gunicorn + Nginx 部署服务器环境

    现在我们手里有一个准备发布的项目,那么如何将他上传到你的服务器,并让外网访问呢? 前提: 1. 安装了Python环境 apt-get install python-dev 2. 安装Flask pi ...

  6. 火狐浏览器插件(XPI 文件)签名指南

    Symantec,Thawte,GlobalSign 签发的代码签名证书都可以签名火狐浏览器插件(XPI)文件.如果您还没有代码签名证书,请联系易维信(EVTrust)购买火狐代码签名证书. 第 1 ...

  7. [Codeforces 872]比赛记录

    强行打了$cf$上的第一场比赛,现在感觉自己的$rating$会炸飞= = A  这是练习输入输出吗QAQ,竟然$WA$了两遍QAQ,我$WA$的一声就哭了出来啊QAQ B  好像很水的乱扫就好了,m ...

  8. 清北学堂模拟赛d6t2 刀塔

    分析:看到最小值最大就很显然是二分了吧,二分一下最小值,把小于它的数给删掉,然后看每个数向左边能延伸多长,往右边能延伸多长,最后统计一下有没有可行答案就可以了. #include <cstdio ...

  9. poj 3621最优比例生成环(01分数规划问题)

    /* 和求最小生成树差不多 转载思路:http://www.cnblogs.com/wally/p/3228171.html 思路:之前做过最小比率生成树,也是属于0/1整数划分问题,这次碰到这道最优 ...

  10. cocoa 运行apple脚本文件的方法

    NSString* path = [[NSBundle mainBundle] pathForResource:@"ScriptName" ofType:@"scpt&q ...