B - Minor Reduction

题源:https://codeforces.com/contest/1626/problem/B

题意

给定一个超级大的整数 x ,可以对任意相邻两位数进行操作(把他们拿出来,然后再把他们的和放回原来的位置,注意不能有前导0,也就是说如果和是一位数的话就只放入一位数),求经过一次操作后的最大 x 为多少

思路

我的做法可能太绕了,特判了好多个。

  1. 首先特判掉只有两位数的情况(10 =< x < 100 ),直接加起来就行

  2. 一般情况:

  • 先来分析一下,如果相邻两位加起来能够进位的话,也就是结果还是两位数,对原 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的更多相关文章

  1. Educational Codeforces Round 121 (Rated for Div. 2)——A - Equidistant Letters

    A - Equidistant Letters 题源:https://codeforces.com/contest/1626/problem/A 今天上午VP了这场CF,很遗憾的是一道题也没写出来,原 ...

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

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

  4. Educational Codeforces Round 43 (Rated for Div. 2)

    Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...

  5. Educational Codeforces Round 35 (Rated for Div. 2)

    Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...

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

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

  8. Educational Codeforces Round 63 (Rated for Div. 2) 题解

    Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...

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

随机推荐

  1. Django中ORM对数据库的增删改查

    Django中ORM对数据库数据的增删改查 模板语言 {% for line in press %} {% line.name %} {% endfor %} {% if 条件 %}{% else % ...

  2. 前端好用API之Fullscreen

    前情 在前端开发需求中,特别网页有视频需求时,需要做视频全屏功能,或者在某些可视化大屏项目也要做全屏. Fullscreen介绍 让你可以简单地控制浏览器,使得一个元素与其子元素,如果存在的话,可以占 ...

  3. python练习册 每天一个小程序 第0012题

    # -*-coding:utf-8-*- def test(content): text = content flag = 0 with open('filtered_words.txt') as f ...

  4. [XMAN筛选赛](web)ctf用户登录

    0x00 题目概述 就只写了几道web题,有些考察点比较明显,所以个人感觉来说web总体不难. 一航的writeup写得差不多,我这写个他没写的wirteup. 看题: 链接点进去是一个登录页面,习惯 ...

  5. djinn

    靶机准备 将靶机ova文件导入虚拟机,并将网络设置为NAT 开机获得ip:192.168.164.188 kali:192.168.164.137 渗透测试 扫描端口 nmap -sS -sV -T5 ...

  6. 题解0004:单词接龙(洛谷P1019)

    题目描述:已知一组单词,给定一个开头的字母,要求出以这个字母开头的最长的"龙"(每个单词都最多在"龙"中出现两次),在两个单词相连时,其重合部分合为一部分. 题 ...

  7. 写出Hibernate中核心接口/类的名称,并描述他们各自的责任?

    Hibernate的核心接口一共有5个,分别为:Session.SessionFactory.Transaction.Query和 Configuration.这5个核心接口在任何开发中都会用到.通过 ...

  8. Python使用pip安装No matching distribution found for PyYaml==5.3.1

    ERROR: Command errored out with exit status 1: command: /usr/local/dmahz/p_book_data/bin/python3.9 - ...

  9. SpringDataJdbc整合MyBatis方式

    由于官方文档springdatajdbc整合mybatis过于简述,导致死磕了一段时间, SpringDataJdbc整合Mybatis的官方文档:https://docs.spring.io/spr ...

  10. Spring根据路径前缀获取不同Resource

    相关文章:https://www.jianshu.com/p/5bab9e03ab92 官方文档:https://docs.spring.io/spring/docs/current/spring-f ...