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. docker 在window10下的安装

    在win10下安装docker 打开下载页面 https://store.docker.com/editions/community/docker-ce-desktop-windows 打开控制面板 ...

  2. mybatis入门篇:通过SqlSession.selectList进行数据查询

    作为一个java菜鸟,早就从慕课网中学到一些基本的mybatis的用法,但是一直不成体系,懵懵懂懂,既然正式入了java这个坑,就打算好好学学,所以买了本<MyBatis从入门到精通>,在 ...

  3. 9-安装redis

    1.在linux上安装C语言环境 yum install gcc-c++ 2.解压源码包 tar -xvf /opt/soft/redis-3.0.0.tar -C /opt/app/ 3.编译源码( ...

  4. pass parameter by endpoint, this is for websocket

    使用了Java的字符串:@ServerEndpoint("/chat/{room}")public class MyEndpoint {@OnMessagepublic void ...

  5. android 开发 View _1_ View的子类们 和 视图坐标系图

    目录: android 开发 View _2_ View的属性动画ObjectAnimator ,动画效果一览 android 开发 View _3_ View的属性动画ValueAnimator a ...

  6. 格式化hdfs后,hadoop集群启动hdfs,namenode启动成功,datanode未启动

    集群格式化hdfs后,在主节点运行启动hdfs后,发现namenode启动了,而datanode没有启动,在其他节点上jps后没有datanode进程!原因: 当我们使用hdfs namenode - ...

  7. QTP 学习 - 对象库

    QTP的关键字视图和专家视图 1.Keyword view(关键字视图) 在录制脚本的过程中,用户执行的每一个步骤,在关键字视图中记录为一行. 关键字视图直观有效,用户可以很清楚的看到被录制对象的录制 ...

  8. SpringBoot 之 thymeleaf

    thymeleaf 的maven 配置我们都知道: <dependency> <groupId>org.springframework.boot</groupId> ...

  9. leetcode124

    class Solution { int maxValue; public int maxPathSum(TreeNode root) { maxValue = Integer.MIN_VALUE; ...

  10. python 爬虫启航

    1. 使用excel(简单使用) 数据- 自网站-导入 2.you-get python爬虫入门 1.环境配置 python,request,lxml 2.原理 爬虫的框架如下: 1.挑选种子URL: ...