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. Django URL(路由系统)

    Django URL Django 1.11版本 URLconf官方文档 URL配置(URLconf)就像 Django 所支撑网站的目录.它的本质是URL模式以及要为该URL模式调用的视图函数之间的 ...

  2. Linux之网络文件共享服务(FTP)

    一.FTP概念 •File Transfer Protocol 早期的三个应用级协议之一 •基于C/S结构 •双通道协议:数据和命令连接 •数据传输格式:二进制(默认)和文本  •两种模式:服务器角度 ...

  3. System----堡垒机

    你知道嘛是堡垒机吗? 你知道堡垒机是奏嘛的吗? 1,改server 端 socket server 接受到的请求 执行指令前,记录收到的指令,来源ip 用户名 缺点:每台机器都要更改源码,加入指令记录 ...

  4. BZOJ 1051 HAOI 2006 受欢迎的牛

    [题解] 先用tarjan缩点,然后如果某个强联通分量的出度为0,则该强联通分量内的点数为答案,否则无解.因为若其他所有的强联通分量都有边连向Ai,则Ai必定没有出边,否则Ai连向的点所属的强联通分量 ...

  5. wx微信小程序

    俩三行时: ==========

  6. Java 学习(7):java 日期时间 & 正则表达式

    目录 --- 日期时间 --- 正则表达式 日期时间:java.util 包提供了 Date 类来封装当前的日期和时间. Date 类提供两个构造函数来实例化 Date 对象. 构造函数:用于初始化对 ...

  7. 小议:部署SharePoint 2013时,无法连接SQL Server问题

    最近在给学员培训时发现,个别学员在完毕SharePoint 2013部署时,无法连接SQL Server,两种报错情况,例如以下所看到的: :配置SharePointConnect to SQL Se ...

  8. HDU1698 Just a Hook 【线段树】+【成段更新】+【lazy标记】

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  9. 欢聚时代校园招聘java开发一面经历

    收到yy短信通知笔试通过后隔天就一面了,面试时间是下午1点半,跟另外一个同学在1点半的时候已经到了目的酒店,发现面试都集中在一个大厅,摆了非常多桌椅,由不同的面试关在面试.等到2点多的时候才到我.先说 ...

  10. 《从零開始学Swift》学习笔记(Day 52)——Cocoa错误处理模式

    原创文章,欢迎转载. 转载请注明:关东升的博客 Swift错误处理模式,在Swift1.x和Swift 2.0是不同的两种模式. Swift 1.x代码错误处理模式採用Cocoa框架错误处理模式,到如 ...