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从小到大排序然后取最小的.输出 ...
随机推荐
- spring mvc多个请求的影响 和使用全局变量
对于那些会以多线程运行的单例类(比如spring mvc中的controller,dao,service): 局部变量不会受多线程影响 成员变量会受到多线程影响 如果方法里有成员变量,只有读操作,不受 ...
- Jenkins高级用法 - Jenkinsfile 介绍及实战经验
系列目录 1.Jenkins 安装 2.Jenkins 集群 3.Jenkins 持续集成 - ASP.NET Core 持续集成(Docker&自由风格&Jenkinsfile) 4 ...
- centos安装rabbitmq
RabbitMQ是流行的开源消息队列系统,是AMQP(Advanced Message Queuing Protocol高级消息队列协议)的标准实现,用erlang语言开发.RabbitMQ据说具有良 ...
- 图解Go语言内存分配
目录 基础概念 内存管理单元 内存管理组件 mcache mcentral mheap 内存分配流程 总结 参考资料 Go语言内置运行时(就是runtime),抛弃了传统的内存分配方式,改为自主管理. ...
- SmartSql V3 重磅发布
超轻量级的ORM框架!107kb 更新内容 移除Dapper依赖 支持存储过程 增强扩展性 重构代码 优化缓存策略 动态实现仓储接口 支持 参数&结果映射 & TypeHandler ...
- 盘点 Python 中的那些冷知识(二)
上一篇文章分享了 Python中的那些冷知识,地址在这里 盘点 Python 中的那些冷知识(一) 今天将接着分享!! 06. 默认参数最好不为可变对象 函数的参数分三种 可变参数 默认参数 关键字参 ...
- 客户端浏览器- UWP兼容版本WebView
WebView简介 在win10之前,浏览器控件有WPF版本webBrowser.Winform版本WebBrowser,浏览器内核为IE. win10之后,微软不再维护原有的WebBrowser,转 ...
- DevExpress AspxGridView分页使用隐藏系统默认英文分页
1第一篇文章研究了怎么汉化,但是在实际使用过程中发现汉化的有小问题,DevExpress支持自定义按钮,也可以在属性中设置成中文,这样避免汉化不准确的问题 <dx:ASPxGridView ID ...
- C# xml序列化与反序列化 特性的使用
以下为将被序列化的类Entity: [XmlRoot("Root")] public class Entity { [XmlAttribute(AttributeName = &q ...
- SharePoint中你不知道的图片库(实战)
分享人:广州华软 无名 一. 前言 以前,在门户网站放一个图片幻灯片时,除了要自己开发实现外,还需要处理上传图片的大小,但在SharePoint中,使用图片库,无需花费时间和精力,就能马上实现. 二. ...