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. python程序正式开始

    第几个hello world 程序了,为曾经没有毅力的自己默哀下.今天的课程语言的分类,三大类:机器语言,汇编语言,高级语言. 其中最让我痛恨的就是汇编语言,我们大学没事开什么这课程,大学混日子的喔不 ...

  2. Sublime Text 3激活

    Sublime Text 3激活方式: 一.修改hosts文件: 1:windows系统:      找到 C:\Windows\System32\drivers\etc\hosts 这个文件,  用 ...

  3. 协程,greenlet,gevent

    """ 协程 """ ''' 协程: 类似于一个可以暂停的函数,可以多次传入数据,可以多次返回数据 协程是可交互的 耗资源大小:进程 --& ...

  4. __str__ 和 __repr

    #1 默认类里面默认提供的__str__方法,是返回类的内存地址class foo: def __init__(self): pass #2 修改类里面默认提供的__str__方法class fun: ...

  5. cfile fopen fopen_s win10下打开文件失败

    vc程序在win10下面使用fopen 打开文件失败,在网上查 了下是因为UAC权限的问题如下: 由于windows vista win7 win8 win 10 添加了UAC权限,所以会导致 在系统 ...

  6. ListView的基本使用方法和RecyclerView的基本使用方法

    ListView是一种用于列表显示数据内容的控件,它可以通过适配器实现对于数据的列表显示,而RecyclerView是对于ListView优化后的列表数据显示控件. 个人对于List的使用经历多半在新 ...

  7. 离线部署 pm2

    1. install nodejs curl --silent --location https://rpm.nodesource.com/setup_6.x | sudo bash - 2.安装pm ...

  8. 如何强制停止http请求

    http请求很多时候会受到网络阻塞.重连等原因导致响应很慢,如果此时做了一些操作,但过几秒后又响应了之前的请求,就会造成很多问题,此时我们可以使用abort()方法强制停止http请求: let aj ...

  9. C++成员函数在内存中的存储方式

    用类去定义对象时,系统会为每一个对象分配存储空间.如果一个类包括了数据和函数,要分别为数据和函数的代码分配存储空间.按理说,如果用同一个类定义了10个对象,那么就需要分别为10个对象的数据和函数代码分 ...

  10. FortiGate防火墙500D下PC至外网丢包

    1.现状: 如图,防火墙堆叠,500D共4个出口方向,联通.电信.FQ.运维专线 2.现象: 到网关和防火墙上.下联口不丢包,到网联通和运维专线方向丢包4%左右,电信和FQ方向不丢包 3.分析 采用从 ...