Divisibility by 25 CodeForces - 988E (技巧的暴力)
You are given an integer nn from 11 to 10181018 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 2525? Print -1 if it is impossible to obtain a number that is divisible by 2525.
Input
The first line contains an integer nn (1≤n≤10181≤n≤1018). It is guaranteed that the first (left) digit of the number nn is not a zero.
Output
If it is impossible to obtain a number that is divisible by 2525, print -1. Otherwise print the minimum number of moves required to obtain such number.
Note that you can swap only adjacent digits in the given number.
Examples
5071
4
705
1
1241367
-1
Note
In the first example one of the possible sequences of moves is 5071 →→ 5701 →→ 7501 →→ 7510 →→ 7150.
题意:
给你一个整数,你可以交换这个整数中的相邻的数位,但交换后的数不能有前导0,请你输出最小的交换次数,使之可以被25整除。
思路:
我们应该知道,>1且可以被25整除的数,后两位一定是00,25,50,75,这四个整数。
那么我们先通过记录这个数n的每一位数的数次,来判断是否是输出-1,
如果出现过2次0,或者1次2一次5,或者一次5一次0,一次7一次5都可以确定一定可以交换出想要的数。否则就是-1.
确定一定可以交换出想要的数后,我们就要想法记录出最小的移动次数。
我们可以知道,如果我们想要最后两位是50,那么我们要把一个数字0移动到最后一位,我们想移动次数最小,那么一定是选择移动最靠右边的那个0,5一定是最靠右的5.其他借此类推。
我们怎么记录移动的次数呢?
我们用STL中的string类型中的rfind函数(从右开始查找)
然后如果想找的字符的下标是i,那么需要移动len-i-1次,len是数组的当前长度。
然后把这个字符从string中删除掉,(消除对下一个字符的影响)。
然后另外一个字符同理。
我们要注意移动的结果可能会有前导0的,那么我们只需要记录有多少个前导0,就可以通过移动多少次来把签到0后面的字符移动到第一位。
取多个值中的最小值就是ans。
细节见代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=;while(b){if(b%)ans=ans*a%MOD;a=a*a%MOD;b/=;}return ans;}
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
string s,t;
int cnt[maxn];
int len;
int check(string s,char a,char b)
{
int res=;
if(s.rfind(a)!=-)
{
int id=s.rfind(a);
res+=len-id-;
s.erase(s.rfind(a),);
if(s.rfind(b)!=-)
{
id=s.rfind(b);
res+=len-id-;
s.erase(s.rfind(b),);
while(s[]=='')
{
res++;
s.erase(,);
}
return res;
}else
{
return inf;
}
}else
{
return inf;
}
}
int main()
{
//freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
//freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
//gbtb;
cin>>s;
t=s;
len=s.length();
rep(i,,len)
{
cnt[s[i]]++;
}
if((cnt['']>=)||(cnt['']>=&&cnt['']>=)||(cnt['']>=&&cnt['']>=)||(cnt['']>=&&cnt['']>=))
{
int ans=inf;
ans=min(ans,check(t,'',''));
ans=min(ans,check(t,'',''));
ans=min(ans,check(t,'',''));
ans=min(ans,check(t,'',''));
cout<<ans<<endl;
}else
{
cout<<-<<endl;
}
return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}
Divisibility by 25 CodeForces - 988E (技巧的暴力)的更多相关文章
- Divisibility by 25 CodeForces - 988E
You are given an integer nn from 11 to 10181018 without leading zeroes. In one move you can swap any ...
- Divisibility by 25 CodeForces - 988E(模拟)
遇见模拟题 有两种做法 例如这题: 1.直接去算次数(统计哪个数在第几位,然后去运算) 2.模拟操作 贴一个别人的代码...https://blog.csdn.net/weixin_39453270/ ...
- Codeforces Round #486 (Div. 3) E. Divisibility by 25
Codeforces Round #486 (Div. 3) E. Divisibility by 25 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...
- CF 988E Divisibility by 25 思维 第十二
Divisibility by 25 time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- Codeforces A. Playlist(暴力剪枝)
题目描述: Playlist time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- 你应该了解的25个JS技巧
目录 1. 类型检查小工具 2. 检查是否为空 3. 获取列表最后一项 4. 带有范围的随机数生成器 5. 随机 ID 生成器 6. 创建一个范围内的数字 7. 格式化 JSON 字符串,string ...
- Codeforces Round #486 (Div. 3)988E. Divisibility by 25技巧暴力||更暴力的分类
传送门 题意:给定一个数,可以对其做交换相邻两个数字的操作.问最少要操作几步,使得可以被25整除. 思路:问题可以转化为,要做几次交换,使得末尾两个数为00或25,50,75: 自己一开始就是先for ...
- Codeforces 988E. Divisibility by 25
解题思路: 只有尾数为25,50,75,00的数才可能是25的倍数. 对字符串做4次处理,以25为例. a. 将字符串中的最后一个5移到最后一位.计算交换次数.(如果没有找到5,则不可能凑出25,考虑 ...
- [codeforces 200 A Cinema]暴力,优化
题意大致是这样的:有一个有n行.每行m个格子的矩形,每次往指定格子里填石子,如果指定格子里已经填过了,则找到与其曼哈顿距离最小的格子,然后填进去,有多个的时候依次按x.y从小到大排序然后取最小的.输出 ...
随机推荐
- 每日分享!~ JavaScript数组去重
数组去重 数组去重在很多面试的过程中,都是大题出现!网络上出现了很多数组去重的方式.多数的达到了12种以上. 今天我只给大家介绍两种我比较喜欢,比较认可!入手简单的-能解决自己的问题就可以了 好了 , ...
- 使用描述符实现property功能
# Author : Kelvin # Date : 2019/1/25 14:46 class Decproperty: def __init__(self, func): self.func = ...
- Python基础(time模块,datetime模块)
#Author : Kelvin #Date : 2019/1/6 15:10 import time #获取此时的时间戳(从此刻到1970年一月一号零点的秒数) res1=time.time() p ...
- C#代码安装Windows服务(控制台应用集成Windows服务)
最近在为一款C/S架构的科研软件开发云计算版,需要用到WCF,考虑到不需要什么界面以及稳定性,无人值守性,准备用Windows Service作为宿主,无奈Windows Service的安装太为繁复 ...
- linux-2.6.18源码分析笔记---中断
一.中断初始化 中断的一些硬件机制不做过多的描述,只介绍一些和linux实现比较贴近的机制,便于理解代码. 1.1 关于intel和linux几种门的简介 intel提供了4种门:系统门,中断门,陷阱 ...
- sql 修改、更新、替换 某个字段的部分内容(转载)
来源:https://blog.csdn.net/jiangnanqbey/article/details/81304834 1*.需求 将表(Ws_FormMain)的字段(order_Number ...
- java爬虫系列第一讲-爬虫入门
1. 概述 java爬虫系列包含哪些内容? java爬虫框架webmgic入门 使用webmgic爬取 http://ady01.com 中的电影资源(动作电影列表页.电影下载地址等信息) 使用web ...
- 【Redis】redis各类型数据存储分析
一.简介和应用 Redis是一个由ANSI C语言编写,性能优秀.支持网络.可持久化的K-K内存数据库,并提供多种语言的API.它常用的类型主要是 String.List.Hash.Set.ZSet ...
- Eclipse设置全局用户名
-Duser.name=你的名字
- java基础(三):谈谈java异常的处理
1.知识点总结 1.1.异常分类 异常就是java中出现的不正常的现象(错误与异常),按照继承的体系结构,可以分类如下 Throwable: 它是所有错误与异常的超类(祖宗类) |- Error 错误 ...