QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#798381#8783. Cherry Pickingpiaoyun#WA 2ms12068kbC++202.2kb2024-12-04 13:07:272024-12-04 13:07:28

Judging History

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

  • [2024-12-04 13:07:28]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:12068kb
  • [2024-12-04 13:07:27]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int MAXN = 1e6 + 10;
int N,M,K,Q;
int a[MAXN];
string str;
int fa[MAXN];
int sz[MAXN];
int L[MAXN];
int R[MAXN];
int getfa(int x){
    if(fa[x] == x) return x;
    return fa[x] = getfa(fa[x]);
}

int tot = 0;

void merge(int x, int y){
    x = getfa(x);
    y = getfa(y);
    if(x == y) return;
    if(sz[x] >= K) tot--;
    if(sz[y] >= K) tot--;
    fa[y] = x;
    sz[x] += sz[y];
    if(sz[x] >= K) tot++;
    L[x] = min(L[x], L[y]);
    R[x] = max(R[x], R[y]);
}

int getsz(int x){
    return sz[getfa(x)];
}

void prepare(){
	cin >> N >> K;
	for(int i = 1; i <= N; i++){
		cin >> a[i];
	}
    cin >> str;
    for(int i = 1; i <= N; i++){
        sz[i] = str[i-1] - '0';
        fa[i] = i;
        if(K == 1) tot += str[i-1] - '0';
        L[i] = R[i] = i;
    }
    for(int i = 1; i <= N - 1; i++){
        if(sz[i] && sz[i+1]){
            merge(i, i+1);
        }
    }
    vector<pair<int,int>> v;
    for(int i = 1; i <= N; i++){
        v.emplace_back(a[i], i);
    }
    sort(v.begin(), v.end());
    int ans = 0;
    if(tot){
        ans = 1e18;
        for(int i = 1; i <= N; i++){
            ans = min(ans, a[i]);
        }
    }
    for(int l = 0, r = 0; l < N; l = r){
        while(r < N && v[r].first == v[l].first){
            r++;
        }
        for(int i = l; i < r; i++){
            int p = v[i].second;
            int f = getfa(p);
            if(str[p - 1] == '1'){
                if(sz[f] >= K) tot--;
                sz[f]--;
                if(sz[f] >= K) tot++;
            }
            if(sz[f] == 0){
                if(L[f] > 1){
                    merge(L[f]-1, L[f]);
                }
                int g = getfa(f);
                if(sz[g] > 0 && sz[R[g]+1] > 0){
                    merge(R[g], R[g] + 1);
                }
            }
        }
        // cout << v[l].first << " " << tot << endl;
        if(tot) ans = max(ans, v[l].first + 1);
    }
    cout << ans << '\n';
}

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	int T = 1;
	// cin >> T;
	while(T--){
		prepare();
	}
}



Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 11832kb

input:

5 2
1 2 3 4 5
01101

output:

2

result:

ok answer is '2'

Test #2:

score: 0
Accepted
time: 0ms
memory: 11772kb

input:

5 2
3 4 5 2 1
10101

output:

0

result:

ok answer is '0'

Test #3:

score: 0
Accepted
time: 2ms
memory: 12004kb

input:

1 1
1
1

output:

1

result:

ok answer is '1'

Test #4:

score: 0
Accepted
time: 2ms
memory: 11724kb

input:

1 1
1
0

output:

0

result:

ok answer is '0'

Test #5:

score: -100
Wrong Answer
time: 0ms
memory: 12068kb

input:

5 3
8 3 5 2 7
10101

output:

4

result:

wrong answer expected '5', found '4'