Official

C - T-shirts Editorial by en_translator


First, Takahashi can wash all the T-shirts on a day with no plans, resetting the number of available T-shirts, so one can split the sequence of plans by 0 and think chunk-wise independently. Also, by the condition of the problem statement, if one can satisfy the condition by buying \(a\) logo T-shirts, then one can do so by buying any number of logo T-shirts greater than \(a\).
Thus, if 0 occurs \(c_0\) times in \(S\) at positions \(d_1,d_2,\ldots, d_{c_0}\), one can find the minimum number of logo T-shirts required to wear T-shirts to satisfy the conditions within the \(1\)-st through \((d_1-1)\)-th days, \((d_1+1)\)-th through \((d_2-1)\)-th days, \(\ldots\), \((d_{c_0-1}+1)\)-th through \((d_{c_0}-1)\)-th days, and \((d_{c_0}+1)\)-th through \(N\)-th days; then the sought answer can be obtained as the maximum among them.


Next, given a sequence of plans represented by a length-\(k\) string \(T\) consisting of 1 and 2, we consider the minimum number of T-shirts required to buy to satisfy the conditions within the days.
If 1 occurs \((k-x)\) times and 2 occurs \(x\) times in \(T\), it is necessary that:

  • he owns at least \(x\) logo T-shirts, and
  • he owns at least \(x\) T-shirts (whether plain or with logo).

This is evident because Takahashi needs to wear a logo T-shirts on a day with a competitive programming event, and wear an either kind of T-shirts on a day with a luncheon or an event.

Conversely, if these conditions are satisfied, i.e. if Takahashi buys \(y=\max(x,k-M)\) or more logo T-shirts, he can wear T-shirts to satisfy the conditions as follows:

  • if he has a plan to go out for a meal, wear a plain T-shirt if he has any washed, and a logo T-shirt otherwise.
  • if he has a plan for a competitive programming event, wear a logo T-shirt.
Proof Assume that he bought $y=\max(x,k-M)$ or more T-shirt and obey the rules above, but he does not have a T-shirt to wear on the $t$-th $(1\leq t\leq k)$. Since there are $k$ or more T-shirts, there is at least one T-shirt, either plain or with logo. Thus, the situation is limited to where he is going to an event on that day, but has only a plain T-shirt. However, (by the choices of shirts described above) he never wears a logo T-shirt on a day for a meal during the first $(t-1)$ days, so it appears that $(y+1)$ or more events occurred during the first $t$ days. But, during the $k$ days an event occurs at most $x(\leq y)$ times, so this is impossible. Therefore, it never happens that he has no T-shirt to wear during the $k$ days, thus he can wear T-shirts to satisfy the conditions in the $k$ days.


By this fact, it turns out that if a sequence of plans for \(k\) days consists of \((k-x)\) 1s and \(x\) 2s, he needs to buy at least \(\max(x,k-M)\) T-shirts to wear them to satisfy the conditions.

All that left is to find this value for each chunk split by 0, and find the maximum value among them.

To find this value, one can scan \(S\) from left to right while counting the occurrences of 1 and 2, and every time it encounters 0 or the end of the string, find \(y=\max(x,k-M)\) and reset the count. Here, no character in \(S\) is inspected twice, so the complexity is \(O(N)\), which is fast enough.
Therefore, the problem has been solved.
Note that you can append 0 to the tail of \(S\) to handle the end of the string and 0 uniformly.

Sample code in C++:

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

int main(void){
	int n,m;
	int ans=0;
	int x=0,y=0;
	string s;

	cin>>n>>m;
	cin>>s;
	s+="0";

	for(int i=0;i<=n;i++){
		if(s[i]=='0'){
			ans=max(ans,max(x+y-m,y));
			x=0,y=0;
		}
		if(s[i]=='1')x++;
		if(s[i]=='2')y++;
	}
	
	cout<<ans<<endl;
	return 0;
}

posted:
last update: