Codeforces Round #373 (Div. 2) C. Efim and Strange Grade 贪心、实数字符串"a.b"处理

C. Efim and Strange Grade
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 tseconds. 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 5than 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 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.



Source

Codeforces Round #373 (Div. 2)


My Solution

贪心、实数字符串"a.b"处理

每次可以任选择一位进行四舍五入,求中间所有过程中出现的最大的数字。

所以选 '.'右边的第一个 s[i] > '4'的位置,往左进行四舍五入,本次一旦停止了,如果当前位置 s[j] <= '4', 则break;当前的 s 就是答案了;否则继续往左四舍五入。

对于 '.'右边没有 s[i] > '4' 则s本身直接就是答案了

此外 对于 999.99999这样的数字有特殊的处理 四舍五入是 从右往左,当 s[i] == '9' 且 i == 0 时  s = '1' + s;


此外是 用字典序 if(ans < s) ans = s; 这个比较的复杂度 是 O(n),所以这样做 最糟糕复杂度是 O(n^2),所以应该直接把 答案放在s里,即确保每次处理完都是当前情况下的答案,

然后 用 一个 string tt;把末尾的0去掉://即对于s从右向左,第一个不是 0 的数字或者 '.'开始把该位(非'.'时)以及其左边的字符记录到tt里, 然后对于tt倒的输出。这样可以去掉末尾的一连串的 '0'。

    ans = s;

    string tt;
    int sz = ans.size();
    flag = false;
    for(int i = sz-1; i >= 0; i--){
        if(ans[i] == 0) continue;
        if(!flag){
            if(ans[i] != '0'){
                flag = true;
                if(ans[i] == '.') ;
                else tt += ans[i];
            }
        }
        else{
            //cout << tt <<endl;
            tt += ans[i];
        }
    }

    sz = tt.size();
    for(int i = sz-1; i >= 0; i--){
        cout << tt[i];
    }
    cout << endl;


总复杂度 O(n)


#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
typedef long long LL;
const int maxn = 2e5 + 8;


int main()
{
    #ifdef LOCAL
    freopen("c.txt", "r", stdin);
    //freopen("c.out", "w", stdout);
    int T = 6;
    while(T--){
    #endif // LOCAL
    ios::sync_with_stdio(false); cin.tie(0);

    LL n, t, cnt = 0, point;
    string s, ans;
    cin >> n >> t;
    cin >> s;
    //ans = s;
    bool flag = true;                      //用来标记是否遇到了 '.'
    //应该从左到右
    int start = -1;
    for(int i = 0; i < n; i++){
        if(start != -1){
            if(s[i] > '4'){
                start = i;
                break;
            }
        }
        else if(s[i] == '.') {start = i+1; point = i;}
    }
    for(int i = start + 1; i < n; i++){
        s[i] = '0';
    }
    bool ok = false;                      //用来标记进行了一次四舍五入,然后一旦在ok == true的情况下停下了四舍五入,当前的s就是答案了
    //cout << start << " " << point <<endl;
    for(int i = start; i >= 0; i--){
        if(start == point + 1){
            if(s[start] > '4'){
                start = n;
            }
            else{
                break;
            }
        }
        if(s[i] != '.'){
            if(s[i] > '4'){
                s[i] = '0';
                i--;
                for(i; i >= 0; i--){
                    if(s[i] == '.') {flag = false; continue;}
                    if(s[i] != '9'){
                        s[i] += 1;
                        cnt++;
                        /*
                        if(s > ans){
                            ans = s;
                        }
                        */
                        i++;
                        ok = true;
                        break;
                    }
                    else{
                        s[i] = '0';
                    }

                    if(i == 0){
                        s = '1' + s;

                    }
                }
            }
            else if(ok) break;
            else{
                s[i] = 0;
            }
            if(!flag) break;
            if(cnt == t) break;
        }
        else{ break;}
    }
    //cout << s <<endl;
    ans = s;
    string tt;
    int sz = ans.size();
    flag = false;
    for(int i = sz-1; i >= 0; i--){
        if(ans[i] == 0) continue;
        if(!flag){
            if(ans[i] != '0'){
                flag = true;
                if(ans[i] == '.') ;
                else tt += ans[i];

            }
        }
        else{
            //cout << tt <<endl;
            tt += ans[i];
        }
    }

    sz = tt.size();
    for(int i = sz-1; i >= 0; i--){
        cout << tt[i];
    }
    cout << endl;

    #ifdef LOCAL
    cout << endl;
    }
    #endif // LOCAL
    return 0;
}

  Thank you!

                                                                                                                                               ------from ProLights 

你可能感兴趣的:(ACM,codeforces,贪心,实数字符串a.b处理)