CDOJ weekly question div2 This is a violence question (violence/thinking)

write picture description here

Yesterday, chipizz suddenly asked me how to write this question, and I was talking about it. It seemed very simple, so I went to CDOJ to register an account and plan to write.
The first idea of ​​getting this question is to prepend all the one-digit and two-digit numbers that can be composed of numbers that are divisible by 3, and then let k Take a modulo of 2 and then make a special judgment on xjb and it is over, but wa12.
After a little discussion with Mr. C, I found that there is a serious bug in this way of thinking. The set of data
3 4
1 2 5
should output 5511, but if I write it like this, it will output 5151.
Now that the bug is found, it is easy to fix it. After solving this bug, continue to submit——-wa7, how can you get ahead? ? ? I was shocked at the time.
Soon I thought of a set of data to hack the current program
4 5
1 4 6 7.
This set of data should output 77766, and I output 66666.
This set of data completely overturns the basis of all previous ideas, so we have to change our thinking.

After careful consideration, we can find that it actually affects the k only his rear k 3 digits, then we can directly output k 3 the largest number, then according to ( k 3 ) M a x N u m % 3 The obtained number can be rounded up to be divisible by 3, right?

a long time i f e l s e After that, submit again -- wa7
?????
I was shocked again, a question that should be simpler in div2, I did it for 3h and still didn't get an A? ? ?
Alright, alright, after calming down, I found out that when making numbers, 0 can be matched with 3/6, 1 can be matched with 2/5, and 2 can be matched with 1/4. Before, I only thought about making up 3!

OK, OK, finally AC.
In general, I personally feel that the problem is not difficult, but there are pits. If you fall into the pit and don't find it, it may take a while.

Ps. Let’s spit out CDOJ a little bit. Although I have probably waded 2 pages of status, there is nearly a page of Restricted Function in it. I have never seen this, and then I frantically tested which piece of code caused it. With such a return value, I was surprised to find that replacing cin and cout with scanf and printf would be no problem, which was a little depressing.

There are two codes below, one is written by myself, and the other is written by my teammates. You can take a look at it.

Personal Edition (longer, abu, very long)

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<vector>
#include<set>
#include<queue>
#include<limits.h>
#include<string.h>
#include<map>
#include<list>
using namespace std;
typedef long long ll;

#define inf int(0x3f3f3f3f)
#define mod int(1e9+7)
#define eps double(1e-6)
#define pi acos(-1.0)
#define lson  root << 1
#define rson  root << 1 | 1


int n,k;

struct edge
{
    int leave;
    int num;
}a[100005];

int vis[10005];
int s[100005];

bool cmp(edge x,edge y)
{
    return x.num>y.num;
}

int main()
{
    scanf("%d%d",&n,&k);
    int x1=0;
    int x2=0;
    int x0=0;
    memset(vis,0,sizeof(vis));
    memset(s,0,sizeof(s));
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i].num);
        vis[a[i].num]=1;
        int shu=a[i].num%3;
        a[i].leave=shu;
        if(shu==0)
            x0=1;
        else if(shu==1)
            x1=1;
        else if(shu==2)
            x2=1;
    }
    for(int i=0;i<100;i++)
    {
        int ss=i;
        int sum=0;
        int flag=1;
        if(vis[i]&&i==0)
            s[i]=1;
        while(ss)
        {
            int sss=ss%10;
            ss/=10;
            if(!vis[sss])
                flag=0;
            sum+=sss;
        }
        if(sum%3==0&&flag&&sum!=0)
            s[i]=1;
    }
    if(k==2)
    {
        int s1=-1;
        int s2=-1;
        int s3=-1;
        for(int i=99;i>=10;i--)
        {
            if(s[i])
            {
                //printf("%d\n",i);
                s1=i/10;
                s2=i%10;
                break;
            }
        }
        for(int i=9;i>=0;i--)
        {
            if(s[i])
            {
                //printf("%d%d\n",i,i);
                s3=i;
                break;
            }
        }
        if(s1==-1&&s2==-1&&s3==-1)
        {
            printf("-1\n");
            return 0;
        }
        else
        {
            if(s1>s3)
            {
                printf("%d%d\n",s1,s2);
            }
            else if(s1==s3)
            {
                if(s2>=s3)
                    printf("%d%d\n",s1,s2);
                else
                    printf("%d%d\n",s3,s3);
            }
            else if(s1<s3)
            {
                if(s3!=0)
                    printf("%d%d\n",s3,s3);
                else
                    printf("-1\n");
            }
            return 0;
        }
    }
    else if(k==1)
    {
        for(int i=9;i>=0;i--)
        {
            if(s[i])
            {
                printf("%d\n",i);
                return 0;
            }
        }
        printf("-1\n");
        return 0;
    }
    sort(a,a+n,cmp);
    if(a[0].num==0)
    {
        if(k!=1)
            printf("-1\n");
        return 0;
    }
    if(x1+x2+x0>=2)
    {

        int shu1=a[0].num;

        int lef=(k-3)*(a[0].leave)%3;
        int Max1=-1;
        int Max2=-1;
        int Max0=-1;
        for(int i=0;i<n;i++)
        {
            if(Max0==-1&&a[i].leave==0)
                Max0=a[i].num;
            if(Max1==-1&&a[i].leave==1)
                Max1=a[i].num;
            if(Max2==-1&&a[i].leave==2)
                Max2=a[i].num;
        }
        if(lef==0)
        {
            for(int i=0;i<k;i++)
                printf("%d",shu1);
            printf("\n");
        }
        else if(lef==1)//5 /2 2 1/0 0 2/1 1 0
        {
            //cout<<111<<endl;
            for(int i=0;i<k-3;i++)
                printf("%d",a[0].num);
            if(Max0>=Max1&&Max0>=Max2)
            {
                if(Max1!=-1&&Max2!=-1)
                    printf("%d%d%d\n",Max0,Max0,Max2);
                else if(Max2==-1&&Max1!=-1)
                    printf("%d%d%d\n",Max0,Max1,Max1);
                else if(Max1==-1&&Max2!=-1)
                    printf("%d%d%d\n",Max0,Max0,Max2);
                else
                    printf("-1\n");
            }
            else if(Max1>=Max0&&Max1>=Max2)
            {
                if(Max0!=-1&&Max2!=-1)
                    printf("%d%d%d\n",Max1,Max1,Max0);
                else if(Max0!=-1&&Max2==-1)
                    printf("%d%d%d\n",Max1,Max1,Max0);
                else if(Max0==-1&&Max2!=-1)
                    printf("%d%d%d\n",Max1,Max2,Max2);
                else
                    printf("-1\n");
            }
            else if(Max2>=Max1&&Max2>=Max0)
            {
                if(Max1!=-1&&Max0!=-1)
                    printf("%d%d%d\n",Max2,Max2,Max1);
                else if(Max1!=-1&&Max0==-1)
                    printf("%d%d%d\n",Max2,Max2,Max1);
                else if(Max1==-1&&Max0!=-1)
                    printf("%d%d%d\n",Max2,Max0,Max0);
                else
                    printf("-1\n");
            }
        }
        else if(lef==2)//4/1   0 0 1/ 2 1 1/2 2 0
        {
            //cout<<111<<endl;
            //cout<<Max0<<" "<<Max1<<" "<<Max2<<endl;
            for(int i=0;i<k-3;i++)
                printf("%d",shu1);
            if(Max2>=Max0&&Max2>=Max1)
            {
                if(Max0!=-1)
                    printf("%d%d%d\n",Max2,Max2,Max0);
                else if(Max1!=-1)
                    printf("%d%d%d\n",Max2,Max1,Max1);
                else
                    printf("-1\n");
            }
            else if(Max1>=Max2&&Max1>=Max0)
            {
                if(Max2!=-1)
                    printf("%d%d%d\n",Max1,Max1,Max2);
                else if(Max0!=-1)
                    printf("%d%d%d\n",Max1,Max0,Max0);
                else
                    printf("-1\n");
            }
            else if(Max0>=Max1&&Max0>=Max2)
            {
                if(Max1!=-1&&Max2!=-1)
                {
                    printf("%d%d%d\n",Max0,Max0,Max1);
                }
                else if(Max1!=-1&&Max2==-1)
                    printf("%d%d%d\n",Max0,Max0,Max1);
                else if(Max1==-1&&Max2!=-1)
                    printf("%d%d%d\n",Max0,Max2,Max2);
                else
                    printf("-1\n");
            }
        }
    }
    else
    {
        if(x0==1)
        {
            for(int i=0;i<k;i++)
                printf("%d",a[0].num);
            printf("\n");
        }
        else
        {
            if(k%3==0)
            {
                for(int i=0;i<k;i++)
                    printf("%d",a[0].num);
                printf("\n");
            }
            else
                printf("-1\n");
        }
    }
}

Teammate version (shorter)

#include<bits/stdc++.h>
#define fi first
#define se second
#define lson l,mid,o<<1
#define rson mid+1,r,o<<1|1
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int, int> P;
typedef pair<int, P> PII;
const LL INF = 0x3f3f3f3f;
const int N = 1e6 + 10;
const int M = 110;
const LL mod = 1e9 + 7;
const double PI=acos(-1);


int v[3];
int ans[N];
int main()
{
    int n, m, x, mm = -1;
    scanf("%d%d", &n, &m);
    v[0] = v[1] = v[2] = -1;
    for(int i = 1; i <= n; ++i)
    {
        scanf("%d", &x);
        v[x % 3] = max(v[x % 3], x);
        mm = max(mm, x);
    }
    if(m >= 2 && mm == 0)
    {
        puts("-1");
        return 0;
    }
    bool flag = true;
    int zz = m % 3;
    for(int i = zz; i < m; ++i) ans[i] = mm;
    if(zz == 1)
    {
        if(v[0] != -1)
        {
            ans[0] = v[0];
        }
        else if(v[1] != -1 && v[2] != -1 && m >= 4)
        {
            ans[0] = ans[1] = v[(mm % 3) ^ 3];
        }
        else flag = false;
    }
    else if(zz == 2)
    {
        if(v[0] != -1 && (v[1] != -1 && v[2] != -1))
        {
            if(mm % 3 == 0) ans[0] = ans[1] = mm;
            else    ans[0] = v[(mm % 3) ^ 3], ans[1] = mm;
        }
        else if(v[0] != -1)
        {
            ans[0] = ans[1] = v[0];
        }
        else if(v[1] != -1 && v[2] != -1)
        {
            ans[0] = v[(mm % 3) ^ 3];
            ans[1] = mm;
        }
        else flag = false;
    }
    if(flag)
    {
        for(int i = m - 1; i >= 0; --i) printf("%d", ans[i]);
        puts("");
    }
    else
    {
        puts("-1");
    }
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325714409&siteId=291194637