QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#377627#6185. Best ProblemKevin5307WA 99ms182804kbC++205.8kb2024-04-05 16:06:412024-04-05 16:19:26

Judging History

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

  • [2024-04-05 16:19:26]
  • 自动重测本题所有获得100分的提交记录
  • 测评结果:WA
  • 用时:99ms
  • 内存:182804kb
  • [2024-04-05 16:19:14]
  • hack成功,自动添加数据
  • (/hack/587)
  • [2024-04-05 16:06:42]
  • 评测
  • 测评结果:100
  • 用时:93ms
  • 内存:188084kb
  • [2024-04-05 16:06:41]
  • 提交

answer

//Author: Kevin
#include<bits/stdc++.h>
//#pragma GCC optimize("O2")
using namespace std;
#define ll long long
#define ull unsigned ll
#define pb emplace_back
#define mp make_pair
#define ALL(x) (x).begin(),(x).end()
#define rALL(x) (x).rbegin(),(x).rend()
#define srt(x) sort(ALL(x))
#define rev(x) reverse(ALL(x))
#define rsrt(x) sort(rALL(x))
#define sz(x) (int)(x.size())
#define inf 0x3f3f3f3f
#define pii pair<int,int>
#define lb(v,x) (int)(lower_bound(ALL(v),x)-v.begin())
#define ub(v,x) (int)(upper_bound(ALL(v),x)-v.begin())
#define uni(v) v.resize(unique(ALL(v))-v.begin())
#define longer __int128_t
void die(string S){puts(S.c_str());exit(0);}
struct segment
{
	int lpos,rpos,cnt;
	ll sum;
	bool type;
	segment():sum(-1){}
	segment(int _lpos,int _rpos,int _cnt,ll _sum,bool _type):
		lpos(_lpos),rpos(_rpos),cnt(_cnt),sum(_sum),type(_type){}
};
ll calc(int cnt,int pos)
{
	int r=(cnt-1)*4+pos;
	return 1ll*(r+pos)*cnt/2;
}
ll dp[5005000][2];
ll total[5005000];
int main()
{
	// freopen("schrodingerszomb12.in","r",stdin);
	// freopen("out.txt","w",stdout);
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	for(int i=5;i<5005000;i++)
	{
		int x=(i-1)/4*4;
		total[i]+=x;
		total[i]+=total[x-1];
	}
	int t=1;
	while(t--)
	{
		string s;
		cin>>s;
		s="1"+s+"0";
		int n=sz(s);
		vector<pii> vec;
		for(int i=1;i<n;i++)
			if(s[i]==s[i-1])
				vec.pb(s[i]^48,i);
		// for(auto pr:vec)
		// 	cerr<<"? "<<pr.first<<" "<<pr.second<<endl;
		vector<pii> tmp;
		ll ssum=0;
		for(auto pr:vec)
			if(!sz(tmp)||pr.first==0||tmp.back().first==1||(pr.second-tmp.back().second)%4)
				tmp.pb(pr);
			else
			{
				ssum+=pr.second-tmp.back().second;
				tmp.pop_back();
			}
		swap(tmp,vec);
		// for(auto pr:vec)
		// 	cerr<<"? "<<pr.first<<" "<<pr.second<<endl;
		vector<pii> vec2;
		vector<int> vb;
		vb.pb(0);
		for(int i=1;i<sz(vec);i++)
			if((vec[i].second-vec[i-1].second)%4)
				vb.pb(i);
		vb.pb(sz(vec));
		vector<segment> cur;
		auto add=[&](int cnt,int pos,ll sum,bool type)->void
		{
			// cerr<<"! "<<cnt<<" "<<pos<<" "<<sum<<" "<<type<<endl;
			if(!sz(cur)||cur.back().type!=type)
			{
				cur.pb(pos,pos+(cnt-1)*4,cnt,sum,type);
				return ;
			}
			if(!type)
			{
				int rpos=cur.back().rpos;
				int need=(pos-rpos-1)/4;
				pos-=need*4;
				sum-=4ll*cnt*need;
				cur.back().rpos=pos+(cnt-1)*4;
				cur.back().cnt+=cnt;
				cur.back().sum+=sum;
			}
			else
			{
				int need=(pos-cur.back().rpos-1)/4;
				cur.back().lpos+=need*4;
				cur.back().rpos=pos+(cnt-1)*4;
				cur.back().sum+=sum+4ll*cur.back().cnt*need;
				cur.back().cnt+=cnt;
			}
		};
		for(int i=1;i<sz(vb);i++)
		{
			int l=vb[i-1],r=vb[i]-1;
			// cerr<<"& "<<l<<" "<<r<<endl;
			int cnt0=(r-l+1)/2,cnt1=(r-l+1)/2;
			if((r-l+1)%2)
			{
				if(vec[l].first)
					cnt1++;
				else
					cnt0++;
			}
			if(cnt1)
				add(cnt1,vec[l].second,calc(cnt1,vec[l].second),0);
			if(cnt0)
				add(cnt0,vec[r].second,calc(cnt0,vec[r].second-4*cnt0+4),1);
		}
		// for(auto seg:cur)
		// 	cerr<<seg.cnt<<" "<<seg.sum<<" "<<seg.lpos<<" "<<seg.rpos<<" "<<seg.type<<endl;
		// cerr<<endl;
		for(int i=0;i<=sz(cur)+10;i++)
			dp[i][0]=dp[i][1]=-0x3f3f3f3f3f3f3f3f;
		dp[0][0]=dp[0][1]=0;
		vector<pair<segment,segment>> parts;
		for(auto seg:cur)
			if(seg.type)
			{
				if(!sz(parts)||~parts.back().second.sum)
					parts.pb(segment(),seg);
				else
					parts.back().second=seg;
			}
			else
			{
				if(!sz(parts)||~parts.back().first.sum||~parts.back().second.sum)
					parts.pb(seg,segment());
				else
					parts.back().first=seg;
			}
		for(int i=0;i<sz(parts);i++)
		{
			auto pr=parts[i];
			if(pr.first.sum==-1)
			{
				assert(i==0);
				ll val=max(dp[i][0],dp[i][1]);
				val+=pr.second.sum;
				int len=pr.second.lpos-1;
				if(len>4)
				{
					int cnt=(len-1)/4;
					cnt=(cnt+1)/2;
					ll R=len-1,L=R-cnt*8;
					val+=(L+R)*cnt/2;
				}
				dp[i+1][0]=max(dp[i+1][0],val);
				int rbound=n-1;
				if(i!=sz(parts)-1)
					rbound=parts[i+1].first.lpos-1;
				int mv=(rbound-pr.second.rpos)/4;
				pr.second.lpos+=mv*4;
				pr.second.sum+=4ll*pr.second.cnt*mv;
				val=max(dp[i][0],dp[i][1]);
				val+=pr.second.sum;
				len=pr.second.lpos-1;
				// cerr<<"! "<<len<<endl;
				if(len>4)
					val+=total[len];
				dp[i+1][1]=max(dp[i+1][1],val);
			}
			else if(pr.second.sum==-1)
			{
				assert(i==sz(parts)-1);
				{
					ll val=dp[i][1];
					val-=pr.first.sum;
					int len=n-pr.first.rpos;
					if(len>4)
						val+=total[len];
					dp[i+1][1]=max(dp[i+1][1],val);
				}
				{
					ll val=dp[i][0];
					int lbound=1;
					if(i)
						lbound=parts[i-1].second.rpos+1;
					int mv=(pr.first.lpos-lbound)/4;
					pr.first.sum-=4ll*pr.first.cnt*mv;
					pr.first.rpos-=4*mv;
					val-=pr.first.sum;
					int len=n-pr.first.rpos;
					if(len>4)
						val+=total[len];
					dp[i+1][1]=max(dp[i+1][1],val);
				}
			}
			else
			{
				for(int a=0;a<2;a++)
					for(int b=0;b<2;b++)
					{
						int mvl=0,mvr=0;
						if(!a)
						{
							int lbound=1;
							if(i)
								lbound=parts[i-1].second.rpos+1;
							mvl=(parts[i].first.lpos-lbound)/4;
						}
						if(b)
						{
							int rbound=n-1;
							if(i!=sz(parts)-1)
								rbound=parts[i+1].first.lpos-1;
							mvr=(rbound-parts[i].second.rpos)/4;
						}
						ll val=dp[i][a]-(pr.first.sum-4ll*mvl*pr.first.cnt)+(pr.second.sum+4ll*mvr*pr.second.cnt);
						int len=(pr.second.lpos-pr.first.rpos-1);
						len+=(mvl+mvr)*4;
						if(len>4)
							val+=total[len];
						dp[i+1][b]=max(dp[i+1][b],val);
					}
			}
			// cerr<<dp[i+1][0]<<" "<<dp[i+1][1]<<endl;
		}
		ll tot=0;
		for(auto pr:vec)
			if(pr.first)
				tot+=pr.second;
			else
				tot-=pr.second;
		if(!sz(parts))
			cout<<total[n-1]<<'\n';
		else
			cout<<(dp[sz(parts)][1]+tot+ssum)/4<<'\n';
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 12ms
memory: 42696kb

input:

10100010011001011111

output:

5

result:

ok 1 number(s): "5"

Test #2:

score: 0
Accepted
time: 3ms
memory: 42636kb

input:

0000010101100110101101010110000110100111000010010111111100101101110101000111101111010101010010101010

output:

58

result:

ok 1 number(s): "58"

Test #3:

score: 0
Accepted
time: 12ms
memory: 72256kb

input:

100011000011001011010100111110011001000110111101101001100000110101101001101111100101110001101101000001001011011111001100111010101111001110000110001100101100101001001110000111100001100110000101111110001010101100100110010001110011101011110011101111000111010111100110100011110000011111110000111110111110...

output:

302244

result:

ok 1 number(s): "302244"

Test #4:

score: 0
Accepted
time: 7ms
memory: 48496kb

input:

100101010101010101010101010101010101010101100101101010101101010110101001001010101010101101010101010010101010010101010110101010101010101010101101010110101010101010101010101010101010101010101010101010101010101010101010101010101011010101010101010101010101011010010101110101010110101010101010101010111010...

output:

3328566

result:

ok 1 number(s): "3328566"

Test #5:

score: 0
Accepted
time: 12ms
memory: 45344kb

input:

101010101010101010101101010101010101010101010101010101010100101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101001010101010101010101010101010101010101010101010101010101010110...

output:

36918913

result:

ok 1 number(s): "36918913"

Test #6:

score: 0
Accepted
time: 4ms
memory: 45356kb

input:

101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010...

output:

371689423

result:

ok 1 number(s): "371689423"

Test #7:

score: 0
Accepted
time: 8ms
memory: 45424kb

input:

101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010...

output:

3378077368

result:

ok 1 number(s): "3378077368"

Test #8:

score: 0
Accepted
time: 3ms
memory: 45364kb

input:

101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010...

output:

31250155082

result:

ok 1 number(s): "31250155082"

Test #9:

score: 0
Accepted
time: 8ms
memory: 45380kb

input:

101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010...

output:

21976748120

result:

ok 1 number(s): "21976748120"

Test #10:

score: 0
Accepted
time: 8ms
memory: 45380kb

input:

101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010...

output:

31249989871

result:

ok 1 number(s): "31249989871"

Test #11:

score: 0
Accepted
time: 4ms
memory: 45536kb

input:

101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010...

output:

15417990032

result:

ok 1 number(s): "15417990032"

Test #12:

score: 0
Accepted
time: 8ms
memory: 45400kb

input:

101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010...

output:

31249770935

result:

ok 1 number(s): "31249770935"

Test #13:

score: 0
Accepted
time: 99ms
memory: 182804kb

input:

001001110010100110000111011111010010000100110011010111100100011110101100000001000011001011010000100101101110001010011110010000000101110101101101001011000101011000001001110011001101111010111010000100011011111101101001000001111000001101100011011011111100001000000011001111110000100001111110111000011110...

output:

1529176

result:

ok 1 number(s): "1529176"

Test #14:

score: 0
Accepted
time: 71ms
memory: 133392kb

input:

111100101110111001010010100101010101110101100100011110100001010110101101000010100111001101110011011010010101100101010111001001100111001010111010100111010001110110000001001100001010000101011011010101010011000111010111111011011000010101101001010101010101111010101100101000010100101101110101110101011100...

output:

3453809

result:

ok 1 number(s): "3453809"

Test #15:

score: 0
Accepted
time: 44ms
memory: 105724kb

input:

010100110101010101111100101010101010010100101010010101010010010101011010101010100101010101010011100110101011010011100101010101001010101010110011001010101101101110101010101010101010100101010101100100101010101100010101001010101010101010110100101001010010100101101010101010101010101011101001010101101010...

output:

7267243

result:

ok 1 number(s): "7267243"

Test #16:

score: 0
Accepted
time: 40ms
memory: 84436kb

input:

101100101010101010100010101010101001101010101010101010101101011010100101010101001010110101010101011011001010010101010101010001101010010010101010101101010100010101010101010100101010100101010101010101010101010010101000101010111010010110100101001010101101010101010110101001001010101001101010101010101010...

output:

11025428

result:

ok 1 number(s): "11025428"

Test #17:

score: 0
Accepted
time: 24ms
memory: 75840kb

input:

101010101010101101001010101010101011010110010101001010101010100101010101010101010101010101010101010101010110101010101010101010101010101010110101010101010101010101010011010010101010110101010101001010110101010010101010100101011101010110101010010100101010010101101010101010101010101101010101010011010101...

output:

16770603

result:

ok 1 number(s): "16770603"

Test #18:

score: 0
Accepted
time: 4ms
memory: 56872kb

input:

101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101001010101010101010101010101010101010101010101010101010101010101010101010101010110101001010101010101010101010101010101010101010101010101010101010101...

output:

188646665

result:

ok 1 number(s): "188646665"

Test #19:

score: 0
Accepted
time: 27ms
memory: 56900kb

input:

101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010110101010101010101010101010101010101010101010101010101010101010101010101010101010101010100101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010...

output:

1815374361

result:

ok 1 number(s): "1815374361"

Test #20:

score: 0
Accepted
time: 15ms
memory: 56900kb

input:

101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010...

output:

16307938305

result:

ok 1 number(s): "16307938305"

Test #21:

score: 0
Accepted
time: 8ms
memory: 56884kb

input:

101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010...

output:

184171987292

result:

ok 1 number(s): "184171987292"

Test #22:

score: 0
Accepted
time: 19ms
memory: 56896kb

input:

101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010...

output:

128638532514

result:

ok 1 number(s): "128638532514"

Test #23:

score: 0
Accepted
time: 15ms
memory: 56864kb

input:

101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010...

output:

434363327492

result:

ok 1 number(s): "434363327492"

Test #24:

score: 0
Accepted
time: 19ms
memory: 57036kb

input:

101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010...

output:

493093081142

result:

ok 1 number(s): "493093081142"

Test #25:

score: 0
Accepted
time: 21ms
memory: 56900kb

input:

101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010...

output:

669857207760

result:

ok 1 number(s): "669857207760"

Test #26:

score: 0
Accepted
time: 21ms
memory: 57044kb

input:

101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010...

output:

468842033630

result:

ok 1 number(s): "468842033630"

Test #27:

score: 0
Accepted
time: 15ms
memory: 56868kb

input:

101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010...

output:

639985723182

result:

ok 1 number(s): "639985723182"

Test #28:

score: 0
Accepted
time: 23ms
memory: 56832kb

input:

101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010...

output:

391921131742

result:

ok 1 number(s): "391921131742"

Test #29:

score: 0
Accepted
time: 7ms
memory: 56764kb

input:

101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010...

output:

781249375000

result:

ok 1 number(s): "781249375000"

Test #30:

score: 0
Accepted
time: 7ms
memory: 56988kb

input:

101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010...

output:

781249997600

result:

ok 1 number(s): "781249997600"

Extra Test:

score: -3
Extra Test Failed : Wrong Answer on 2
time: 7ms
memory: 42936kb

input:

0101010101

output:

12

result:

wrong answer 1st numbers differ - expected: '3', found: '12'