CF 988E Divisibility by 25 思维 第十二
1 second
256 megabytes
standard input
standard output
You are given an integer nn from 11 to 10181018 without leading zeroes.
In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes.
What is the minimum number of moves you have to make to obtain a number that is divisible by 2525? Print -1 if it is impossible to obtain a number that is divisible by 2525.
The first line contains an integer nn (1≤n≤10181≤n≤1018). It is guaranteed that the first (left) digit of the number nn is not a zero.
If it is impossible to obtain a number that is divisible by 2525, print -1. Otherwise print the minimum number of moves required to obtain such number.
Note that you can swap only adjacent digits in the given number.
5071
4
705
1
1241367
-1
In the first example one of the possible sequences of moves is 5071 →→ 5701 →→ 7501 →→ 7510 →→ 7150.
题意: 可以移动每位数的位置,使得移动后的数字是25的倍数,问最小需要移动多少位?
emmmmm,好多特殊情况,wa了很多发
能整除25,则最后两位只能是00,25,50,75
枚举0,2,5,7的个数
然后判断这四种情况,需要注意的是如果处于后面的数在前面了,如果此时另一个数不在末位则移动次数需加一
还有如果从第二位开始有连续的0的话且要移动的数位置在第一位此时移动可能导致开头为0,需要多移动0的个数次,还有特判要用到的0在第二位此时需要加的次数少一
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define debug(a) cout << #a << " " << a << endl
using namespace std;
const int maxn = *1e3 + ;
const int mod = 1e9 + ;
typedef long long ll;
int main(){
std::ios::sync_with_stdio(false);
string s;
while( cin >> s ) {
ll a = -, b = -, c = -, d = -, num = , ta;
for( ll i = ; i < s.length(); i ++ ) {
if( s[i] == '' ) {
num ++;
if( num >= ) {
ta = a;
}
a = i;
} else if( s[i] == '' ) {
b = i;
} else if( s[i] == '' ) {
c = i;
} else if( s[i] == '' ) {
d = i;
}
}
ll ans1 = 1e12, ans2 = 1e12, ans3 = 1e12, ans4 = 1e12;
bool flag = false;
//debug(a), debug(b), debug(c),debug(d),debug(num);
if( a != - && c != - ) {
if( a < c ) {
if( c == s.length() - ) {
ans1 = min( s.length() - - a, ans1 );
} else {
ans1 = min( s.length() - - a + s.length() - - c + , ans1 );
}
} else {
ans1 = min( s.length() - - a + s.length() - - c, ans1 );
}
ll t = , i = ;
while( s[i] == '' ) {
t ++;
i ++;
}
if( s[] == '' && ( a == || c == ) ) {
if( s.length() == ) {
flag = true;
} else {
ans1 += t;
if( a == ) {
ans1 --;
}
}
}
}
if( c != - && b != - ) {
if( c < b ) {
if( b == s.length() - ) {
ans2 = min( s.length() - - c, ans2 );
} else {
ans2 = min( s.length() - - c + s.length() - - b + , ans2 );
}
} else {
ans2 = min( s.length() - - c + s.length() - - b, ans2 );
}
ll t = , i = ;
while( s[i] == '' ) {
t ++;
i ++;
}
if( s[] == '' && ( b == || c == ) ) {
if( s.length() == ) {
flag = true;
} else {
ans2 += t;
}
}
}
if( c != - && d != - ) {
if( c < d ) {
if( d == s.length() - ) {
ans3 = min( s.length() - - c, ans3 );
} else {
ans3 = min( s.length() - - c + s.length() - - d + , ans3 );
//debug(ans);
}
} else {
ans3 = min( s.length() - - c + s.length() - - d, ans3 );
}
ll t = , i = ;
while( s[i] == '' ) {
t ++;
i ++;
}
if( s[] == '' && ( d == || c == ) ) {
if( s.length() == ) {
flag = true;
} else {
ans3 += t;
}
}
}
//debug(ans);
if( num >= ) {
ans4 = min( ans4, s.length() - - a + s.length() - - ta );
}
ll ans = min( min( ans1, ans2 ), min( ans3, ans4 ) );
if( ans == 1e12 && !flag ) {
cout << - << endl;
} else {
cout << ans << endl;
}
}
return ;
}
CF 988E Divisibility by 25 思维 第十二的更多相关文章
- Codeforces 988E. Divisibility by 25
解题思路: 只有尾数为25,50,75,00的数才可能是25的倍数. 对字符串做4次处理,以25为例. a. 将字符串中的最后一个5移到最后一位.计算交换次数.(如果没有找到5,则不可能凑出25,考虑 ...
- Codeforces Round #486 (Div. 3)988E. Divisibility by 25技巧暴力||更暴力的分类
传送门 题意:给定一个数,可以对其做交换相邻两个数字的操作.问最少要操作几步,使得可以被25整除. 思路:问题可以转化为,要做几次交换,使得末尾两个数为00或25,50,75: 自己一开始就是先for ...
- Codeforces Round #486 (Div. 3) E. Divisibility by 25
Codeforces Round #486 (Div. 3) E. Divisibility by 25 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...
- [分享] IT天空的二十二条军规
Una 发表于 2014-9-19 20:25:06 https://www.itsk.com/thread-335975-1-1.html IT天空的二十二条军规 第一条.你不是什么都会,也不是什么 ...
- 201521123038 《Java程序设计》 第十二周学习总结
201521123038 <Java程序设计> 第十二周学习总结 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 2. 书面作业 将Student ...
- 《Two Dozen Short Lessons in Haskell》(二十二)递归
<Two Dozen Short Lessons in Haskell>(Copyright © 1995, 1996, 1997 by Rex Page,有人翻译为Haskell二十四学 ...
- 【NOI2019十二省联合省选】部分题简要题解
Day 1 T1 异或粽子 题意:给出一个长为n的序列,选择K个不完全重合的区间使得每个区间的异或值的总和最大. 题解:先做一个前缀异或和,对于每一个右端点我们记录三元组(l,r,x)表示在左端点在\ ...
- 智课雅思词汇---十二、vent是什么意思
智课雅思词汇---十二.vent是什么意思 一.总结 一句话总结:词根:ven, vent = come, 表示“来” 词根:vent = wind 风 1.tact是什么意思? 词根:-tact-, ...
- Web 前端开发精华文章推荐(jQuery、HTML5、CSS3)【系列十二】
2012年12月12日,[<Web 前端开发人员和设计师必读文章>系列十二]和大家见面了.梦想天空博客关注 前端开发 技术,分享各种增强网站用户体验的 jQuery 插件,展示前沿的 HT ...
随机推荐
- Shiro权限框架与SpringMVC集成
1.Shiro整合SpringMVC 我们学习Shiro框架肯定是要应用到Web项目上的,所以我们需要整合Shiro和SpringMVC 整合步骤: 第一步:SpringMVC框架的配置 spring ...
- Java 性能优化(一)
Java 性能调优(一) 1.衡量程序性能的标准 (1) 程序响应速度: (2) 内存占有情况: 2.程序调优措施 (1) 设计调优 设计调优处于所有调优手段 的上层,需要在软件开发之前进行.在软件开 ...
- 逆向破解之160个CrackMe —— 001
CrackMe —— 001 160 CrackMe 是比较适合新手学习逆向破解的CrackMe的一个集合一共160个待逆向破解的程序 CrackMe:它们都是一些公开给别人尝试破解的小程序,制作 c ...
- sublime text 3 15个常用插件介绍
1.ColorPicker 功能:调色板(需要输入颜色时,可直接选取颜色) 使用:快捷键Windows: ctrl+shift+c 2.Emmet 功能:编码快捷键,前端必备 使用:在输入代码段后,按 ...
- 单机版ZooKeeper的安装教程
之前一直没有时间去整理,现在抽出几分钟时间整理以下,有问题的在评论区留言即可. 前期准备JDK环境(ZK需要jdk进行编译,本文以jdk1.8.0_211为例).Linux系统(本文以Centos7为 ...
- Jmeter 01 Jmeter下载安装及入门
jmeter简介 Apache JMeter是Apache组织开发的基于Java的压力测试工具.用于对软件做压力测试,它最初被设计用于Web应用测试,但后来扩展到其他测试领域.--百度百科 下载 下载 ...
- 理解-NumPy
# 理解 NumPy 在这篇文章中,我们将介绍使用NumPy的基础知识,NumPy是一个功能强大的Python库,允许更高级的数据操作和数学计算. # 什么是 NumPy? NumPy是一个功能强大的 ...
- npm钉钉脚手架,支持考勤信息获取
钉钉官方并未提供nodejs包,第一次调用接口的时候非常费事,而且尝试去寻找相关的钉钉考勤数据模块的时候只找到了一些消息啊,只能办公啊,免登啊之类的模块,有关考勤数据的似乎没有 关于dd的npm包中一 ...
- 结合suctf-upload labs-RougeMysql再学习
这篇主要记录一下这道题目的预期解法 做这道题首先要在自己的vps搭建一个rouge mysql,里面要填写需要读取客户端的文件名,即我们上传的phar文件路径 先搭一个rouge mysql测试看看: ...
- coo ceo cfo cto cio 区别
常见的CEO(Chief executive officer)首席执行官类似总经理.总裁,是企业的法人代表. COO(Chief operating officer)首席运营官 类似常务总经理CFO( ...