Codeforces Round #486 (Div. 3) E. Divisibility by 25

E. Divisibility by 25

能被25整除的充要条件就是末两位是00,25,50,75。如果没有过程中不出现前导0这一限制,显然对每种情况,贪心取尽量低位即可。本题的关键就在于如何满足这个条件,首先有个”显然”的方法:讨论。。。然后会发现情况太多,过于复杂。所以,我们只好从交换本身的性质入手,找找易于实现的写法。注意到我们最多移动3个数字的位置,最终两个最低位的数,可能还有一个非0数作为最高位,而根据交换的性质,可以发现先移动那个数对于最终的结果没有影响,按照题意我们要先移动那个作为最高位的数,那现在既然调换顺序没有影响,不如把那个数留到最后再交换。于是,这道题的做法就出来了:1)贪心模拟,确定最低两位2)贪心模拟,确定最高位3)检查最后两位是否合法。

#include <bits/stdc++.h>
typedef long long ll;
const int inf = 0x3f3f3f3f;
using namespace std;
int n;
char s[22],tmp[22];
int solve(char a, char b) {
    int p=n-1, ans=0;
    for(p;p>=0;--p)if(s[p]==b)break;
    if(p==-1)return inf;
    for(int i=p+1;i<n;++i)swap(s[i],s[i-1]),++ans;
    p=n-2;
    for(p;p>=0;--p)if(s[p]==a)break;
    if(p==-1)return inf;
    for(int i=p+1;i<n-1;++i)swap(s[i],s[i-1]),++ans;
    for(p=0;p<n;++p)if(s[p]!=‘0‘)break;
    for(;p>=1;--p)swap(s[p],s[p-1]),++ans;
    if(s[n-2]==a&&s[n-1]==b) return ans;
    return inf;
}
int main() {
    scanf(" %s",s);
    memcpy(tmp,s,sizeof(s));
    n=strlen(s);
    int ans = inf;
    ans = min(ans,solve(‘7‘,‘5‘));
    memcpy(s,tmp,sizeof(tmp));
    ans = min(ans,solve(‘2‘,‘5‘));
    memcpy(s,tmp,sizeof(tmp));
    ans = min(ans,solve(‘5‘,‘0‘));
    memcpy(s,tmp,sizeof(tmp));
    ans = min(ans,solve(‘0‘,‘0‘));
    printf("%d\n",(ans==inf)?-1:ans);
    return 0;
}

原文地址:https://www.cnblogs.com/RRRR-wys/p/9124283.html

时间: 2024-10-10 20:11:22

Codeforces Round #486 (Div. 3) E. Divisibility by 25的相关文章

Codeforces Round #486 (Div. 3)988E. Divisibility by 25技巧暴力||更暴力的分类

传送门 题意:给定一个数,可以对其做交换相邻两个数字的操作.问最少要操作几步,使得可以被25整除. 思路:问题可以转化为,要做几次交换,使得末尾两个数为00或25,50,75: 自己一开始就是先for一遍,记录四种可能对于的步数,再对四种可能讨论(有前导0的情况):自己是在数据中,该对了自己的代码, 看了队长和%王宣凯的代码,觉得那才是现场能ac的思路.--暴力交换: #include <iostream> #include <cstdio> #include <algori

数学/找规律/暴力 Codeforces Round #306 (Div. 2) C. Divisibility by Eight

题目传送门 1 /* 2 数学/暴力:只要一个数的最后三位能被8整除,那么它就是答案:用到sprintf把数字转移成字符读入 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstring> 7 #include <iostream> 8 #include <cmath> 9 #include <vector> 10 using namespace std; 11

Codeforces Round #486 (Div. 3) C. Equal Sums

Codeforces Round #486 (Div. 3) C. Equal Sums 题目连接: http://codeforces.com/group/T0ITBvoeEx/contest/988/problem/C Description You are given k sequences of integers. The length of the i-th sequence equals to ni. You have to choose exactly two sequences

Codeforces Round #486 (Div. 3) A. Diverse Team

Codeforces Round #486 (Div. 3) A. Diverse Team 题目连接: http://codeforces.com/contest/988/problem/A Description There are n students in a school class, the rating of the i-th student on Codehorses is ai. You have to form a team consisting of k students

Codeforces Round #486 (Div. 3) B. Substrings Sort

Codeforces Round #486 (Div. 3) B. Substrings Sort 题目连接: http://codeforces.com/contest/988/problem/B Description You are given n strings. Each string consists of lowercase English letters. Rearrange (reorder) the given strings in such a way that for e

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/T0ITBvoeEx/contest/988/problem/D Description There are n distinct points on a coordinate line, the coordinate of i-th point equals to xi. Choose a subset of

Codeforces Round #486 (Div. 3) F. Rain and Umbrellas

Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/contest/988/problem/E Description Polycarp lives on a coordinate line at the point x=0. He goes to his friend that lives at the point x=a. Polycarp can

Codeforces Round #486 (Div. 3)

988A.http://codeforces.com/contest/988/problem/A 题意:给出n个数,让你从中取出m个不同的数组成一组 分析:模拟即可.当每个人为第一次出现时,标记这个位置可取.最后从可取的位置取m个即可 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<vector> 5 using namespace std; 6 const in

【赛时总结】◇赛时&#183;V◇ Codeforces Round #486 Div3

◇赛时·V◇ Codeforces Round #486 Div3 又是一场历史悠久的比赛,老师拉着我回来考古了--为了不抢了后面一些同学的排名,我没有做A题 ◆ 题目&解析 [B题]Substrings Sort +传送门+   [暴力模拟] 题意 给出n个字符串,你需要将它们排序,使得对于每一个字符串,它前面的字符串都是它的子串(对于字符串i,则字符串 1~i-1 都是它的子串). 解析 由于n最大才100,所以 O(n3) 的算法都不会爆,很容易想到暴力模拟. 如果字符串i是字符串j的子串