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 ...
随机推荐
- 学习笔记 requests + BeautifulSoup
第一步:requests get请求 # -*- coding:utf-8 -*- # 日期:2018/5/15 17:46 # Author:小鼠标 import requests url = &q ...
- SpringBoot,SpringCloud入门到精通最简单教程
https://blog.csdn.net/ztx114/article/details/78091689
- 协程,greenlet,gevent
""" 协程 """ ''' 协程: 类似于一个可以暂停的函数,可以多次传入数据,可以多次返回数据 协程是可交互的 耗资源大小:进程 --& ...
- 用python探索和分析网络数据
Edited by Markdown Refered from: John Ladd, Jessica Otis, Christopher N. Warren, and Scott Weingart, ...
- Android 阻隔返回键
//阻隔 返回键 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.K ...
- js实现图片上传预览功能,使用base64编码来实现
实现图片上传的方法有很多,这里我们介绍比较简单的一种,使用base64对图片信息进行编码,然后直接将图片的base64信息存到数据库. 但是对于系统中需要上传的图片较多时并不建议采用这种方式,我们一般 ...
- JAVA字符串类
一.字符串类String1.String是一个类,位于java.lang包中2.创建一个字符串对象的2种方式: String 变量名=“值”; String 对象名=new String(“值”);3 ...
- 修改tomcat的编码方式,可以解决某些get请求乱码问题
在tomcat/conf/server.xml配置文件添加如下,修改tomcat的编码方式 <Connector URIEncoding="utf-8" connection ...
- R语言-箱型图&热力图
1.箱型图 boxplot()函数 > metals<-read.csv("metals.csv",header=TRUE) #读取文件和列名 > boxplot ...
- 项目(三)PXE高效能批量网络装机
PXE:预启动执行环境 PXE是由intel公司开发的网络引导技术,工作在Client/Server模式,允许客户机通过网络从远程服务器下载引导镜像,并加载安装文件或者整个操作系统. 若要搭建PXE网 ...