Luogu P3551 [POI2013] USU-Take-out solution

Topic link

Although it is a green question, I personally feel that the idea is not very good.

First, we assume that we can pass the eliminated bricks, and consider stack maintenance.
We push elements (bricks) into the stack in order, and whenever an element is pushed, check the top of the stack k + 1 k+1k+1 if there is just one piece of black brick elements. If this condition is met, thenk + 1 k+1k+1 block is eliminated.

Draw this plan into a picture like this

image.png

It can be found that these arcs of different colors are mutually contained, and it can be proved that the two arcs will not intersect.
Then, we reverse the order of operations of the above scheme to satisfy the operation that is not included first . In this way, blocks that have already been eliminated will not be passed.

Here is a perceptual understanding of why the two arcs do not cross.
image.png
Obviously, when performing the red operation, B \text{B}Block B is definitely better thanA \text{A}Block A is preferred, so there will be no crossover

#include<cstdio>
#include<iostream>
#include<vector>
using namespace std;
const int Maxn=1000000+10;
vector <int> g[Maxn];
int a[Maxn],c[Maxn];
int sum[Maxn];
char s[Maxn];
int n,k,cnt,tot;
inline int lowbit(int x)
{
    
    
	return x&(-x);
}
void modify(int x,int k)
{
    
    
	while(x<=n)
	{
    
    
		sum[x]+=k;
		x+=lowbit(x);
	}
}
int query(int x)
{
    
    
	int ret=0;
	while(x)
	{
    
    
		ret+=sum[x];
		x-=lowbit(x);
	}
	return ret;
}
int main()
{
    
    
//	freopen("in.txt","r",stdin);
	scanf("%d%d",&n,&k);
	scanf("%s",s+1);
	for(int i=1;i<=n;++i)
	if(s[i]=='c')a[i]=1;
	else a[i]=0;
	for(int i=1;i<=n;++i)
	{
    
    
		c[++cnt]=i;
		if(s[i]=='c')modify(cnt,1);
		if(cnt>k && query(cnt)-query(cnt-k-1)==1)
		{
    
    
			++tot;
			for(int j=1;j<=k+1;++j)
			{
    
    
				if(s[c[cnt]]=='c')
				modify(cnt,-1);
				g[tot].push_back(c[cnt--]);
			}
		}
	}
	for(int i=tot;i;--i)
	{
    
    
		for(int j=g[i].size()-1;j>=0;--j)
		printf("%d ",g[i][j]);
		putchar('\n');
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/Brian_Pan_/article/details/110851109