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的更多相关文章

  1. Codeforces Round #486 (Div. 3)988E. Divisibility by 25技巧暴力||更暴力的分类

    传送门 题意:给定一个数,可以对其做交换相邻两个数字的操作.问最少要操作几步,使得可以被25整除. 思路:问题可以转化为,要做几次交换,使得末尾两个数为00或25,50,75: 自己一开始就是先for ...

  2. Codeforces Round #486 (Div. 3) F. Rain and Umbrellas

    Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...

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

  4. Codeforces Round #486 (Div. 3) A. Diverse Team

    Codeforces Round #486 (Div. 3) A. Diverse Team 题目连接: http://codeforces.com/contest/988/problem/A Des ...

  5. 数学/找规律/暴力 Codeforces Round #306 (Div. 2) C. Divisibility by Eight

    题目传送门 /* 数学/暴力:只要一个数的最后三位能被8整除,那么它就是答案:用到sprintf把数字转移成字符读入 */ #include <cstdio> #include <a ...

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

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

  8. Codeforces Round #486 (Div. 3)988D. Points and Powers of Two

    传送门:http://codeforces.com/contest/988/problem/D 题意: 在一堆数字中,找出尽量多的数字,使得这些数字的差都是2的指数次. 思路: 可以知道最多有三个,差 ...

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

随机推荐

  1. Angularjs启动入口, splash画面,与加快启动的技巧

    Angularjs启动入口, splash画面,与加快启动的技巧 Angularjs启动入口 * 自动响应DOMContentLoaded event * 从ngApp指定的入口启动: 在angula ...

  2. jsonp原理及同源策略

    [个人学习笔记,如有问题还请前辈纠正] jsonp 是用来跨域读取数据的,为什么从不同的域访问数据要用jsop呢?这源于一个著名的安全策略--同源策略,即: 协议.端口号.域名相同 举例说明:http ...

  3. python常见用法

    1.冒泡排序 a = [25,15,47,36,44,455,67,234,7,8,-47] def sortport(): for i in range(len(a)-1): for j in ra ...

  4. Jvm的体系结构

    1.垃圾回收器 垃圾回收器(又称为gc):是负责回收内存中无用的对象(好像地球人都知道),就是这些对象没有任何引用了,它就会被视为:垃圾,也就被干掉了. 2.类装载子系统 一听名字,大家就知道,肯定是 ...

  5. 子页面调整父亲页面的iframe元素

    $('iframe', parent.document).attr('scrolling','no');

  6. 201772020113李清华《面向对象程序设计(java)》第一周学习总结

    201772020113<面向对象程序设计(java)>第一周学习总结 第一部分:课程准备部分 填写课程学习 平台注册账号, 平台名称 注册账号 博客园:www.cnblogs.com b ...

  7. python常用库,包网址

    常用包下载:https://pypi.org/ 1.NumPy: https://www.numpy.org/ 2.pandas: http://pandas.pydata.org/ 3.SciPy: ...

  8. spring aop 学习1

    1.配置自动扫描的包 <context:component-scan base-package="com.ddf.spring.aop.impl"/> 2.使用spri ...

  9. 什么是python的全局解释锁(GIL)

    GIL解决了Python中的什么问题? 为什么选取GIL作为解决方案? 对多线程Python程序的影响 为什么GIL还没有被删除? 为什么在Python 3 中GIL没有被移除? 如何处理Python ...

  10. subString(index,end) 用法

    sb = sb.Substring(0, sb.Length - 1); 获取当前字符串的前一部分去掉最后一个字符