• Efim and Strange Grade


    A. 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 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.

    思路:直接模拟;

    找小数点最前面那位大于5的。

      1 #include<stdio.h>
      2 #include<algorithm>
      3 #include<iostream>
      4 #include<stdlib.h>
      5 #include<queue>
      6 #include<string.h>
      7 #include<map>
      8 #include<vector>
      9 #include<queue>
     10 using namespace std;
     11 typedef long long LL;
     12 typedef struct node
     13 {
     14     int x;
     15     int pos;
     16     bool operator<(const node &cx)const
     17     {
     18         return cx.pos < pos;
     19     }
     20 } ss;
     21 char str[200005];
     22 priority_queue<ss>que;
     23 bool flag[200005];
     24 int main(void)
     25 {
     26     int n;
     27     scanf("%d",&n);
     28     {
     29         int x ;
     30         int tt = 0;
     31         scanf("%d",&x);
     32         scanf("%s",str);
     33         int i,j;
     34         int id=-1;
     35         memset(flag,0,sizeof(flag));
     36         for(i = 0; i < n; i++)
     37         {
     38             if(str[i]=='.')
     39                 break;
     40         }
     41         id = i;
     42         if(id==-1)
     43         {
     44             printf("%s
    ",str[i]);
     45         }
     46         else
     47         {
     48             int p = n-1;
     49             for(j = i+1; j < n; j++)
     50             {
     51                 ss ak;
     52                 ak.pos = j;
     53                 ak.x = str[j]-'0';
     54                 if(ak.x > 4)
     55                     {que.push(ak);break;}
     56             }
     57             while(x&&!que.empty())
     58             {
     59                 x--;
     60                 ss ac = que.top();
     61                 que.pop();
     62                 {
     63                     flag[ac.pos] = true;
     64                     int d = 1;
     65                     int c;
     66                     str[ac.pos] ='';
     67                     p = ac.pos-1;
     68                     for(i = ac.pos-1; i > id; i--)
     69                     {
     70                         c = str[i]-'0';
     71                         c = c + d;
     72                         d = c/10;
     73                         c = c%10;
     74                         str[i] = c+'0';
     75                         if(c == 0)
     76                         {
     77                             p = i;
     78                             str[i]='';
     79                         }
     80                         if(c > 4)
     81                         {
     82                             ss akk ;
     83                             akk.pos = i;
     84                             akk.x = c;
     85                             que.push(akk);
     86                         }
     87                         if(d == 0)
     88                             break;
     89                     }
     90                     if(d > 0)
     91                     {
     92                         for(j = id-1; j >= 0; j--)
     93                         {
     94                             c = str[j]-'0';
     95                             c = c + d;
     96                             d = c/10;
     97                             c = c%10;
     98                             str[j] = c+'0';
     99                             if(d == 0)
    100                                 break;
    101                         }
    102                         if(d >= 1)
    103                             tt++;
    104                     }
    105                 }
    106             }
    107             //printf("%d
    ",tt);
    108             if(tt)printf("%d",tt);
    109             if(str[id+1]=='')str[id] = '';
    110             for(i = 0; i > id; i++)
    111             {
    112                 if(str[i]!='0')
    113                     break;
    114             }
    115             int u ;
    116             if(i == id)
    117                 u = id+1;
    118             else u = i;
    119             printf("%s",str+u);
    120         }
    121     }
    122     return 0;
    123 }
    油!油!you@
  • 相关阅读:
    赴美工作常识(Part 4
    赴美工作常识(Part 3
    50 年前的黑客黑什么?
    孩子王?有孩子气才能为王?
    赴美工作常识(Part 2
    UE4 Keynote 1
    Go Programming Language 3
    Go Programming Language 2
    Go Programming Language
    Lua语法要点2
  • 原文地址:https://www.cnblogs.com/zzuli2sjy/p/5902397.html
Copyright © 2020-2023  润新知