题意:给定一个浮点数,让你在时间 t 内,变成一个最大的数,操作只有把某个小数位进行四舍五入,每秒可进行一次。

析:贪心策略就是从小数点开始找第一个大于等于5的,然后进行四舍五入,完成后再看看是不是还可以,一循环下去,直到整数位,或者没时间了。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <tr1/unordered_map>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
using namespace std :: tr1; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 2e5 + 5;
const int mod = 1e9 + 7;
const int N = 1e6 + 5;
const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
inline LL gcd(LL a, LL b){ return b == 0 ? a : gcd(b, a%b); }
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
char s[maxn];
int a[maxn], b[maxn]; int main(){
while(scanf("%d %d", &n, &m) == 2){
scanf("%s", s);
int cnta = 0, cntb = 0;
a[0] = b[0] = 0;
bool ok = false;
for(int i = 0; i < n; ++i){
if(s[i] == '.'){ ok = true; continue; }
if(ok) b[++cntb] = s[i] - '0';
else a[++cnta] = s[i] - '0';
}
int cnt = 0;
int y = 1, z = cntb; while(m--){
ok = false;
bool x = true;
for(int i = y; i <= z; ++i){
if(b[i] >= 5){
x = false;
cnt = 1;
ok = true;
b[i] = -1;
for(int j = i-1; j >= 0; --j){
if(b[j] + cnt > 9){ b[j] = -1; z = j; }
else { b[j] += cnt; y = j; cnt = 0; break; }
}
}
if(ok) break;
} if(x) break; } if(b[0]){
cnt = 1;
for(int j = cnta; j >= 0; --j){
if(a[j] + cnt > 9) a[j] = 0, cnt = 1;
else { a[j] += cnt; cnt = 0; break; }
}
if(a[0]) printf("1");
for(int i = 1; i <= cnta; ++i)
printf("%d", a[i]);
}
else{
int t = 0;
for(int i = 1; i <= cntb; ++i) if(b[i] == -1){ t = i; break; }
if(!t) t = cntb;
for(int i = t; i > 0; --i)
if(b[i] == 0 || b[i] == -1) b[i] = -1;
else break; for(int i = 1; i <= cnta; ++i)
printf("%d", a[i]);
if(b[1] != -1){
printf(".");
for(int i = 1; i <= cntb; ++i)
if(b[i] == -1) break;
else printf("%d", b[i]);
}
}
printf("\n"); }
return 0;
}

CodeForces 718A Efim and Strange Grade (贪心)的更多相关文章

  1. Codeforces 718A Efim and Strange Grade 程序分析

    Codeforces 718A Efim and Strange Grade 程序分析 jerry的程序 using namespace std; typedef long long ll; stri ...

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

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

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

  5. codeforces 373 A - Efim and Strange Grade(算数模拟)

    codeforces 373 A - Efim and Strange Grade(算数模拟) 原题:Efim and Strange Grade 题意:给出一个n位的实型数,你可以选择t次在任意位进 ...

  6. CF719C. Efim and Strange Grade[DP]

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

  7. Efim and Strange Grade

    Efim and Strange Grade time limit per test 1 second memory limit per test 256 megabytes input standa ...

  8. 【22.17%】【codeforces718B】 Efim and Strange Grade

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  9. Codeforces 437C The Child and Toy(贪心)

    题目连接:Codeforces 437C  The Child and Toy 贪心,每条绳子都是须要割断的,那就先割断最大值相应的那部分周围的绳子. #include <iostream> ...

随机推荐

  1. 全志a13开发总结

    这几天因为工作的原因,開始接触全志a13芯片,本人在网上搜集了好长时间,可是网上的资料对这方面的描写叙述是很少的, 所以,仅仅能靠数据手冊还有官网上面的英文文档进行开发了,下面仅仅是开发中的非常少的一 ...

  2. SolidEdge 工程图中如何绘制中断视图

    右击长条形的视图,点击新增断裂线,然后绘制两个断点   点击完成之后效果如下图所示   如果要修改断裂视图的样式,则选中这个视图,在左键单击,然后点击这个按钮取消显示断裂视图   然后左键单击断裂视图 ...

  3. Java类载入器(二)——自己定义类载入器

      用户定制自己的ClassLoader能够实现以下的一些应用: 自己定义路径下查找自己定义的class类文件,或许我们须要的class文件并不总是在已经设置好的Classpath以下,那么我们必须想 ...

  4. php生成.php文件

    <?php // -- test.php -- // //搜集资料 $str_tmp="<?php\r\n"; //得到php的起始符.$str_tmp将累加 $str ...

  5. js中创建html标签、加入select下默认的option的value和text、删除select元素节点下全部的OPTION节点

    <pre name="code" class="java"> jsp 中的下拉框标签: <s:select name="sjx&qu ...

  6. CPU Stepping

    http://baike.baidu.com/view/16839.htm?fr=ala0_1_1 步进 编辑   步进(Stepping)是CPU的一个重要参数,也叫分级鉴别产品数据转换规范,“步进 ...

  7. flask-本地线程-请求上下文补充

    context(上下文)是flask里面非常好的设计,使用flask需要非常理解应用上下文和请求上下文这两个概念 本地线程 本地线程(thread local)希望不同的线程对于内容的修改只在线程内部 ...

  8. ssh命令、ping命令、traceroute 命令所使用的协议

    在Node reboot or eviction: How to check if yourprivate interconnect CRS can transmit network heartbea ...

  9. jmeter3.0_bodydata中存在中文乱码

    jmeter3.0_bodydata中存在中文乱码 1.进入jmeter.properties配置文件 找到#jsyntaxtextarea.font.family=Hack ,并将“#”取消并重启j ...

  10. 设计模式学习笔记——Prototype原型模式

    原型模型就是克隆. 还有深克隆.浅克隆,一切听上去都那么耳熟能详.