QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#121600#6680. HeapNicolas125841WA 97ms5172kbC++173.2kb2023-07-08 15:19:242023-07-08 15:19:25

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-08 15:19:25]
  • 评测
  • 测评结果:WA
  • 用时:97ms
  • 内存:5172kb
  • [2023-07-08 15:19:24]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

int main(){
    cin.tie(NULL)->sync_with_stdio(false);

    int t;
    cin >> t;

    while(t--){
        int n;
        cin >> n;

        vector<int> v(n), a(n), oa(n);

        for(int i = 0; i < n; i++)
            cin >> v[i];

        for(int i = 0; i < n; i++){
            cin >> a[i];

            oa[i] = a[i];
        }

        string bs;
        vector<int> inds;
        bool good = true, found = false;

        n--;

        while(n > 0){
            int ind = n;
            char type;
            
            if(a[ind] == v[n]){
                assert(a.back() == v[n]);

                if(a[(ind-1)/2] <= v[n])
                    bs.push_back('0');
                else
                    bs.push_back('1');     

                a.pop_back();
                n--;

                continue;
            }else if(a[ind] < v[n])
                type = '1';
            else
                type = '0';
            
            found = false;

            inds.clear();
            inds.push_back(ind);

            do{
                ind = (ind-1)/2;

                inds.push_back(ind);

                if(a[ind] == v[n]){
                    found = true;

                    if(ind > 0){
                        if(a[(ind-1)/2] < v[n])
                            if(type == '1')
                                good = false;
                        else if(a[(ind-1)/2] > v[n])
                            if(type == '0')
                                good = false;                        
                    }

                    break;
                }else if(a[ind] < v[n]){
                    if(type == '0'){
                        good = false;

                        break;
                    }
                }else{
                    if(type == '1'){
                        good = false;

                        break;
                    }
                }
            }while(ind > 0);

            if(!good || !found){
                good = false;

                break;
            }

            for(int i = inds.size()-2; i >= 0; i--)
                swap(a[inds[i]], a[inds[i+1]]);

            assert(a.back() == v[n]);

            bs.push_back(type);
            a.pop_back();
            n--;
        }

        if(a.back() != v[0])
            good = false;
        
        bs.push_back('0');
        a.pop_back();
        n--;

        reverse(bs.begin(), bs.end());

        for(int i = 0; i < n; i++){
            a.push_back(v[i]);

            int ind = i;

            while(ind > 0){
                if(bs[i] == '0' && a[(ind-1)/2] <= v[i])
                    break;
                else if(bs[i] == '1' && a[(ind-1)/2] >= v[i])
                    break;
                
                swap(a[ind], a[(ind-1)/2]);

                ind = (ind-1)/2;
            }
        }

        for(int i = 0; i < n; i++)
            if(a[i] != oa[i])
                good = false;

        if(!good)
            cout << "Impossible\n";
        else
            cout << bs << "\n";        
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3488kb

input:

3
4
2 3 1 4
4 1 3 2
5
4 5 1 2 3
3 4 1 5 2
3
1 1 2
2 1 1

output:

0101
Impossible
001

result:

ok 3 lines

Test #2:

score: -100
Wrong Answer
time: 97ms
memory: 5172kb

input:

1118
77
210555 375330 669075 785279 194474 310626 830710 886719 102133 426324 41900 971872 167203 702222 230534 112350 673156 358623 886831 72864 818751 367894 842740 531878 818794 131219 412128 104469 70427 658494 72060 768735 915294 161365 91671 451974 121863 498890 408674 670274 875754 967228 518...

output:

00010111111010100000101000101001111010010000100101001001001000001100111100010
000001011011010
01011011111111010011000100101100101010000110111000001100
0
Impossible
01
011001000010010011110011010100110010111110101000010001101101000011
Impossible
Impossible
010001111
0010000101001000000100110111100111...

result:

wrong answer 21st lines differ - expected: 'Impossible', found: '000110000'