Submission #340444

# Submission time Handle Problem Language Result Execution time Memory
340444 2020-12-27T15:53:10 Z A_D Slagalica (COCI19_slagalica2) C++14
20 / 70
1000 ms 14952 KB
/*
ID: antwand1
TASK: barn1
LANG: C++
*/
#include <bits/stdc++.h>
#define ll long long
#define int long long
#define du long double
#define F first
#define S second
using namespace std;
deque<int> v[10];
vector<int> vec;
int n;
bool bc(int i,int u)
{
    if(i==n){
        if(u%2)u=8;
        else u=7;
        if(v[u].size()){
            vec.push_back(v[u][0]);
            return 1;
        }
        else return 0;
    }
    bool ret=0;
    int r=1;
    if(u%2)r=3;
    if(v[r].empty()&&v[r+1].empty())return 0;
    if(v[r].empty()){
        int q=v[r+1].front();
        v[r+1].pop_front();
        ret=bc(i+1,0);
        v[r+1].push_front(q);
        if(ret)vec.push_back(q);
        return ret;
    }
    else if(v[r+1].empty()){
        int q=v[r].front();
        v[r].pop_front();
        ret=bc(i+1,1);
        v[r].push_front(q);
        if(ret)vec.push_back(q);
        return ret;
    }
    else{
        if(v[r][0]<v[r+1][0]){
            int q=v[r].front();
            v[r].pop_front();
            ret=bc(i+1,1);
            v[r].push_front(q);
            if(ret)vec.push_back(q);
            if(ret)return ret;
            q=v[r+1].front();
            v[r+1].pop_front();
            ret=bc(i+1,0);
            v[r+1].push_front(q);
            if(ret)vec.push_back(q);
            return ret;
        }
        else{
            int q=v[r+1].front();
            v[r+1].pop_front();
            ret=bc(i+1,0);
            v[r+1].push_front(q);
            if(ret)vec.push_back(q);
            if(ret)return ret;
            q=v[r].front();
            v[r].pop_front();
            ret=bc(i+1,1);
            v[r].push_front(q);
            if(ret)vec.push_back(q);
            return ret;
        }
    }
}
main()
{
    //freopen("barn1.in","r",stdin);freopen("barn1.out","w",stdout);
    cin>>n;
    for(int i=1;i<=n;i++){
        int x,a;
        cin>>x>>a;
        v[x].push_back(a);
    }
    for(int i=1;i<=8;i++)sort(v[i].begin(),v[i].end());
    if(v[5].empty()==0){
        if(bc(2,1)){
            vec.push_back(v[5][0]);
            reverse(vec.begin(),vec.end());
            for(int i=0;i<n;i++){
                cout<<vec[i]<<" ";
            }
            cout<<endl;
        }
        else cout<<-1<<endl;
    }
    else if(v[6].empty()==0){
        if(bc(2,0)){
            vec.push_back(v[6][0]);
            reverse(vec.begin(),vec.end());
            for(int i=0;i<n;i++){
                cout<<vec[i]<<" ";
            }
            cout<<endl;
        }
        else cout<<-1<<endl;
    }
    else cout<<-1<<endl;
}



/*
bool bc(int i,int a)
{
    if(i==n){
        if(a==0){
            if(v[7].empty()==0){
                s+=v[7].front()+'0';
                return 1;
            }
            else return 0;
        }
        else{
            if(v[8].empty()==0){
                s+=v[8].front()+'0';
                return 1;
            }
            else return 0;
        }
    }
    bool ret=0;
    if(a==0){
        if(v[1].empty()&&v[2].empty())return 0;
        if(v[1].empty()){
            int q=v[2][0];
            v[2].pop_front();
            ret=bc(i+1,0);
            v[2].push_front(q);
            if(ret)s+=q+'0';
            return ret;
        }
        else{
            if(v[2].empty()){
                int q=v[1][0];
                v[1].pop_front();
                ret=bc(i+1,1);
                v[1].push_front(q);
                if(ret)s+=q+'0';
                return ret;
            }
            else{
                if(v[1][0]<v[2][0]){
                    int q=v[1][0];
                    v[1].pop_front();
                    ret=bc(i+1,1);
                    v[1].push_front(q);
                    if(ret)s+=q+'0';
                    if(ret)return ret;
                    q=v[2][0];
                    v[2].pop_front();
                    ret=bc(i+1,0);
                    v[2].push_front(q);
                    if(ret)s+=q+'0';
                    return ret;
                }
                else{
                    int q=v[2][0];
                    v[2].pop_front();
                    ret=bc(i+1,0);
                    v[2].push_front(q);
                    if(ret)s+=q+'0';
                    if(ret)return ret;
                    q=v[1][0];
                    v[1].pop_front();
                    ret=bc(i+1,1);
                    v[1].push_front(q);
                    if(ret)s+=q+'0';
                    return ret;
                }
            }
        }
    }
    else{
        if(v[3].empty()&&v[4].empty())return 0;
        if(v[3].empty()){
            int q=v[4][0];
            v[4].pop_front();
            ret=bc(i+1,0);
            v[4].push_front(q);
            if(ret)s+=q+'0';
            return ret;
        }
        else{
            if(v[3].empty()){
                int q=v[3][0];
                v[3].pop_front();
                ret=bc(i+1,1);
                v[3].push_front(q);
                if(ret)s+=q+'0';
                return ret;
            }
            else{
                if(v[3][0]<v[4][0]){
                    int q=v[1][0];
                    v[3].pop_front();
                    ret=bc(i+1,1);
                    v[3].push_front(q);
                    if(ret)s+=q+'0';
                    if(ret)return ret;
                    q=v[4][0];
                    v[4].pop_front();
                    ret=bc(i+1,0);
                    v[4].push_front(q);
                    if(ret)s+=q+'0';
                    return ret;
                }
                else{
                    int q=v[4][0];
                    v[4].pop_front();
                    ret=bc(i+1,0);
                    v[4].push_front(q);
                    if(ret)s+=q+'0';
                    if(ret)return ret;
                    q=v[3][0];
                    v[3].pop_front();
                    ret=bc(i+1,1);
                    v[3].push_front(q);
                    if(ret)s+=q+'0';
                    return ret;
                }
            }
        }
    }
}
*/

Compilation message

slagalica.cpp:78:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   78 | main()
      |      ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 82 ms 13180 KB Output is correct
2 Correct 73 ms 12416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 78 ms 12648 KB Output is correct
2 Correct 69 ms 11628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 72 ms 7788 KB Output is correct
2 Execution timed out 1094 ms 11388 KB Time limit exceeded
# Verdict Execution time Memory Grader output
1 Correct 61 ms 6764 KB Output is correct
2 Correct 86 ms 12392 KB Output is correct
3 Execution timed out 1087 ms 10372 KB Time limit exceeded
# Verdict Execution time Memory Grader output
1 Execution timed out 1057 ms 7700 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 78 ms 12776 KB Output is correct
2 Correct 65 ms 7148 KB Output is correct
3 Execution timed out 1098 ms 10092 KB Time limit exceeded
# Verdict Execution time Memory Grader output
1 Execution timed out 1098 ms 11116 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 93 ms 14952 KB Output is correct
2 Execution timed out 1096 ms 11372 KB Time limit exceeded
# Verdict Execution time Memory Grader output
1 Correct 76 ms 12520 KB Output is correct
2 Execution timed out 1100 ms 11628 KB Time limit exceeded
# Verdict Execution time Memory Grader output
1 Correct 90 ms 14696 KB Output is correct
2 Execution timed out 1097 ms 12572 KB Time limit exceeded
# Verdict Execution time Memory Grader output
1 Correct 75 ms 12428 KB Output is correct
2 Execution timed out 1082 ms 11628 KB Time limit exceeded
# Verdict Execution time Memory Grader output
1 Correct 78 ms 12652 KB Output is correct
2 Execution timed out 1096 ms 12268 KB Time limit exceeded