Submission #821739

# Submission time Handle Problem Language Result Execution time Memory
821739 2023-08-11T14:32:34 Z PVM_pvm Watermelon (INOI20_watermelon) C++17
56 / 100
631 ms 3200 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()
{
    stack<pair<int,int>> st;
    sb[n]=-1;
    //if (chb[n]!=b[n]) return false;
    st.push({cur[n],n});
    for (int q=n-1;q>=1;q--)
    {
        sb[q]=-2;
        while (true)
        {
            if (st.empty())
            {
                sb[q]=-1;
                st.push({cur[q],q});
                break;
            }
            int chislo = st.top().first;
            int pos = st.top().second;
            if (cur[q]>chislo)
            {
                sb[q]=max(sb[q],sb[pos]);
                st.pop();
                continue;
            }
            if (sb[q]==-2) sb[q]=1;
            else sb[q]++;
            st.push({cur[q],q});
            break;
        }
        //cout<<q<<":"<<chb[q]<<" "<<b[q]<<"\n";
        //if (chb[q]!=b[q]2) return false;
    }
    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 ima=false;
    bool ima1=false;
    bool minahminus=false;
    int ngc=0,kdc=0;
    for (int q=f1-1;q>=1;q--)
    {
        if (b[q]>1 && !minahminus) ima=true;
        if (b[q]==1 && !minahminus) ima1=true;
        if (b[q]==-1) minahminus=true;
        if (b[q]!=-1 && minahminus)
        {
            if (ansk[q]>ngc)
            {
                ngc=ansk[q];
                kdc=q;
            }
        }
    }
    if (bfMe[f1]!=0)
    {
        if (ima1)
        {
            int ng=0,kd=0;
            for (int q=f1-1;q>=1;q--)
            {
                if (b[q]==-1) break;
                if (ansk[q]>ng && ansk[q]<ansk[f1])
                {
                    ng=ansk[q];
                    kd=q;
                }
            }
            swap(ansk[f1],ansk[kd]);
            printthem();
            return;
        }
        else
        {
            if (kdc!=0)
            {
                swap(ansk[f1],ansk[kdc]);
                printthem();
                return;
            }
            else
            {
                cout<<"-1\n";
                return;
            }
        }
    }
    int f2=0;
    for (int q=f1;q>=1;q--)
    {
        //cout<<q<<" "<<bfMe[q]<<"\n";
        if (bfMe[q]!=0 || b[q]==-1) break;
        f2=q;
    }
    if (f2==1 || b[f2-1]==-1)
    {
        if (kdc!=0)
        {
            swap(ansk[f2],ansk[kdc]);
            printthem();
            return;
        }
        else
        {
            cout<<"-1\n";
            return;
        }
    }
    int ng=0,kd=0;
    for (int q=f1-1;q>=1;q--)
    {
        if (b[q]==-1) break;
        if (ansk[q]>ng && ansk[q]<ansk[f2])
        {
            ng=ansk[q];
            kd=q;
        }
    }
    swap(ansk[f2],ansk[kd]);
    printthem();
}
int main()
{
    cin>>n>>k;
    if (n<=10)
    {
        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 'void getSecond()':
Main.cpp:199:10: warning: variable 'ima' set but not used [-Wunused-but-set-variable]
  199 |     bool ima=false;
      |          ^~~
Main.cpp: In function 'int main()':
Main.cpp:294:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<perm>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  294 |         if (k<ans.size()) ans[k-1].show();
      |             ~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 324 KB Output is correct
2 Correct 55 ms 336 KB Output is correct
3 Correct 614 ms 812 KB Output is correct
4 Correct 603 ms 296 KB Output is correct
5 Correct 609 ms 296 KB Output is correct
6 Correct 55 ms 212 KB Output is correct
7 Correct 608 ms 300 KB Output is correct
8 Correct 609 ms 296 KB Output is correct
9 Correct 56 ms 292 KB Output is correct
10 Correct 60 ms 300 KB Output is correct
11 Correct 6 ms 212 KB Output is correct
12 Correct 601 ms 488 KB Output is correct
13 Correct 604 ms 580 KB Output is correct
14 Correct 604 ms 304 KB Output is correct
15 Correct 604 ms 432 KB Output is correct
16 Correct 607 ms 388 KB Output is correct
17 Correct 602 ms 400 KB Output is correct
18 Correct 62 ms 348 KB Output is correct
19 Correct 603 ms 292 KB Output is correct
20 Correct 611 ms 292 KB Output is correct
21 Correct 607 ms 292 KB Output is correct
22 Correct 601 ms 304 KB Output is correct
23 Correct 606 ms 296 KB Output is correct
24 Correct 609 ms 300 KB Output is correct
25 Correct 608 ms 292 KB Output is correct
26 Correct 631 ms 292 KB Output is correct
27 Correct 608 ms 292 KB Output is correct
28 Correct 601 ms 288 KB Output is correct
29 Correct 606 ms 432 KB Output is correct
30 Correct 6 ms 212 KB Output is correct
31 Correct 6 ms 212 KB Output is correct
32 Correct 6 ms 212 KB Output is correct
33 Correct 6 ms 328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 324 KB Output is correct
2 Correct 55 ms 336 KB Output is correct
3 Correct 614 ms 812 KB Output is correct
4 Correct 603 ms 296 KB Output is correct
5 Correct 609 ms 296 KB Output is correct
6 Correct 55 ms 212 KB Output is correct
7 Correct 608 ms 300 KB Output is correct
8 Correct 609 ms 296 KB Output is correct
9 Correct 56 ms 292 KB Output is correct
10 Correct 60 ms 300 KB Output is correct
11 Correct 6 ms 212 KB Output is correct
12 Correct 601 ms 488 KB Output is correct
13 Correct 604 ms 580 KB Output is correct
14 Correct 604 ms 304 KB Output is correct
15 Correct 604 ms 432 KB Output is correct
16 Correct 607 ms 388 KB Output is correct
17 Correct 602 ms 400 KB Output is correct
18 Correct 62 ms 348 KB Output is correct
19 Correct 603 ms 292 KB Output is correct
20 Correct 611 ms 292 KB Output is correct
21 Correct 607 ms 292 KB Output is correct
22 Correct 601 ms 304 KB Output is correct
23 Correct 606 ms 296 KB Output is correct
24 Correct 609 ms 300 KB Output is correct
25 Correct 608 ms 292 KB Output is correct
26 Correct 631 ms 292 KB Output is correct
27 Correct 608 ms 292 KB Output is correct
28 Correct 601 ms 288 KB Output is correct
29 Correct 606 ms 432 KB Output is correct
30 Correct 6 ms 212 KB Output is correct
31 Correct 6 ms 212 KB Output is correct
32 Correct 6 ms 212 KB Output is correct
33 Correct 6 ms 328 KB Output is correct
34 Incorrect 1 ms 340 KB Output isn't correct
35 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 11 ms 1492 KB Output is correct
2 Correct 9 ms 724 KB Output is correct
3 Correct 11 ms 1564 KB Output is correct
4 Correct 9 ms 724 KB Output is correct
5 Correct 11 ms 1488 KB Output is correct
6 Correct 11 ms 1492 KB Output is correct
7 Correct 11 ms 1584 KB Output is correct
8 Correct 11 ms 1508 KB Output is correct
9 Correct 11 ms 1492 KB Output is correct
10 Correct 11 ms 1496 KB Output is correct
11 Correct 21 ms 2740 KB Output is correct
12 Correct 21 ms 2772 KB Output is correct
13 Correct 27 ms 2740 KB Output is correct
14 Correct 26 ms 2820 KB Output is correct
15 Correct 19 ms 980 KB Output is correct
16 Correct 19 ms 1076 KB Output is correct
17 Correct 19 ms 980 KB Output is correct
18 Correct 19 ms 1008 KB Output is correct
19 Correct 19 ms 1072 KB Output is correct
20 Correct 18 ms 992 KB Output is correct
21 Correct 19 ms 1004 KB Output is correct
22 Correct 19 ms 980 KB Output is correct
23 Correct 19 ms 1056 KB Output is correct
24 Correct 28 ms 2900 KB Output is correct
25 Correct 27 ms 2900 KB Output is correct
26 Correct 31 ms 2892 KB Output is correct
27 Correct 27 ms 2840 KB Output is correct
28 Correct 31 ms 2952 KB Output is correct
29 Correct 27 ms 2892 KB Output is correct
30 Correct 7 ms 980 KB Output is correct
31 Correct 12 ms 1604 KB Output is correct
32 Correct 21 ms 2516 KB Output is correct
33 Correct 24 ms 2832 KB Output is correct
34 Correct 3 ms 468 KB Output is correct
35 Correct 5 ms 724 KB Output is correct
36 Correct 13 ms 1492 KB Output is correct
37 Correct 27 ms 2836 KB Output is correct
38 Correct 27 ms 2764 KB Output is correct
39 Correct 25 ms 2868 KB Output is correct
40 Correct 22 ms 2436 KB Output is correct
41 Correct 29 ms 3200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 1492 KB Output is correct
2 Correct 9 ms 724 KB Output is correct
3 Correct 11 ms 1564 KB Output is correct
4 Correct 9 ms 724 KB Output is correct
5 Correct 11 ms 1488 KB Output is correct
6 Correct 11 ms 1492 KB Output is correct
7 Correct 11 ms 1584 KB Output is correct
8 Correct 11 ms 1508 KB Output is correct
9 Correct 11 ms 1492 KB Output is correct
10 Correct 11 ms 1496 KB Output is correct
11 Correct 21 ms 2740 KB Output is correct
12 Correct 21 ms 2772 KB Output is correct
13 Correct 27 ms 2740 KB Output is correct
14 Correct 26 ms 2820 KB Output is correct
15 Correct 19 ms 980 KB Output is correct
16 Correct 19 ms 1076 KB Output is correct
17 Correct 19 ms 980 KB Output is correct
18 Correct 19 ms 1008 KB Output is correct
19 Correct 19 ms 1072 KB Output is correct
20 Correct 18 ms 992 KB Output is correct
21 Correct 19 ms 1004 KB Output is correct
22 Correct 19 ms 980 KB Output is correct
23 Correct 19 ms 1056 KB Output is correct
24 Correct 28 ms 2900 KB Output is correct
25 Correct 27 ms 2900 KB Output is correct
26 Correct 31 ms 2892 KB Output is correct
27 Correct 27 ms 2840 KB Output is correct
28 Correct 31 ms 2952 KB Output is correct
29 Correct 27 ms 2892 KB Output is correct
30 Correct 7 ms 980 KB Output is correct
31 Correct 12 ms 1604 KB Output is correct
32 Correct 21 ms 2516 KB Output is correct
33 Correct 24 ms 2832 KB Output is correct
34 Correct 3 ms 468 KB Output is correct
35 Correct 5 ms 724 KB Output is correct
36 Correct 13 ms 1492 KB Output is correct
37 Correct 27 ms 2836 KB Output is correct
38 Correct 27 ms 2764 KB Output is correct
39 Correct 25 ms 2868 KB Output is correct
40 Correct 22 ms 2436 KB Output is correct
41 Correct 29 ms 3200 KB Output is correct
42 Correct 11 ms 1512 KB Output is correct
43 Correct 10 ms 716 KB Output is correct
44 Correct 11 ms 1492 KB Output is correct
45 Correct 10 ms 596 KB Output is correct
46 Correct 9 ms 724 KB Output is correct
47 Correct 9 ms 620 KB Output is correct
48 Correct 13 ms 596 KB Output is correct
49 Correct 11 ms 1504 KB Output is correct
50 Correct 11 ms 1504 KB Output is correct
51 Correct 11 ms 1508 KB Output is correct
52 Correct 22 ms 2760 KB Output is correct
53 Correct 21 ms 2772 KB Output is correct
54 Correct 18 ms 980 KB Output is correct
55 Correct 18 ms 1076 KB Output is correct
56 Correct 21 ms 2516 KB Output is correct
57 Correct 23 ms 2832 KB Output is correct
58 Correct 23 ms 2824 KB Output is correct
59 Correct 22 ms 2772 KB Output is correct
60 Correct 25 ms 2772 KB Output is correct
61 Correct 22 ms 2808 KB Output is correct
62 Correct 22 ms 2556 KB Output is correct
63 Correct 28 ms 2904 KB Output is correct
64 Correct 28 ms 2976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 324 KB Output is correct
2 Correct 55 ms 336 KB Output is correct
3 Correct 614 ms 812 KB Output is correct
4 Correct 603 ms 296 KB Output is correct
5 Correct 609 ms 296 KB Output is correct
6 Correct 55 ms 212 KB Output is correct
7 Correct 608 ms 300 KB Output is correct
8 Correct 609 ms 296 KB Output is correct
9 Correct 56 ms 292 KB Output is correct
10 Correct 60 ms 300 KB Output is correct
11 Correct 6 ms 212 KB Output is correct
12 Correct 601 ms 488 KB Output is correct
13 Correct 604 ms 580 KB Output is correct
14 Correct 604 ms 304 KB Output is correct
15 Correct 604 ms 432 KB Output is correct
16 Correct 607 ms 388 KB Output is correct
17 Correct 602 ms 400 KB Output is correct
18 Correct 62 ms 348 KB Output is correct
19 Correct 603 ms 292 KB Output is correct
20 Correct 611 ms 292 KB Output is correct
21 Correct 607 ms 292 KB Output is correct
22 Correct 601 ms 304 KB Output is correct
23 Correct 606 ms 296 KB Output is correct
24 Correct 609 ms 300 KB Output is correct
25 Correct 608 ms 292 KB Output is correct
26 Correct 631 ms 292 KB Output is correct
27 Correct 608 ms 292 KB Output is correct
28 Correct 601 ms 288 KB Output is correct
29 Correct 606 ms 432 KB Output is correct
30 Correct 6 ms 212 KB Output is correct
31 Correct 6 ms 212 KB Output is correct
32 Correct 6 ms 212 KB Output is correct
33 Correct 6 ms 328 KB Output is correct
34 Incorrect 1 ms 340 KB Output isn't correct
35 Halted 0 ms 0 KB -