QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#263714#6454. Unbalanced ParenthesesZhouShangWA 9ms8336kbC++143.2kb2023-11-25 04:32:532023-11-25 04:32:53

Judging History

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

  • [2023-11-25 04:32:53]
  • 评测
  • 测评结果:WA
  • 用时:9ms
  • 内存:8336kb
  • [2023-11-25 04:32:53]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
#define rep(i,a,b) for(int i = a; i < (b); i++)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define PB push_back
#define FS first
#define SD second
#define cmx(x, y) x = max(x, y)
#define cmn(x, y) x = min(x, y)
#define ary(k) array<int, k>
typedef pair<int, int> pii;
typedef vector<int> vi;

int cost[1000002];
int change[1000002];

signed main() {
    cin.tie(0)->sync_with_stdio(0); cin.exceptions(cin.failbit);
    int n, k;
    cin >> n >> k;
    string slo;
    cin >> slo;
    int pocz_min = 0;
    priority_queue<pii> pq;
    set<pii> used;
    for (int i = 0; i < n; i++) {
        cin >> cost[i];
        if (cost[i] < 0) {
            pocz_min -= cost[i];
            cost[i] *= -1;
            if (slo[i] == ')')
                slo[i] = '(';
            else
                slo[i] = ')';
        }
        if (slo[i] == ')')
            pq.push({-cost[i], i});
    }
    if (n%2) {
        cout << -pocz_min << "\n";
        return 0;
    }
    if (2*k >= n) {
        cout << "?\n";
        return 0;
    }
    int k1 = 0, k2 = 0;
    for (int i = 0; i < n; i++) {
        if (slo[i] == ')') {
            k2--;
        } else {
            k2++;
        }
        cmx(k1, -k2);
    }
    int best = 1e18;
    int akt = 0, curst = 0, curcost = 0;
    while (!pq.empty() && curst + k2/2 <= k) {
        auto [c, id] = pq.top();
        pq.pop();
        used.insert({-c, id});
        curst++;
        curcost += -c;
        change[id] = 1;
    }
//    cerr << best << "\n";
    if (curst + k2/2 > k)
        cmn(best, curcost);
    for (int i = 0; i < n; i++) {
        if (slo[i] == ')') {
            akt++;
        } else {
            akt--;
        }
        if (change[i] == 1) {
            curst--;
            curcost -= cost[i];
            used.erase({-cost[i], i});
        }
        if (slo[i] == '(') {
            pq.push({-cost[i], i});
        }
        int have = k2/2 + ((akt+1)/2) * 2;
        while (have + curst > k + 1 && used.size() > 0) {
            auto [c, id] = *used.begin();
            curst--;
            curcost += c;
            used.erase({c, id});
            change[id] = 0;
        }
        while (!pq.empty() && !used.empty()) {
            auto [c1, id1] = *used.begin();
            auto [c2, id2] = pq.top();
            if (abs(c1) > abs(c2)) {
                used.insert({c2, id2});
                used.erase({c1, id1});
                pq.pop();
                pq.push({c1, id1});
                curcost += (c1-c2);
                change[id1] = 0;
                change[id2] = 1;
            } else {
                break;
            }
        }
//        cerr << i << " " << have << "\n";
        while (!pq.empty() && curst + have <= k) {
            auto [c, id] = pq.top();
            pq.pop();
            used.insert({-c, id});
            curst++;
            curcost += -c;
            change[id] = 1;
        }
        if (have + curst > k)
            cmn(best, curcost);
//        cerr << curcost << "\n";
    }
    cout << best - pocz_min << "\n";

}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

4 1
((()
480
617
-570
928

output:

480

result:

ok single line: '480'

Test #2:

score: 0
Accepted
time: 1ms
memory: 5412kb

input:

4 3
)()(
-532
870
617
905

output:

?

result:

ok single line: '?'

Test #3:

score: 0
Accepted
time: 7ms
memory: 8336kb

input:

77943 7851
())()(()))(()())(())()()()(((()(()())(()(()()))(()())((())))(()(())()())(()(())()()((()))()(((()))()((((((((()())))()((())))))((())(()()(())((())(((()()(()()()()()())(()(()()())(((()))(())))()))(())))((()))((()((((((()((()(()((()()))(())((()(()))()((()))(((()((()(((()((()))))(())))(()())(...

output:

-19567394

result:

ok single line: '-19567394'

Test #4:

score: 0
Accepted
time: 5ms
memory: 5788kb

input:

46462 26066
))(((()())))()))))))()()((((((())))))(()(()(()))))(()()()))))))())()()(()))(()(()(()()()))()(())((()((())(())(()((((())((((()())(())))(()()()))()(())())((()((()())))())())()(()(()()(()(()))()())((((()())(())))()((())))()))())(()())((())()(((()))(((())))))())((())))()(()(()())(()(((((()()...

output:

?

result:

ok single line: '?'

Test #5:

score: -100
Wrong Answer
time: 9ms
memory: 5888kb

input:

32132 1160
)(((()()))))())))))()()()))))()))(()()(()(()()()(())(())))(()(()))))())))))()()())())(((()))()()())))(((()())())))))()(((())))())()()(((()(((())(()(())(()()(()()()))))()(()()(())(()()(((())()(((((())((()(())))))(()(())))))())(())(((()))(())(()(((((()))((()))))))((()()(()()))(()())))(()(()...

output:

-7938664

result:

wrong answer 1st lines differ - expected: '-7957990', found: '-7938664'