• Codeforces Round #104 (Div. 2) //缺E


    ----------------

    A. Lucky Ticket

    ---

    判断给出的数字是不是两半和相等的幸运数字。

    ---

    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int n;
        char s[111];
        cin>>n>>s;
        bool ok=true;
        for (int i=0;i<n;i++){
            if (s[i]!='4'&&s[i]!='7'){
                ok=false;
                break;
            }
        }
        if (!ok) cout<<"NO"<<endl;
        else{
            int a=0,b=0;
            for (int i=0;i<n;i++){
                if (i<n/2) a+=s[i]-'0';
                else b+=s[i]-'0';
            }
            if (a==b) cout<<"YES"<<endl;
            else cout<<"NO"<<endl;
        }
        return 0;
    }
    ----------------

    B. Lucky Mask

    ---

    找到比a大的最小的能砍成幸运数字的数字是多少。

    ---

    #include <iostream>
    #include <cmath>
    using namespace std;
    bool check(int a,int b){
        int n=0;
        int i=1;
        while (a>0){
            int t=a%10;
            a/=10;
            if (t==4||t==7){
                n=t*i+n;
                i*=10;
            }
        }
        if (n==b) return true;
        return false;
    }
    int main()
    {
        int a,b;
        cin>>a>>b;
        if (a<b) cout<<b<<endl;
        else{
            do{
                a++;
            }while (!check(a,b));
            cout<<a<<endl;
        }
        return 0;
    }
    ----------------

    C. Lucky Conversion

    ---

    两个只含有幸运数字的串。

    有两种操作,交换任意两个数字的位置,改变一个数字。

    求使两串相等的最小操作数。

    贪心,先尽可能的交换,然后改变剩下不相等数字的值。

    ---

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    char a[111111],b[111111];
    int main()
    {
        cin>>a>>b;
        int n=strlen(a);
        int res=0;
        int x=0,y=0;
        for (int i=0;i<n;i++){
            if (a[i]!=b[i]){
                if (a[i]=='4') x++;
                else y++;
            }
        }
        res=max(x,y);
        cout<<res<<endl;
        return 0;
    }
    ----------------

    D. Lucky Number 2

    ---

    给出4、7、47、74出现的次数。构造最小序列。

    首先考虑将47、74拼接成4747474...的形式。

    若4的数量与47相等。。则拼成74747的形式。

    将剩下的4插入第一个4的位置里。将剩下的7插入最后一个7的位置里。

    若某步中出现负数则说明不存在序列。

    ---

    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    using namespace std;
    int a1,a2,a3,a4;
    char s[2111111];
    char tmp[2111111];
    char ad4[2111111];
    char ad7[2111111];
    int l4,l7;
    int len,cnt;
    bool solve(){
        len=0;
        if (abs(a3-a4)>1) return false;
        if (a3==0&&a4==0&&a1*a2!=0) return false;
        int c4=0,c7=0;
        if (a3>a4){
            for (int i=0;i<a3;i++){
                s[len++]='4';
                s[len++]='7';
                c4++,c7++;
            }
        }
        else if (a3<a4){
            for (int i=0;i<a4;i++){
                s[len++]='7';
                s[len++]='4';
                c4++,c7++;
            }
        }
        else{
            if (a1<=a3){
                for (int i=0;i<a4;i++){
                    s[len++]='7';
                    s[len++]='4';
                    c4++,c7++;
                }
                s[len++]='7';
                c7++;
            }
            else{
                for (int i=0;i<a3;i++){
                    s[len++]='4';
                    s[len++]='7';
                    c4++,c7++;
                }
                s[len++]='4';
                c4++;
            }
        }
        a1=a1-(c4);
        a2=a2-(c7);
        if (a1<0||a2<0) return false;
        l4=l7=0;
        for (int i=0;i<a1;i++) ad4[l4++]='4';
        for (int i=0;i<a2;i++) ad7[l7++]='7';
        ad4[l4]=0;
        ad7[l7]=0;
        if (a3==0&&a4==0){
            if (a1>0) strcpy(s,ad4);
            else if (a2>0) strcpy(s,ad7);
            return true;
        }
        cnt=0;
        //cerr<<"text "<<s<<endl;
        if (a1>0){
            for (int i=0;i<len;i++){
                tmp[cnt++]=s[i];
                if (s[i]=='4'){
                    for (int j=0;j<l4;j++){
                        tmp[cnt++]=ad4[j];
                    }
                    for (int j=i+1;j<len;j++){
                        tmp[cnt++]=s[j];
                    }
                    break;
                }
            }
            tmp[cnt]=0;
            //strcpy(s,tmp);
            for (int i=0;i<=cnt;i++) s[i]=tmp[i];
            s[cnt]=0;
            len=cnt;
        }
        cnt=0;
        if (a2>0){
            int p=0;
            for (p=len-1;p>=0;p--){
                if (s[p]=='7') break;
            }
            //cerr<<"p="<<p<<endl;
            //cerr<<"ad7 "<<ad7<<endl;
            //cerr<<s<<endl;
            for (int i=0;i<len;i++){
                tmp[cnt++]=s[i];
                //cerr<<tmp[cnt-1]<<endl;
                if (i==p){
                    //cerr<<"move!"<<endl;
                    for (int j=0;j<l7;j++){
                        tmp[cnt++]=ad7[j];
                        //cerr<<tmp[cnt-1]<<endl;
                    }
                    for (int j=i+1;j<len;j++){
                        tmp[cnt++]=s[j];
                    }
                    break;
                }
            }
            tmp[cnt]=0;
            //cerr<<"templa "<<tmp<<endl;
            for (int i=0;i<=cnt;i++) s[i]=tmp[i];
            //strcpy(s,tmp);
            s[cnt]=0;
            len=cnt;
        }
        return true;
    }
    
    int main()
    {
        cin>>a1>>a2>>a3>>a4;
        if (!solve()) cout<<-1<<endl;
        else cout<<s<<endl;
        return 0;
    }


    ----------------


    ----------------


    ----------------

  • 相关阅读:
    BZOJ2821 作诗(Poetize) 【分块】
    BZOJ2724 蒲公英 【分块】
    Codeforces 17E Palisection 【Manacher】
    BZOJ2565 最长双回文串 【Manacher】
    Codeforces 25E Test 【Hash】
    CODEVS3013 单词背诵 【Hash】【MAP】
    HDU2825 Wireless Password 【AC自动机】【状压DP】
    HDU2896 病毒侵袭 【AC自动机】
    HDU3065 病毒侵袭持续中【AC自动机】
    HDU2222 Keywords Search 【AC自动机】
  • 原文地址:https://www.cnblogs.com/cyendra/p/3681584.html
Copyright © 2020-2023  润新知