0%

Starter 189-Blocks in a String(减少块的数量)

思路讲解

使用分块以及分块思想,考虑一个未定义元素块。

https://www.codechef.com/problems/BLOCKSTR?tab=solution

那么我们这个为什么可以ans-=2?

1
....01????10.....

这种情况自然不必多说,那么确实是实打实的减少了2个块。

1
.....11??????11?????11......

那么我们的疑惑是这种情况下-2,-2可以吗?答案当然是可以的,这里的“?”全部变为1了以后总块数就是1了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
sort(all(cnts[0]));
sort(all(cnts[1]),greater<ll>());
sort(all(cnts[2]),greater<ll>());
ll ans=oans;
vector<bool> alde(N+5,false);
FOR(i,0,N){
if(i<cnt1){
cout<<-1<<" ";
continue;
}
if(i>N-cnt0){
cout<<-1<<" ";
continue;
}
if(i==cnt1){
cout<<ans<<" ";
continue;
}
if(!cnts[2].empty()){ // 1 1
ll id=SZ(cnts[2])-1;
cnts[2][id]--;
if(cnts[2][id]<=0){
cnts[2].pop_back();
ans-=2;
}
cout<<ans<<" ";
continue;
}
if(!cnts[1].empty()){
ll id=SZ(cnts[1])-1;
cnts[1][id]--;
if(cnts[1][id]<=0){
cnts[1].pop_back();
}
cout<<ans<<" ";
continue;
}
if(!cnts[0].empty()){
ll id=SZ(cnts[0])-1;
if(!alde[id]) ans+=2;
cnts[0][id]--;
alde[id]=true;
if(cnts[0][id]<=0){
cnts[0].pop_back();
}
cout<<ans<<" ";
continue;
}
}

AC代码

https://www.codechef.com/viewsolution/1168370649

心路历程(WA,TLE,MLE……)

这个0???0的这个块的排序要反过来,因为我们需要他更持久一点,因为他毕竟是要ans+=2的。

1
sort(all(cnts[0]));