Submission #722700

# Submission time Handle Problem Language Result Execution time Memory
722700 2023-04-12T16:47:58 Z groshi Restore Array (RMI19_restore) C++17
100 / 100
13 ms 1312 KB
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
int s[6000];
int sumy[6000];
int t[11000][5];
bool ustawione[6000];
struct wi{
    vector<int> Q;
    int odl=1e9;
    bool odw=0;
}*w;
int32_t main()
{
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(0);
    int n,m;
    cin>>n>>m;
    int maxx=0;
    for(int i=1;i<=m;i++)
    cin>>t[i][0]>>t[i][1]>>t[i][2]>>t[i][3];
    for(int i=1;i<=m;i++)
    maxx=max(maxx,t[i][2]);
    bool brak=1;
    for(int i=1;i<=m;i++)
        if(t[i][2]!=1 && t[i][2]!=t[i][1]-t[i][0]+1)
            brak=0;
    if(n<=18 && m<=200)
    {
        for(int i=0;i<(1<<n);i++)
        {
            for(int j=0;j<n;j++)
            {
                int pom=i&(1<<j);
                if(pom>0)
                s[j]=1;
                else s[j]=0;
            }
            sumy[0]=s[0];
            for(int j=1;j<n;j++)
                sumy[j]=sumy[j-1]+s[j];
            bool git=0;
            for(int j=1;j<=m;j++)
            {
                int x=t[j][0];
                int y=t[j][1];
                int mam=sumy[y];
                if(x>0)
                    mam-=sumy[x-1];
                if(t[j][3]==0)
                {
                    if(mam<t[j][2])
                    {
                        git=1;
                        break;
                    }
                }
                else{
                    if(mam>t[j][2]-1)
                    {
                        git=1;
                        break;
                    }
                }
            }
            if(git==0)
            {
            for(int j=0;j<n;j++)
            cout<<!s[j]<<" ";
            cout<<"\n";
            return 0;
            }

        }
        cout<<"-1";
        return 0;
    }
    else if(maxx==1){
        for(int i=1;i<=m;i++)
            if(t[i][3]==1)
                for(int a=t[i][0];a<=t[i][1];a++)
                    s[a]=1;
        for(int i=1;i<=m;i++)
        {
            if(t[i][3]==0)
            {
                int suma=0;
                for(int a=t[i][0];a<=t[i][1];a++)
                    suma+=s[a];
                if(suma==t[i][1]-t[i][0]+1)
                {
                    cout<<"-1";
                    return 0;
                }
            }
        }
        for(int i=0;i<n;i++)
        cout<<s[i]<<" ";
    }
    else if(brak){
        bool git=0;
        for(int i=1;i<=m;i++)
        {
            if(t[i][3]==1 && t[i][2]==1)
                for(int a=t[i][0];a<=t[i][1];a++)
                {
                    if(ustawione[a]==1 && s[a]==0)
                    {
                        git=1;
                        break;
                    }
                    s[a]=1,ustawione[a]=1;
                }
            if(t[i][3]==0 && t[i][2]!=1)
                for(int a=t[i][0];a<=t[i][1];a++)
                {
                    if(s[a]==1)
                    {
                        git=1;
                        break;
                    }
                    ustawione[a]=1;
                }
            if(git)
            {
                cout<<"-1";
                return 0;
            }
        }
        for(int i=1;i<=m;i++)
        {
            bool git=0;
            if(t[i][3]==1 && t[i][2]==1)
                continue;
            if(t[i][3]==0 && t[i][2]!=1)
                continue;
            int chce=t[i][3];
            int puste=0;
            int gdzie=0;
            for(int a=t[i][0];a<=t[i][1];a++)
            {
                if(ustawione[a]==1 && s[a]==chce)
                {
                    git=1;
                    break;
                }
                if(ustawione[a]==0)
                {
                    puste++;
                    gdzie=a;
                }
            }
            if(git)
                continue;
            if(puste==0)
            {
                cout<<"-1";
                return 0;
            }
            if(puste==1)
            {
                ustawione[gdzie]=1;
                s[gdzie]=chce;
            }
        }
        int pop=0;
        for(int i=0;i<n;i++)
        {
            if(ustawione[i])
            continue;
            s[i]=pop;
            pop++;
            pop%=2;
        }
        for(int i=0;i<n;i++)
            cout<<s[i]<<" ";
        return 0;
    }
    else{
            int a,b,c,d;
        w=new wi[n+3];
    w[0].Q.push_back(1);
    w[0].Q.push_back(0);
    for(int i=1;i<n;i++)
    {
        w[i].Q.push_back(i+1);
        w[i].Q.push_back(1);
    }
    for(int i=n;i>=1;i--)
    {
        w[i].Q.push_back(i-1);
        w[i].Q.push_back(0);
    }
    for(int i=1;i<=m;i++)
    {
        a=t[i][0];
        b=t[i][1];
        c=t[i][2];
        d=t[i][3];
        a++;
        b++;
        if(d==1)
        {
            w[b].Q.push_back(a-1);
            w[b].Q.push_back(-(b-a+1)+c-1);
        }
        else{
            w[a-1].Q.push_back(b);
            w[a-1].Q.push_back(b-a+1-c);
        }
    }
    priority_queue<pair<int,int> > kolejka;
    kolejka.push({0,0});
    w[0].odl=0;
    bool k=0;
    while(!kolejka.empty())
    {
        auto para=kolejka.top();
        kolejka.pop();
        w[para.second].odw=1;
        for(int i=0;i<w[para.second].Q.size();i+=2)
        {
            if(w[para.second].odl+w[para.second].Q[i+1]<w[w[para.second].Q[i]].odl)
            {
                w[w[para.second].Q[i]].odl=w[para.second].odl+w[para.second].Q[i+1];
                auto parap=make_pair(w[w[para.second].Q[i]].odl*(-1),w[para.second].Q[i]);
                kolejka.push(parap);
            }
            if(w[w[para.second].Q[i]].odw && w[w[para.second].Q[i]].odl<0)
            {
                k=1;
                break;
            }
        }
        if(k==1)
            break;
    }
    if(k==1)
        cout<<"-1";
    else{
        for(int i=1;i<=n;i++)
        {
            cout<<w[i].odl-w[i-1].odl<<" ";
        }
    }
    return 0;
    }
    return 0;
}

Compilation message

restore.cpp: In function 'int32_t main()':
restore.cpp:223:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  223 |         for(int i=0;i<w[para.second].Q.size();i+=2)
      |                     ~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 324 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 7 ms 340 KB Output is correct
4 Correct 3 ms 332 KB Output is correct
5 Correct 8 ms 340 KB Output is correct
6 Correct 6 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 596 KB Output is correct
2 Correct 12 ms 596 KB Output is correct
3 Correct 10 ms 612 KB Output is correct
4 Correct 10 ms 596 KB Output is correct
5 Correct 5 ms 584 KB Output is correct
6 Correct 5 ms 584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 596 KB Output is correct
2 Correct 12 ms 596 KB Output is correct
3 Correct 10 ms 612 KB Output is correct
4 Correct 10 ms 596 KB Output is correct
5 Correct 5 ms 584 KB Output is correct
6 Correct 5 ms 584 KB Output is correct
7 Correct 13 ms 616 KB Output is correct
8 Correct 10 ms 588 KB Output is correct
9 Correct 9 ms 584 KB Output is correct
10 Correct 10 ms 596 KB Output is correct
11 Correct 3 ms 592 KB Output is correct
12 Correct 4 ms 596 KB Output is correct
13 Correct 10 ms 596 KB Output is correct
14 Correct 4 ms 616 KB Output is correct
15 Correct 4 ms 596 KB Output is correct
16 Correct 4 ms 612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 324 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 7 ms 340 KB Output is correct
4 Correct 3 ms 332 KB Output is correct
5 Correct 8 ms 340 KB Output is correct
6 Correct 6 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 10 ms 596 KB Output is correct
12 Correct 12 ms 596 KB Output is correct
13 Correct 10 ms 612 KB Output is correct
14 Correct 10 ms 596 KB Output is correct
15 Correct 5 ms 584 KB Output is correct
16 Correct 5 ms 584 KB Output is correct
17 Correct 13 ms 616 KB Output is correct
18 Correct 10 ms 588 KB Output is correct
19 Correct 9 ms 584 KB Output is correct
20 Correct 10 ms 596 KB Output is correct
21 Correct 3 ms 592 KB Output is correct
22 Correct 4 ms 596 KB Output is correct
23 Correct 10 ms 596 KB Output is correct
24 Correct 4 ms 616 KB Output is correct
25 Correct 4 ms 596 KB Output is correct
26 Correct 4 ms 612 KB Output is correct
27 Correct 6 ms 1236 KB Output is correct
28 Correct 7 ms 1220 KB Output is correct
29 Correct 9 ms 1236 KB Output is correct
30 Correct 7 ms 1236 KB Output is correct
31 Correct 7 ms 1236 KB Output is correct
32 Correct 6 ms 1224 KB Output is correct
33 Correct 5 ms 1096 KB Output is correct
34 Correct 4 ms 1108 KB Output is correct
35 Correct 8 ms 1312 KB Output is correct
36 Correct 7 ms 1216 KB Output is correct