Codeforces Round #486 (Div. 3) E. Divisibility by 25
Codeforces Round #486 (Div. 3) E. Divisibility by 25
题目连接:
http://codeforces.com/group/T0ITBvoeEx/contest/988/problem/E
Description
You are given an integer n from 1 to 10^18 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 25? Print -1 if it is impossible to obtain a number that is divisible by 25.
Sample Input
5071
Sample Output
4
题意
给定一个数,只能交换相邻元素,最少交换几次使得数能被25整除?
题解:
只有末尾是 00, 25, 50, 75才能被整除。将对应数字先移到末尾,再移动倒数第二个数字,是最短的步骤,比较四个情况最少步骤即可。
代码
#include <bits/stdc++.h>
using namespace std;
string d;
int ans;
int getnumber(int a,int b) {
int top = d.size();
int cnt=0;
string s = d;
if (b!=s[top-1]) {
int i=top-2;
for (i=top-2;i>=0;i--) if (s[i]==b) break;
if (i>=0) {
cnt = top-1-i;
for (;i<top-1;i++) swap(s[i],s[i+1]);
} else return 0x7fffffff;
}
if (a!=s[top-2]) {
int i=top-3;
for (i=top-3;i>=0;i--) if (s[i]==a) break;
if (i>=0) {
cnt += top-2-i;
for (;i<top-2;i++) swap(s[i],s[i+1]);
} else return 0x7fffffff;
}
if (s[0]=='0') {
int i=0;
for (;i<top;i++) if (s[i]!='0') break;
if (i<top-2) {
cnt += i;
for (;i;i--) swap(s[i],s[i-1]);
} else return 0x7fffffff;
}
if (s[top-2]==a && s[top-1]==b) return cnt;
else return 0x7fffffff;
}
int main() {
cin>>d;
ans = 0x7fffffff;
ans = min(ans,getnumber('0','0'));
ans = min(ans,getnumber('2','5'));
ans = min(ans,getnumber('5','0'));
ans = min(ans,getnumber('7','5'));
cout << (ans == 0x7fffffff?-1:ans);
}
Codeforces Round #486 (Div. 3) E. Divisibility by 25的更多相关文章
- Codeforces Round #486 (Div. 3)988E. Divisibility by 25技巧暴力||更暴力的分类
传送门 题意:给定一个数,可以对其做交换相邻两个数字的操作.问最少要操作几步,使得可以被25整除. 思路:问题可以转化为,要做几次交换,使得末尾两个数为00或25,50,75: 自己一开始就是先for ...
- Codeforces Round #486 (Div. 3) F. Rain and Umbrellas
Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...
- Codeforces Round #486 (Div. 3) D. Points and Powers of Two
Codeforces Round #486 (Div. 3) D. Points and Powers of Two 题目连接: http://codeforces.com/group/T0ITBvo ...
- Codeforces Round #486 (Div. 3) A. Diverse Team
Codeforces Round #486 (Div. 3) A. Diverse Team 题目连接: http://codeforces.com/contest/988/problem/A Des ...
- 数学/找规律/暴力 Codeforces Round #306 (Div. 2) C. Divisibility by Eight
题目传送门 /* 数学/暴力:只要一个数的最后三位能被8整除,那么它就是答案:用到sprintf把数字转移成字符读入 */ #include <cstdio> #include <a ...
- Codeforces Round #306 (Div. 2) C. Divisibility by Eight 暴力
C. Divisibility by Eight Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/ ...
- Codeforces Round #486 (Div. 3)-B. Substrings Sort
B. Substrings Sort time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- Codeforces Round #486 (Div. 3)988D. Points and Powers of Two
传送门:http://codeforces.com/contest/988/problem/D 题意: 在一堆数字中,找出尽量多的数字,使得这些数字的差都是2的指数次. 思路: 可以知道最多有三个,差 ...
- Codeforces Round #486 (Div. 3)-C. Equal Sums
C. Equal Sums time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
随机推荐
- MSB8013
解决方案: 去掉勾选
- android 开发 View _11_ xml动画
请大家尊重原创者版权,转载请标明出处:http://blog.csdn.net/harvic880925/article/details/39996643 谢谢! 一.概述 Android的anima ...
- 《算法导论》——重复元素的随机化快排Optimization For RandomizedQuickSort
昨天讨论的随机化快排对有重复元素的数组会陷入无限循环.今天带来对其的优化,使其支持重复元素. 只需修改partition函数即可: int partition(int *numArray,int he ...
- jQuery入门基础(动画效果)
一.隐藏显示 1.show()--显示隐藏的被选择元素 例:$(selector).show(speed,callback); 2.hide()--隐藏被选元素的内容 例:$(selector).hi ...
- java 内部类注意点
1.只有内部类可以是私有类,而常规类只可以具有包可见性,或公有可见性. 内部类访问权限修饰符可以为:public.protected.默认.private:(可以用哪些修饰符视具体内部类类型而定) 常 ...
- I2C的小结
下面是 I 2 C 总线的一些特征 只要求两条总线线路 一条串行数据线 SDA 一条串行时钟线 SCL 每个连接到总线的器件都可以通过唯一的地址和一直存在的简单的主机 从机关系软件设定地 址 主机可以 ...
- python Django 无法获取post 参数问题
对于 request.POST.get(name) 方式取值,需要 from 表单提交数据,如果 是ajax 提交数据,则需要做如下设置: 1.设置请求头,以from表单方式传值 'Content-T ...
- JUC详解
一.Java多线程 -- JUC包源码分析1 -- CAS/乐观锁 乐观锁其实就是不加锁,用CAS + 循环重试,实现多个线程/多个客户端,并发修改数据的问题 使用AtomicStampedRefer ...
- 关于http以及aphace配置https
我是通过腾讯云配置的ssl. 网站:www.xian029.cn 免费申请,然后通过phpstudy 来配置的. 密码学: 研究密码编码与解码的学科,可以分为编码学和破译学. HTTPS ...
- LENGTH()和CHAR_LENGTH()区别
LENGTH()返回以字节为单位的字符串的长度.CHAR_LENGTH()返回以字符为单位的字符串的长度. 分别对应于java中的str.getBytes().length和str.length( ...