codeforces 719C Efim and Strange Grade (复杂模拟)

来源:互联网 发布:淘宝动态评分怎么提升 编辑:程序博客网 时间:2024/05/21 01:48

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 digitn + 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 0001 ≤ 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 110.245
output
10.25
input
6 210.245
output
10.3
input
3 1009.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.30will be considered incorrect.

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



题目大意:进行k次四舍五入,可以得到的最大的数。

解题思路:从小数点后面找到第一个大于等于5的数字,从后往前遍历,要特别注意9的往前进位,本题还有一个trick 999.999 这个数字最后要输出1000,而我最开始的算法并没有考虑往前面+1的情况 T^T....被人hack了...

AC代码

#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <cmath>#include<set>#include<map>#include<queue>#define MAXN 5005#define inf 0x3f3f3f3f#define ONES(x) __builtin_popcount(x)using namespace std;typedef long long ll ;const double eps =1e-8;const int mod = 1000000007;typedef pair<int, int> P;const double PI = acos(-1.0);int dx[4] = {0,0,1,-1};int dy[4] = {1,-1,0,0};int n,t;int ans;char str[200005];int main(){    //freopen("test.txt","r",stdin);    cin>>n>>t;    cin>>str;    int len = strlen(str);    int dot_pos = 0,pos = 0;//pos->the operation place    int flag = 0;    int last = len;//last标记截止的位置,时刻更新    for(int i = 0; i<len ; i++)//预处理部分,找到小数点的位置和小数点后面第一个大于5的数字的位置    {        if(str[i]=='.')            {dot_pos = i;flag = 1;}        if(flag==1 && str[i]-'5'>=0)            {pos = i;break;}    }    if(pos <= dot_pos)//特判小数点后没有大于5的数字的情况    {        for(int i = 0; i<len ; i++)            cout << str[i];        cout << endl;        return 0;    }    int m = len - dot_pos;    int k = 0;    while(k<min(m-1,t) && pos>dot_pos && str[pos]>='5')//进行最多k次四舍五入,位置不能超过小数点    {        if(pos-1 == dot_pos)            pos--;        while(str[pos-1]=='9')//处理9的进位的情况        {            pos--;            if(pos-1 == dot_pos)//可以越过小数点                pos--;            last = pos;        }        str[pos-1] += 1;        pos--;        last = pos;        k++;    }    //cout << last << endl;    for(int i = 0; i<=last ; i++)        cout << str[i];    if(last==-1 && str[0]=='9')//特殊处理需要+1的情况        cout << "1" ;    while(last+1<dot_pos)    {        cout << "0";        last++;    }    cout << endl;    //cout << ans << endl;    return 0;}


0 0