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 ...
随机推荐
- middleware中间件
django 中的中间件(middleware),在django中,中间件其实就是一个类,在请求到来和结束后,django会根据自己的规则在合适的时机执行中间件中相应的方法. 在django项目的se ...
- 用mongodb 固定集合实现只保留固定数量的记录,自动淘汰老旧数据
在一个保存report记录的场景中,我们使用MongoDB进行数据存储 example: db: report Collection: daily_report 创建db: use report; ...
- Java编程基础阶段笔记 day 07 面向对象编程(上)
面向对象编程 笔记Notes 面向对象三条学习主线 面向过程 VS 面向对象 类和对象 创建对象例子 面向对象的内存分析 类的属性:成员变量 成员变量 VS 局部变量 类的方法 方法的重载 可变个 ...
- MyBatis之foreach
foreach foreach 元素是非常强大的,它允许你指定一个集合,声明集合项和索引变量,它们可以用在元素体内.它也允许你指定开放和关闭的字符串,在迭代之间放置分隔符.这个元素是很智能的,它不会偶 ...
- python异常处理-异常捕获-抛出异常-断言-自定义异常-UDP通信-socketserver模块应用-3
异常捕获 异常:程序在运行过程中出现了不可预知的错误,并且该错误没有对应的处理机制,那么就会以异常的形式表现出来 影响:整个程序无法再正常运行 异常的结构 异常的类型 NameError 异常的信息 ...
- 详解阿里P7架构师是怎么在Spring中实现事务暂停
摘要 Spring框架是一个流行的基于轻量级控制反转容器的Java/J2EE应用框架,尤其在数据访问和事务管理方面的能力是众所周知的.Spring的声明性事务分离可以应用到任何POJO目标对象,并且包 ...
- kpm字符串匹配算法
首先是简单的朴素匹配算法 /* * 返回子串t在主串s的位置,若不存在则返回0 */ public static int index(String s, String t) { int i = 0;/ ...
- 洛谷 P2016 战略游戏
题意简述简述 求一棵树的最小点覆盖 题解思路 树形DP dp[i][0]表示第i个点覆盖以i为根的子树的最小值,且第i个点不放士兵 dp[i][1]表示第i个点覆盖以i为根的子树的最小值,且第i个点放 ...
- 对博弈活动中蕴含的信息论原理的讨论,以及从熵角度看不同词素抽象方式在WEBSHELL文本检测中的效果区别
1. 从赛马说起 0x1:赛马问题场景介绍 假设在一场赛马中有m匹马参赛,令第i匹参赛马获胜的概率为pi,如果第i匹马获胜,那么机会收益为oi比1,即在第i匹马上每投资一美元,如果赢了,会得到oi美元 ...
- StudyAndroid.1
目标: 手动创建第一个Activity 开发环境: Android Studio 3.3.1 Build #AI-182.5107.16.33.5264788, built on January 29 ...