Educational Codeforces Round 121 (Rated for Div. 2)——B - Minor Reduction
B - Minor Reduction
题源:https://codeforces.com/contest/1626/problem/B
题意
给定一个超级大的整数 x ,可以对任意相邻两位数进行操作(把他们拿出来,然后再把他们的和放回原来的位置,注意不能有前导0,也就是说如果和是一位数的话就只放入一位数),求经过一次操作后的最大 x 为多少
思路
我的做法可能太绕了,特判了好多个。
首先特判掉只有两位数的情况(10 =< x < 100 ),直接加起来就行
一般情况:
先来分析一下,如果相邻两位加起来能够进位的话,也就是结果还是两位数,对原 x 带来的影响是最小的,又因为和一定比原来两个数字小,所以我们优先对后面的位数来操作。
如果不能进位,放在前面比较好,因为加起来所得到的值一定比原来两位上任何一个数大。比如123(33比15大)
那么就来实现一下:
统计每两个相邻数之和,存在cnt数组里面,记录一个最大的和maxn。对于当前的cnt[i],可以分为两种:
1. 小于10(不进位的情况):只记录maxn
2. 大于10(进位):越往后越好,记录下标 k 和 maxn
对于maxn:
1. maxn < 10:两数相加一定会比原来任何一位都大,高位数上的改变所增大的值一定比低位数上带来的影响大,所以要在前面改变(比如111,112)
2. maxn >= 10:两数相加一定会变小,所以尽可能取低位数上来改变(如17878)
我滴代码
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 2e5 + 5;
int t;
int main (){
cin >> t;
while (t --){
string s;
cin >> s;
if (s.size() == 2){
cout << (s[0] - '0') + (s[1] - '0') << endl;
continue;
}
int cnt[N] = {0}, maxn = -1, k = -1;
for (int i = 1; i < s.size(); i ++){
cnt[i] = (s[i] - '0') + (s[i - 1] - '0');
if (cnt[i] < 10){
if (cnt[i] > maxn)
maxn = cnt[i];
}
else{
k = i, maxn = cnt[i];
}
}
if (maxn < 10)
s[0] = char(cnt[1] + '0'), s[1] = '#';
else
s[k] = char(maxn % 10 + '0'), s[k - 1] = char(maxn / 10 + '0');
for (int i = 0; i < s.size(); i ++)
if (s[i] != '#')
cout << s[i];
cout << endl;
}
}
//考虑两个可能会被hack掉的数:111, 17878, 112, 291
辛辛苦苦调了好久的代码,码了好久的字啊www
Educational Codeforces Round 121 (Rated for Div. 2)——B - Minor Reduction的更多相关文章
- Educational Codeforces Round 121 (Rated for Div. 2)——A - Equidistant Letters
A - Equidistant Letters 题源:https://codeforces.com/contest/1626/problem/A 今天上午VP了这场CF,很遗憾的是一道题也没写出来,原 ...
- Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship
Problem Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...
- Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)
Problem Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...
- Educational Codeforces Round 43 (Rated for Div. 2)
Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...
- Educational Codeforces Round 35 (Rated for Div. 2)
Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://code ...
- Educational Codeforces Round 63 (Rated for Div. 2) 题解
Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...
- Educational Codeforces Round 39 (Rated for Div. 2) G
Educational Codeforces Round 39 (Rated for Div. 2) G 题意: 给一个序列\(a_i(1 <= a_i <= 10^{9}),2 < ...
随机推荐
- CF1504X Codeforces Round #712
CF1504D Flip the Cards(找规律+贪心) 题目大意:给你n张牌,正反面都有数字,保证所有牌上的数字在$[1,2n]$内且互不相同.你可以翻转任意张牌,接下来需要把牌按正面的数字从小 ...
- Apache+tomcat实现应用服务器集群
Ngnix/Apache比较 Nginx:Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行.其特点是占有内存少,并发能力 ...
- Git常见错误整理
参考文章 git 排错 fatal: 'git status --porcelain' failed in submodule abi/cpp 1 fatal: 'git status --porce ...
- js file对象 文件大小转换可视容易阅读的单位
function returnFileSize(number) { if(number < 1024) { return number + 'bytes'; } else if(number & ...
- Dubbo 用到哪些设计模式?
Dubbo 框架在初始化和通信过程中使用了多种设计模式,可灵活控制类加载.权 限控制等功能. 工厂模式 Provider 在 export 服务时,会调用 ServiceConfig 的 export ...
- java-与文件相关
java.nio.file 表示non-blocking 非阻塞io(输入和输出) 一个 Path 对象表示一个文件或者目录的路径,是一个跨操作系统(OS)和文件系统的抽象 java.nio.file ...
- Redis 常见性能问题和解决方案?
1.Master 最好不要写内存快照,如果 Master 写内存快照,save 命令调度 rdbSave 函数,会阻塞主线程的工作,当快照比较大时对性能影响是非常大的,会间断性 暂停服务 2.如果数据 ...
- 一个注解@Recover搞定丑陋的循环重试代码
使用背景 在实际项目中其中一部分逻辑可能会因为调用了外部服务或者等待锁等情况下出现不可预料的异常,在这个时候我们可能需要对调用这部分逻辑进行重试,代码里面主要就是使用for循环写一大坨重试的逻辑,各种 ...
- 若没有任何实例包含Class Body 则enum被隐式声明为final
本文参考 今天在Java Language Specification上偶然看到一条关于枚举的语法特点说明 An enum declaration is implicitly final unless ...
- SVN在拉取(更新)代码的时候出现Error:svn: E155037: Previous operation has not finished; run 'cleanup' if it was interrupted问题 ---window版
简易方法1 今天朋友看到朋友报错这个错误,偷偷学习了下他的方法并做记录以防忘记 简易方法2 今天使用svn时报了一个这个错,网上搜索时都说是要使用sqllite来删除svn队列. 其实可以直接使用id ...