QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#497032#7942. $K$ SubsequencesdeepthoughtWA 28ms3760kbC++233.2kb2024-07-28 18:19:502024-07-28 18:19:51

Judging History

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

  • [2024-07-28 18:19:51]
  • 评测
  • 测评结果:WA
  • 用时:28ms
  • 内存:3760kb
  • [2024-07-28 18:19:50]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

int32_t main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int tc;
    cin >> tc;
    for(int tt = 1; tt <= tc; tt++) {
        int n, k;
        cin >> n >> k;
        vector <int> a(n);
        bool pl = true;
        int cnt = 0;
        for(int i = 0; i < n; i++){ 
            cin >> a[i];
            if(a[i] == 1) pl = false;
            if(a[i] == 1) cnt++;
        }
        if(pl == true) {
            for(int i = 0; i < n; i++) cout << 1 << " ";
            cout << '\n';
            continue;
        }
        if(tt == 14722) assert(k <= 3);
        
        
        // 7 tane 1
        // 3 tane 0

        


        // 000011111
        // lo last 0, high first 1
        int lo = 0;
        int hi = n;
        while((hi - lo) > 1) {
            int mid = lo + (hi - lo) / 2;
            stack <pair <int, int>> pos;
            stack <pair<int, int>> neg;
            for(int i = k; i >= 1; i--) {
                pos.push({i, 0});
            }
            bool ok = true;
            for(int i = 0; i < n; i++) {
                if(a[i] == 1) {
                    if(pos.empty()) {ok = false; break;}
                    auto [x, y] = pos.top();
                    pos.pop();
                    if(y + 1 == mid) neg.push({x, y + 1});
                    else pos.push({x, y + 1});
                }
                else if(a[i] == - 1) {
                    if(neg.empty()) {
                        if(pos.empty()) {ok = false; break;}
                        auto [x, y] = pos.top();
                        pos.pop();
                        pos.push({x, max(y - 1, 0)});                        
                    }
                    else {
                        auto [x, y] = neg.top();
                        neg.pop();
                        assert(y > 0);
                        assert(y == mid);
                        pos.push({x, y - 1});
                    }
                }
            }
            if(ok) {
                hi = mid;
            }
            else {
                lo = mid;
            }
        }
        stack <pair <int, int>> pos;
        stack <pair<int, int>> neg;
        for(int i = k; i >= 1; i--) {
            pos.push({i, 0});
        }
        int mid = hi;
        for(int i = 0; i < n; i++) {
            if(a[i] == 1) {
                auto [x, y] = pos.top();
                cout << x << " ";
                pos.pop();
                if(y + 1 == mid) neg.push({x, y + 1});
                else pos.push({x, y + 1});
            }
            else if(a[i] == - 1) {
                if(neg.empty()) {
                    auto [x, y] = pos.top();
                    cout << x << " ";
                    pos.pop();
                    pos.push({x, max(y - 1, 0)});                        
                }
                else {
                    auto [x, y] = neg.top();
                    cout << x << " ";
                    neg.pop();
                    assert(y > 0);
                    pos.push({x, y - 1});
                }
            }
        }
        cout << '\n';
    }
    





}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3620kb

input:

5
3 2
1 -1 1
4 2
-1 1 1 -1
7 3
1 1 1 1 1 1 1
10 3
1 1 1 1 -1 -1 1 1 1 1
12 4
1 1 1 1 -1 -1 -1 -1 1 1 1 1

output:

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

result:

ok Correct (5 test cases)

Test #2:

score: -100
Wrong Answer
time: 28ms
memory: 3760kb

input:

18434
10 1
-1 1 1 -1 -1 1 -1 -1 1 1
10 2
-1 -1 -1 1 1 -1 1 1 1 1
10 2
1 -1 -1 -1 -1 1 1 -1 1 1
10 7
1 1 -1 1 -1 1 1 -1 -1 1
9 1
-1 1 -1 1 1 -1 1 -1 1
8 1
-1 -1 -1 -1 1 1 -1 -1
10 3
-1 -1 -1 1 1 1 1 -1 -1 -1
9 1
1 -1 -1 1 -1 -1 -1 -1 -1
10 10
-1 1 1 1 1 1 1 1 1 1
10 4
-1 1 -1 1 -1 1 1 -1 1 1
9 3
1 1 ...

output:

1 1 1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 2 2 
1 1 1 1 1 1 1 1 1 2 
1 2 2 2 2 2 3 3 2 2 
1 1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 
1 1 1 1 1 2 2 2 1 1 
1 1 1 1 1 1 1 1 1 
1 1 2 3 4 5 6 7 8 9 
1 1 1 1 1 1 2 2 2 3 
1 2 2 1 1 1 1 1 1 
1 2 2 2 3 4 4 4 
1 1 2 3 4 5 6 7 7 7 
1 1 1 1 1 1 1 2 2 
1 1 1 1 1 1 1 1 1 
1 1...

result:

wrong answer Jury found better answer than participant (test case 14722)