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

传送门

题意:给定一个数,可以对其做交换相邻两个数字的操作。问最少要操作几步,使得可以被25整除。

思路:问题可以转化为,要做几次交换,使得末尾两个数为00或25,50,75;

  自己一开始就是先for一遍,记录四种可能对于的步数,再对四种可能讨论(有前导0的情况);自己是在数据中,该对了自己的代码,

  看了队长和%王宣凯的代码,觉得那才是现场能ac的思路。--暴力交换;

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <iterator>
#include <cmath>
using namespace std;

#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue

#define Pll pair<ll,ll>
#define Pii pair<int,int>

#define fi first
#define se second

#define OKC ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
typedef long long ll;
typedef unsigned long long ull;
const int inf = 0x3f3f3f3f;

/*-----------------show time----------------*/
ll n;
string str;
int a[5][3];
int main(){

    cin>>str;
    int len = str.length();
    reverse(str.begin(),str.end());
    int flag0=0,flag5=0;
    memset(a,inf,sizeof(a));
    for(int i=0;i<len; i++)         //先记录所有可能的四种情况。
    {
        if(str[i]==‘2‘)
        {
            if(a[1][2]==inf)a[1][2] = i-1;
        }
        else if(str[i]==‘0‘)
        {
            if(a[2][2]!=inf&&a[2][1]==inf)
            {
                a[2][1] = i + 1;
            }
            else if(a[2][1]==inf)
            {
                a[2][1] = i;
            }
            if(a[4][1]==inf)
            {
                a[4][1] = i;
            }
            else if(a[4][2]==inf)a[4][2] = i-1;
        }
        else if(str[i]==‘5‘)
        {
            if(a[1][2]!=inf&&a[1][1]==inf)
            {
                a[1][1] = i + 1;
            }
            else if(a[1][1]==inf)
            {
                a[1][1] = i;
            }

            if(a[2][2]==inf)a[2][2] = i-1;

            if(a[3][2]!=inf&&a[3][1]==inf)
            {
                a[3][1] = i + 1;
            }
            else if(a[3][1]==inf)
            {
                a[3][1] = i;
            }
        }
        else if(str[i]==‘7‘)
        {
             if(a[3][2]==inf)a[3][2] = i-1;
        }
    }
    int ans = -1;
    for(int i=1; i<=4;i ++)                             //这里要确定会不会在交换中有0的情况。
    {
        if(a[i][1]!=inf&&a[i][2]!=inf&&(i==1||i==3))
        {
                char q,w;
                int id = i;
                if(id==1) q = ‘2‘,w = ‘5‘;
                // if(id==2) q = ‘5‘,w = ‘0‘;
                if(id==3) q = ‘7‘,w = ‘5‘;
                // if(id==4) q = ‘0‘,w = ‘0‘;
                int tot = 0;
                // debug(id);
                int flag1 = 0,flag2 = 0;
                for(int i=0;i<len; i++)
                {
                    if(q==str[i]&&i!=len-1)flag1 = 1;
                    if(w==str[i]&&i!=len-1)flag2 = 1;
                }
                if(flag1==0||flag2==0)
                    for(int i=len-1; i>=0; i--)
                    {
                        if(str[i] != q && str[i]!= w &&str[i]!=‘0‘)break;
                        if(str[i]==‘0‘)tot++;
                    }
                int tmp = a[i][1] + a[i][2] + tot;
                if(ans==-1)ans =tmp;
                else ans = min(ans,tmp);
        }
        else if(a[i][1]!=inf&&a[i][2]!=inf)
        {
                char q,w;
                q = ‘5‘,w = ‘0‘;
                int tot = 0;
                // debug(id);
                int flag1 = 0,flag2 = 0;
                int tt = 0;     //记录0的个数
                for(int i=0;i<len; i++)
                {
                    if(q==str[i]&&i!=len-1)flag1 = 1;
                    if(w==str[i]&&i!=len-1)tt++;
                }
                if(i==4||tt==1)flag1=1,flag2=1;
                if(flag1==0||flag2==0)
                {
                     for(int i=len-1; i>=0; i--)
                     {
                        if(str[i] != q &&str[i]!=‘0‘)break;
                        if(str[i]==‘0‘)tot++;
                     }
                     tot--;
                }
                int tmp = a[i][1] + a[i][2] + tot;
                if(ans==-1)ans =tmp;
                else ans = min(ans,tmp);
        }
    }
    cout<<ans<<endl;
    return 0;
}

自己写的分类讨论

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <iterator>
#include <cmath>
using namespace std;

#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue

#define Pll pair<ll,ll>
#define Pii pair<int,int>

#define fi first
#define se second

#define OKC ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
typedef long long ll;
typedef unsigned long long ull;
const int inf = 0x3f3f3f3f;

/*-----------------show time----------------*/

string str,tmp;
int cnt = 0;
int sw(char a,int len)
{
    for(int i=len;i>=0;i--)
    {
        int t = 0;
        if(tmp[i]==a)
        {
            for(int j=i;j<len;j++)
                swap(tmp[j],tmp[j+1]),t++;
            return t;
        }
    }
    return inf;
}
int main(){
    cin>>str;
    int len = str.length();
    int ans = inf;
    //00
    int x;
    tmp = str;
    x = sw(‘0‘,len-1);
    x += sw(‘0‘,len-2);
    int i;
    for(cnt = 0, i=0; i<len&&tmp[i]==‘0‘ ;i++)cnt++;
        ans = min(ans,x + cnt);
        //一开始写成了for(int  i=0,cnt = 0; i<len&&tmp[i]==‘0‘ ;i++)cnt++;
        //使得cnt的计数出了循环就没了效果。
    //25
    tmp = str;
    x = sw(‘5‘,len-1);
    x += sw(‘2‘,len-2);

    for(cnt = 0, i=0; i<len&&tmp[i]==‘0‘ ;i++)cnt++;
        ans = min(ans,x + cnt);
    //75
    tmp = str;
    x = sw(‘5‘,len-1);
    x += sw(‘7‘,len-2);

    for(cnt = 0, i=0; i<len&&tmp[i]==‘0‘ ;i++)cnt++;
         ans = min(ans,x + cnt);

    //50
    tmp = str;
    x = sw(‘0‘,len-1);
    x += sw(‘5‘,len-2);

    for(cnt = 0, i=0; i<len&&tmp[i]==‘0‘ ;i++)cnt++;
        ans = min(ans,x + cnt);
    if(ans>=inf)puts("-1");
    else
        cout<<ans<<endl;
    return 0;
}

%mxk

原文地址:https://www.cnblogs.com/ckxkexing/p/9141598.html

时间: 2024-11-10 15:22:17

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

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

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

数学/找规律/暴力 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 #286 (Div. 2)B. Mr. Kitayuta&#39;s Colorful Graph(dfs,暴力)

数据规模小,所以就暴力枚举每一种颜色的边就行了. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<string> #include<cmath> #include<map> #include<set> #include<vector> #include<algorithm>

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