CF719C. Efim and Strange Grade[DP]
1 second
256 megabytes
standard input
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.
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.
Print the maximum grade that Efim can get in t seconds. Do not print trailing zeroes.
6 1
10.245
10.25
6 2
10.245
10.3
3 100
9.2
9.2
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.
题意:一个长度为n数,小数点后可以四舍五入t次,求最大可以是多少
终于填坑了,已经一个周了
一开始读错题,因为全部可以四舍五入。必须要学英语了
虽然没想到正解,写到一个贪心,每次从头往后找第一个可以进位的,然后就TLE了
正解竟然是DP,好神
d[i]表示i这一位进位最少需要几次,转移很简单了
最后进位时一定小心处理
//
// main.cpp
// cf#373cdp
//
// Created by Candy on 9/25/16.
// Copyright © 2016 Candy. All rights reserved.
// #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int N=2e5+,INF=1e9+;
int n,t,point=,l,high=;
char s[N];
int d[N];
void dp(){
d[n+]=INF;
for(int i=n;i>point;i--){
if(s[i]<'') d[i]=INF;
if(s[i]>='') d[i]=;
if(s[i]=='') d[i]=d[i+]+;
}
}
void carry(int p){//printf("c %d\n",p);
int flag=;
for(int i=p-;i>=;i--){
if(flag==) break;
if(s[i]=='.') continue;
if(s[i]=='') {s[i]=''; if(i>point) l=i-;else l=point-;}
else {s[i]++;flag=;break;}
}
if(flag) high=;
}
int main(int argc, const char * argv[]) {
scanf("%d%d%s",&n,&t,s+);l=n;
for(int i=;i<=n;i++) if(s[i]=='.'){point=i;break;}
dp();
for(int i=point+;i<=n;i++) if(d[i]<=t){carry(i);l=i-;break;}
if(high){
putchar('');
for(int i=;i<point;i++) putchar('');
}else{
for(int i=;i<=l-;i++) putchar(s[i]);
if(s[l]!='.') putchar(s[l]);
}
return ;
}
CF719C. Efim and Strange Grade[DP]的更多相关文章
- Efim and Strange Grade
Efim and Strange Grade time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Codeforces Round #373 (Div. 2) C. Efim and Strange Grade 水题
C. Efim and Strange Grade 题目连接: http://codeforces.com/contest/719/problem/C Description Efim just re ...
- codeforces 719C. Efim and Strange Grade
C. Efim and Strange Grade time limit per test 1 second memory limit per test 256 megabytes input sta ...
- Codeforces Round #373 (Div. 2) C. Efim and Strange Grade —— 贪心 + 字符串处理
题目链接:http://codeforces.com/problemset/problem/719/C C. Efim and Strange Grade time limit per test 1 ...
- codeforces 373 A - Efim and Strange Grade(算数模拟)
codeforces 373 A - Efim and Strange Grade(算数模拟) 原题:Efim and Strange Grade 题意:给出一个n位的实型数,你可以选择t次在任意位进 ...
- Codeforces 718A Efim and Strange Grade 程序分析
Codeforces 718A Efim and Strange Grade 程序分析 jerry的程序 using namespace std; typedef long long ll; stri ...
- 【22.17%】【codeforces718B】 Efim and Strange Grade
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- CodeForces 718A Efim and Strange Grade (贪心)
题意:给定一个浮点数,让你在时间 t 内,变成一个最大的数,操作只有把某个小数位进行四舍五入,每秒可进行一次. 析:贪心策略就是从小数点开始找第一个大于等于5的,然后进行四舍五入,完成后再看看是不是还 ...
- 【题解】Making The Grade(DP+结论)
[题解]Making The Grade(DP+结论) VJ:Making the Grade HNOI-D2-T3 原题,禁赛三年. 或许是我做过的最简单的DP题了吧(一遍过是什么东西) 之前做过关 ...
随机推荐
- javascript笔记图
1.this 2.对象 3.继承 4.跨域 5.事件 6.基础
- spring-hellow word
在大三的时候开了一门JAVAEE SSH框架,属于软件方向选修课程,虽然本人是搞硬件的,但是也选了这么课程,因为我在想有一天物联网也会走上大门户的,所以果断去蹭课了,时至今日,重新拾起来, ...
- js基本算法:冒泡排序,二分查找
知识扩充: 时间复杂度:算法的时间复杂度是一个函数,描述了算法的运行时间.时间复杂度越低,效率越高. 自我理解:一个算法,运行了几次时间复杂度就为多少,如运行了n次,则时间复杂度为O(n). 1.冒泡 ...
- spring+ibatis+多数据源
环境:spring3.1+ibatis2.3.4+oracle+hbase要求:需要在工程中操作两个不同的数据源,一个是mssql,另一个是hbase.实现: <bean id=" ...
- BT5 & Kali Linux 网卡选择
[需要注意的几点]: 芯片类型 知否支持外接天线 网卡固件版本 支持的无线协议 网卡功率 BT5/Kali RC3支持网卡: RTL8187, R8187 (Realtek) 功率高.兼容性好 Ra ...
- 通过JavaScript原型链理解基于原型的编程
零.此文动机 用了一段时间的Lua,用惯了Java C++等有Class关键字的语言,一直对Lua的中的面向对象技术感到费解,一个开源的objectlua更是看了n遍也没理解其中的原理,直到看到了Pr ...
- Android java传递int类型数组给C
接着前面的文章<Android java传递int类型数据给C><Android java传递string类型数据给C>,继续实践 实现public native int[] ...
- exec
之前一直这样显示 不知道修改了什么,变成了这样. 在终端 找到这个podfile所在的目录 chmod 600 podfile 然后 就变回来了.可能是修改权限的问题.
- 【代码笔记】iOS-点评内容
一,效果图. 二,工程图. 三,代码. ViewController.h #import <UIKit/UIKit.h> @interface ViewController : UIVie ...
- OC 内存泄露 自动释放池
花絮:看到下面的代码就想起这么一个调侃: 一个老程序员,功成名就,金盆洗手不在写代码后,决定练练书法.提笔思索良久后在纸上写下:Hello world! /********************** ...