Submission #821628

# Submission time Handle Problem Language Result Execution time Memory
821628 2023-08-11T12:13:21 Z PVM_pvm Watermelon (INOI20_watermelon) C++17
31 / 100
410 ms 3400 KB
#include<bits/stdc++.h>
using namespace std;
#define MAXX 100007
#define MAXN 13
int b[MAXX],b2[MAXX],n,k;
struct perm
{
    int ch[MAXN];
    perm(int a[])
    {
        for (int q=1;q<=n;q++) ch[q]=a[q];
    }
    void show()
    {
        //cout<<"P:";
        for (int q=1;q<=n;q++) cout<<ch[q]<<" ";
        cout<<"\n";
    }
};
vector<perm> ans;
int cur[MAXN],sb[MAXN];
bool dt[MAXN];
void check()
{
    for (int q=1;q<=n;q++) sb[q]=-1;
    for (int q=1;q<=n;q++) dt[q]=false;
    for (int tm=1;true;tm++)
    {
        bool smyrt=false;
        for (int q=1;q<=n;q++)
        {
            if (dt[q]) continue;
            int next=q+1;
            for (next=q+1;next<=n;next++)
            {
                if (!dt[next]) break;
            }
            if (next>n) break;
            if (cur[next]>cur[q])
            {
                dt[q]=true;
                smyrt=true;
                sb[q]=tm;
            }
        }
        if (!smyrt) break;
    }
    bool pr=true;
    for (int q=1;q<=n;q++)
    {
        if (sb[q]!=b[q]) pr=false;
    }
    if (pr)
    {
        perm spr(cur);
        ans.push_back(spr);
    }
}
int ind[MAXN];
int ind2[MAXN];
bool cmp(perm &a, perm &b){
    for (int q=1;q<=n;q++)
    {
        ind[a.ch[q]] = q;
        ind2[b.ch[q]] = q;
    }
    for (int q=1;q<=n;q++)
    {
        if (ind[q] < ind2[q]) return 1;
        if (ind[q] > ind2[q]) return 0;
    }
    return 0;
}
int ansk[MAXX];
int pr1[MAXX];
int chb[MAXX];
int posOfLST[MAXX];
int bfMe[MAXX];
bool finalcheck()
{
    stack<pair<int,int>> st;
    chb[n]=-1;
    //if (chb[n]!=b[n]) return false;
    st.push({ansk[n],n});
    for (int q=n-1;q>=1;q--)
    {
        chb[q]=-2;
        while (true)
        {
            if (st.empty())
            {
                chb[q]=-1;
                st.push({ansk[q],q});
                break;
            }
            int chislo = st.top().first;
            int pos = st.top().second;
            if (ansk[q]>chislo)
            {
                chb[q]=max(chb[q],chb[pos]);
                st.pop();
                continue;
            }
            if (chb[q]==-2) chb[q]=1;
            else chb[q]++;
            st.push({ansk[q],q});
            break;
        }
        //cout<<q<<":"<<chb[q]<<" "<<b[q]<<"\n";
        //if (chb[q]!=b[q]2) return false;
    }
    for (int q=1;q<=n;q++)
    {
        if (chb[q]!=b2[q]) return false;
    }
    return true;
}
int lf[MAXN],rf[MAXN];
void printthem()
{
        for (int q=1;q<=n;q++)
        {
            cout<<ansk[q]<<" ";
        }
        cout<<"\n";
}
void getSecond()
{
    int lst=0;
    for (int q=1;q<=n;q++)
    {
        if (b[q]==-1)
        {
            lf[q]=lst;
        }
        if (b[q]==1)
        {
            lst=q;
        }
    }
    lst=0;
    for (int q=n;q>=1;q--)
    {
        if (b[q]==-1)
        {
            rf[q]=lst;
        }
        if (b[q]==1)
        {
            lst=q;
        }
    }
    int pos=0;
    for (int q=1;q<=n;q++)
    {
        if (lf[q]!=0)
        {
            pos=q;
            break;
        }
    }
    if (pos==0)
    {
        cout<<"-1\n";
        return;
    }
    if (b[n-1]==-1)
    {
        int ch=ansk[n];
        int hpos=0;
        for (int q=1;q<=n;q++)
        {
            if (ansk[q]==ch-1)
            {
                hpos=q;
            }
        }
        swap(ansk[hpos],ansk[n]);
        printthem();
        return;
    }
    int f1=0;
    for (int q=n;q>=1;q--)
    {
        if (b[q]==1)
        {
            f1=q;
            break;
        }
    }
    bool minahminus=false;
    int him4=0;
    for (int q=f1;q>=1;q--)
    {
        if (b[q]==-1) minahminus=true;
        if (b[q]==1 && minahminus)
        {
            him4=q;
            while (bfMe[him4]!=0)
            {
                him4=bfMe[him4];
            }
        }
    }
    int him=0;
    for (int q=n;q>=1;q--)
    {
        if (b[q]==1)
        {
            int w=q;
            while (bfMe[w]!=0)
            {
                w=bfMe[w];
            }
            if (w!=q)
            {
                him=q;
                break;
            }
        }
    }
    if (him==0)
    {
        if (him4!=0)
        {
            swap(ansk[him4],ansk[f1]);
            printthem();
            return;
        }
        if (him==0)
        {
            cout<<"-1\n";
        }
        return;
    }
    int him2=0;
    for (int q=him-1;q>=1;q--)
    {
        if (b[q]==1)
        {
            him2=q;
            while (bfMe[him2]!=0)
            {
                him2=bfMe[him2];
            }
            break;
        }
    }
    if (him2==0)
    {
        if (him4!=0)
        {
            swap(ansk[him4],ansk[f1]);
            printthem();
            return;
        }
        cout<<"-1\n";
        return;
    }
    if (ansk[him]>ansk[him4]) swap(ansk[him],ansk[him2]);
    else swap(ansk[him4],ansk[f1]);
    printthem();
}
int main()
{
    cin>>n>>k;
    if (k!=1 && k!=2)
    {
        for (int q=1;q<=n;q++) cin>>b[q];
        for (int q=1;q<=n;q++) cur[q]=q;
        check();
        while (next_permutation(cur+1,cur+n+1)) check();
        sort(ans.begin(),ans.end(),cmp);
        if (k<ans.size()) ans[k-1].show();
        else cout<<"-1\n";
        return 0;
    }
    if (k==1)
    {
        for (int q=1;q<=n;q++) cin>>b[q];
        for (int q=1;q<=n;q++) b2[q]=b[q];
        int br2=0;
        for (int q=1;q<=n;q++)
        {
            if (b[q]!=-1) br2++;
        }
        if (b[n]==-1) posOfLST[n+2]=n;
        else posOfLST[b[n]]=n;
        for (int q=n-1;q>=1;q--)
        {
            if (b[q]==-1) continue;
            if (b[q]==1) {posOfLST[b[q]]=q;continue;}
            int pos=posOfLST[b[q]-1];
            if (pos==0)
            {
                cout<<"-1\n";
                return 0;
            }
            if (bfMe[pos]!=0)
            {
                cout<<"-1\n";
                return 0;
            }
            bfMe[pos]=q;
            posOfLST[b[q]]=q;
        }
        int dok=1;
        for (int q=1;q<=n;q++)
        {
            if (b[q]==1)
            {
                ansk[q]=dok;
                //cout<<q<<" "<<dok<<"\n";
                dok++;
                int w=bfMe[q];
                while (w!=0)
                {
                    ansk[w]=dok;
                    //cout<<w<<" "<<dok<<"\n";
                    dok++;
                    w=bfMe[w];
                }
            }
        }
        int tm=n;
        for (int q=1;q<=n;q++)
        {
            if (ansk[q]==0)
            {
                ansk[q]=tm;
                tm--;
            }
        }
        if (finalcheck())
        {
            for (int q=1;q<=n;q++)
            {
                cout<<ansk[q]<<" ";
            }
            cout<<"\n";
        }
        else
        {
            cout<<"-1\n";
        }
    }
    else
    {
        for (int q=1;q<=n;q++) cin>>b[q];
        for (int q=1;q<=n;q++) b2[q]=b[q];
        int br2=0;
        for (int q=1;q<=n;q++)
        {
            if (b[q]!=-1) br2++;
        }
        if (b[n]==-1) posOfLST[n+2]=n;
        else posOfLST[b[n]]=n;
        for (int q=n-1;q>=1;q--)
        {
            if (b[q]==-1) continue;
            if (b[q]==1) {posOfLST[b[q]]=q;continue;}
            int pos=posOfLST[b[q]-1];
            if (pos==0)
            {
                cout<<"-1\n";
                return 0;
            }
            if (bfMe[pos]!=0)
            {
                cout<<"-1\n";
                return 0;
            }
            bfMe[pos]=q;
            posOfLST[b[q]]=q;
        }
        int dok=1;
        for (int q=1;q<=n;q++)
        {
            if (b[q]==1)
            {
                ansk[q]=dok;
                //cout<<q<<" "<<dok<<"\n";
                dok++;
                int w=bfMe[q];
                while (w!=0)
                {
                    ansk[w]=dok;
                    //cout<<w<<" "<<dok<<"\n";
                    dok++;
                    w=bfMe[w];
                }
            }
        }
        int tm=n;
        for (int q=1;q<=n;q++)
        {
            if (ansk[q]==0)
            {
                ansk[q]=tm;
                tm--;
            }
        }
        if (finalcheck())
        {
            getSecond();
        }
        else
        {
            cout<<"-1\n";
        }
    }
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:274:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<perm>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  274 |         if (k<ans.size()) ans[k-1].show();
      |             ~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 316 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 363 ms 348 KB Output is correct
15 Correct 378 ms 444 KB Output is correct
16 Correct 387 ms 424 KB Output is correct
17 Correct 387 ms 340 KB Output is correct
18 Correct 36 ms 340 KB Output is correct
19 Correct 372 ms 300 KB Output is correct
20 Correct 410 ms 304 KB Output is correct
21 Correct 364 ms 312 KB Output is correct
22 Correct 384 ms 312 KB Output is correct
23 Correct 384 ms 304 KB Output is correct
24 Correct 378 ms 300 KB Output is correct
25 Correct 390 ms 316 KB Output is correct
26 Correct 396 ms 312 KB Output is correct
27 Correct 374 ms 304 KB Output is correct
28 Correct 389 ms 316 KB Output is correct
29 Correct 388 ms 316 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Incorrect 1 ms 212 KB Output isn't correct
32 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 316 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 363 ms 348 KB Output is correct
15 Correct 378 ms 444 KB Output is correct
16 Correct 387 ms 424 KB Output is correct
17 Correct 387 ms 340 KB Output is correct
18 Correct 36 ms 340 KB Output is correct
19 Correct 372 ms 300 KB Output is correct
20 Correct 410 ms 304 KB Output is correct
21 Correct 364 ms 312 KB Output is correct
22 Correct 384 ms 312 KB Output is correct
23 Correct 384 ms 304 KB Output is correct
24 Correct 378 ms 300 KB Output is correct
25 Correct 390 ms 316 KB Output is correct
26 Correct 396 ms 312 KB Output is correct
27 Correct 374 ms 304 KB Output is correct
28 Correct 389 ms 316 KB Output is correct
29 Correct 388 ms 316 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Incorrect 1 ms 212 KB Output isn't correct
32 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 11 ms 1620 KB Output is correct
2 Correct 10 ms 840 KB Output is correct
3 Correct 11 ms 1672 KB Output is correct
4 Correct 10 ms 844 KB Output is correct
5 Correct 11 ms 1604 KB Output is correct
6 Correct 11 ms 1620 KB Output is correct
7 Correct 11 ms 1620 KB Output is correct
8 Correct 13 ms 1676 KB Output is correct
9 Correct 12 ms 1604 KB Output is correct
10 Correct 11 ms 1620 KB Output is correct
11 Correct 22 ms 2956 KB Output is correct
12 Correct 22 ms 2972 KB Output is correct
13 Correct 22 ms 3024 KB Output is correct
14 Correct 22 ms 2988 KB Output is correct
15 Correct 23 ms 1196 KB Output is correct
16 Correct 19 ms 1208 KB Output is correct
17 Correct 19 ms 1172 KB Output is correct
18 Correct 23 ms 1244 KB Output is correct
19 Correct 20 ms 1124 KB Output is correct
20 Correct 19 ms 1204 KB Output is correct
21 Correct 23 ms 1224 KB Output is correct
22 Correct 19 ms 1128 KB Output is correct
23 Correct 20 ms 1116 KB Output is correct
24 Correct 28 ms 3104 KB Output is correct
25 Correct 32 ms 3036 KB Output is correct
26 Correct 28 ms 3012 KB Output is correct
27 Correct 28 ms 2960 KB Output is correct
28 Correct 28 ms 3012 KB Output is correct
29 Correct 29 ms 3020 KB Output is correct
30 Correct 7 ms 1108 KB Output is correct
31 Correct 12 ms 1652 KB Output is correct
32 Correct 22 ms 2736 KB Output is correct
33 Correct 24 ms 2968 KB Output is correct
34 Correct 4 ms 596 KB Output is correct
35 Correct 5 ms 872 KB Output is correct
36 Correct 13 ms 1732 KB Output is correct
37 Correct 23 ms 2892 KB Output is correct
38 Correct 22 ms 2908 KB Output is correct
39 Correct 25 ms 2916 KB Output is correct
40 Correct 22 ms 2464 KB Output is correct
41 Correct 33 ms 3400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 1620 KB Output is correct
2 Correct 10 ms 840 KB Output is correct
3 Correct 11 ms 1672 KB Output is correct
4 Correct 10 ms 844 KB Output is correct
5 Correct 11 ms 1604 KB Output is correct
6 Correct 11 ms 1620 KB Output is correct
7 Correct 11 ms 1620 KB Output is correct
8 Correct 13 ms 1676 KB Output is correct
9 Correct 12 ms 1604 KB Output is correct
10 Correct 11 ms 1620 KB Output is correct
11 Correct 22 ms 2956 KB Output is correct
12 Correct 22 ms 2972 KB Output is correct
13 Correct 22 ms 3024 KB Output is correct
14 Correct 22 ms 2988 KB Output is correct
15 Correct 23 ms 1196 KB Output is correct
16 Correct 19 ms 1208 KB Output is correct
17 Correct 19 ms 1172 KB Output is correct
18 Correct 23 ms 1244 KB Output is correct
19 Correct 20 ms 1124 KB Output is correct
20 Correct 19 ms 1204 KB Output is correct
21 Correct 23 ms 1224 KB Output is correct
22 Correct 19 ms 1128 KB Output is correct
23 Correct 20 ms 1116 KB Output is correct
24 Correct 28 ms 3104 KB Output is correct
25 Correct 32 ms 3036 KB Output is correct
26 Correct 28 ms 3012 KB Output is correct
27 Correct 28 ms 2960 KB Output is correct
28 Correct 28 ms 3012 KB Output is correct
29 Correct 29 ms 3020 KB Output is correct
30 Correct 7 ms 1108 KB Output is correct
31 Correct 12 ms 1652 KB Output is correct
32 Correct 22 ms 2736 KB Output is correct
33 Correct 24 ms 2968 KB Output is correct
34 Correct 4 ms 596 KB Output is correct
35 Correct 5 ms 872 KB Output is correct
36 Correct 13 ms 1732 KB Output is correct
37 Correct 23 ms 2892 KB Output is correct
38 Correct 22 ms 2908 KB Output is correct
39 Correct 25 ms 2916 KB Output is correct
40 Correct 22 ms 2464 KB Output is correct
41 Correct 33 ms 3400 KB Output is correct
42 Incorrect 11 ms 1588 KB Output isn't correct
43 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 316 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 363 ms 348 KB Output is correct
15 Correct 378 ms 444 KB Output is correct
16 Correct 387 ms 424 KB Output is correct
17 Correct 387 ms 340 KB Output is correct
18 Correct 36 ms 340 KB Output is correct
19 Correct 372 ms 300 KB Output is correct
20 Correct 410 ms 304 KB Output is correct
21 Correct 364 ms 312 KB Output is correct
22 Correct 384 ms 312 KB Output is correct
23 Correct 384 ms 304 KB Output is correct
24 Correct 378 ms 300 KB Output is correct
25 Correct 390 ms 316 KB Output is correct
26 Correct 396 ms 312 KB Output is correct
27 Correct 374 ms 304 KB Output is correct
28 Correct 389 ms 316 KB Output is correct
29 Correct 388 ms 316 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Incorrect 1 ms 212 KB Output isn't correct
32 Halted 0 ms 0 KB -