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 ...
随机推荐
- Java动态,安全追踪工具
Java动态,安全追踪工具 在我们日常的开发中,总是难以避免的要解决线上的问题.如果线上的问题我们在本地调试的时候无论调试多少次发现明明本地调用了这个方法呀,怎么线上就是没调呢?还有就是出了问题的时候 ...
- 同时启动多个tomcat,端口修改
所用Tomcat服务器都为zip 版,非安装版.以 tomcat8 为例: 安装第二个Tomcat完成后,打开 tomcat/conf/server.xml 文件,查找以下三处: 1. 修改http访 ...
- 从原理层面掌握@RequestAttribute、@SessionAttribute的使用【一起学Spring MVC】
每篇一句 改我们就改得:取其精华,去其糟粕.否则木有意义 前言 如果说知道@SessionAttributes这个注解的人已经很少了,那么不需要统计我就可以确定的说:知道@RequestAttribu ...
- 【CodeForces - 1200C】Round Corridor (数论gcd)
Round Corridor Descriptions Amugae位于一个非常大的圆形走廊中.走廊由两个区域组成.内部区域等于nñ扇区,外部区域等于m米部门.在相同区域(内部或外部)的每对扇区之间 ...
- Go-json解码到结构体-踩坑
package main import ( "encoding/json" "fmt" ) type User struct { Name string `js ...
- [SCOI2007]压缩(动态规划,区间dp,字符串哈希)
[SCOI2007]压缩 状态:设\(dp[i][j]\)表示前i个字符,最后一个\(M\)放置在\(j\)位置之后的最短字串长度. 转移有三类,用刷表法来实现. 第一种是直接往压缩串后面填字符,这样 ...
- python 编码报错问题 'ascii' codec can't encode characters 解决方法
python在安装时,默认的编码是ascii, 当程序中出现非ascii编码时,python的处理常常会报这样的错 'ascii' codec can't encode characters pyth ...
- jmeter+WebDriver:启动浏览器进行web自动化
无论是web自动化还是手机app自动化,WebDriver是Selenium的核心模块,jmeter WebDriver 仅支持Firefox.Chrome 和 HTML Unit驱动,暂不支持IE ...
- Mongodb最基础入门教程
Mongodb最基础入门教程 如果想了解一下redis的入门教程,可以去看一下我的上一篇博客 Mongodb的安装大家可以参考一下其他博主的博客,这里我就不做介绍了.不过值得注意的是,在Linux ...
- springboot中oracle的依赖添加失败的解决
由于Oracle授权问题,Maven3不提供oracle JDBC driver 步骤一:在pom中添加如下: <!--Oracle 驱动 --> <dependency> ...