关于今天的B题
  • 板块学术版
  • 楼主蒟蒻炒扇贝
  • 当前回复2
  • 已保存回复2
  • 发布时间2022/2/13 20:45
  • 上次更新2023/10/28 08:37:15
查看原帖
关于今天的B题
19228
蒟蒻炒扇贝楼主2022/2/13 20:45

没有想让这份代码AC的意思,因为我这份代码空间复杂度较高所以数组大小只开到了 10510^5

这里代码的思路参考了官方题解,样例能过,但只能AC三个点,求数据量小且可以hack掉此代码的数据。

//Coded by weak_shell
#include<bits/stdc++.h>
using namespace std;
#define pir pair<int,int>
#define fs first
#define sc second
int read()
{
    int x=0,fh=1;
    char ch=getchar();
    while(!isdigit(ch))
	{
        if(ch=='-') fh=-1;
        ch=getchar();
    }
    while(isdigit(ch))
	{
        x=(x<<1)+(x<<3)+ch-'0';
        ch=getchar();
    }
    return x*fh;
}
const int MAXN=1e5+5;
struct node
{
	char hd,tl;
	int cb;
};
stack<node>suf[MAXN];
int n;
string s[MAXN];
int get_ans(char he)
{
	int maxx=0;
	#define hf suf[i].size()
	for(int i=1;i<=n;i++)
	{
		int cnt=0;
		char now=s[i][s[i].size()-1];
		suf[i].push(node{now,now,0});
		maxx++;
		for(int j=s[i].size()-2;j>=1;j--)
		{
			if(now==s[i][j])cnt++;
			else now=s[i][j];
			suf[i].push(node{s[i][j],s[i][s[i].size()-1],cnt});
			maxx++;
		}
	}
	char ed=he;
	int ans=0;
	while(maxx)
	{
		if(ed=='0')
		{
			for(int i=1;i<=n;i++)if(hf&&suf[i].top().hd=='0'&&suf[i].top().tl=='0')
			{
				if(ed=='0'&&ans)ans++;
				maxx--,ans+=suf[i].top().cb,suf[i].pop(),i--;
				ed='0';
			}
			for(int i=1;i<=n;i++)if(hf&&suf[i].top().hd=='0'&&suf[i].top().tl=='1')
			{
				if(ed=='0'&&ans)ans++;
				maxx--,ans+=suf[i].top().cb,suf[i].pop(),i--;
				ed='1';
			}
			for(int i=1;i<=n;i++)if(hf&&suf[i].top().hd=='1'&&suf[i].top().tl=='1')
			{
				if(ed=='1'&&ans)ans++;
				maxx--,ans+=suf[i].top().cb,suf[i].pop(),i--;
				ed='1';
			}
			for(int i=1;i<=n;i++)if(hf&&suf[i].top().hd=='1'&&suf[i].top().tl=='0')
			{
				if(ed=='1'&&ans)ans++;
				maxx--,ans+=suf[i].top().cb,suf[i].pop(),i--;
				ed='0';
			}
		}
		else
		{
			for(int i=1;i<=n;i++)if(hf&&suf[i].top().hd=='1'&&suf[i].top().tl=='1')
			{
				if(ed=='1'&&ans)ans++;
				maxx--,ans+=suf[i].top().cb,suf[i].pop(),i--;
				ed='1';
			}
			for(int i=1;i<=n;i++)if(hf&&suf[i].top().hd=='1'&&suf[i].top().tl=='0')
			{
				if(ed=='1'&&ans)ans++;
				maxx--,ans+=suf[i].top().cb,suf[i].pop(),i--;
				ed='0';
			}
			for(int i=1;i<=n;i++)if(hf&&suf[i].top().hd=='0'&&suf[i].top().tl=='0')
			{
				if(ed=='0'&&ans)ans++;
				maxx--,ans+=suf[i].top().cb,suf[i].pop(),i--;
				ed='0';
			}
			for(int i=1;i<=n;i++)if(hf&&suf[i].top().hd=='0'&&suf[i].top().tl=='1')
			{
				if(ed=='0'&&ans)ans++;
				maxx--,ans+=suf[i].top().cb,suf[i].pop(),i--;
				ed='1';
			}
		}
 	}
	return ans;
}
signed main()
{
	cin>>n;
	getchar();
	int flag0=0,flag1=0;
	for(int i=1;i<=n;i++)
	{
		cin>>s[i];
		if(s[i][0]=='0')flag0=1;
		else flag1=1;
	}
	int ans1=0,ans2=0;
	if(flag0)ans1=get_ans('0');
	if(flag1)ans2=get_ans('1');
	//cout<<sizeof(suf)/1e6<<endl;
	cout<<max(ans1,ans2);
}
2022/2/13 20:45
加载中...