QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#831246#1289. A + B ProblemlyxAC ✓60ms14852kbC++142.1kb2024-12-25 12:12:012024-12-25 12:12:06

Judging History

This is the latest submission verdict.

  • [2024-12-25 12:12:06]
  • Judged
  • Verdict: AC
  • Time: 60ms
  • Memory: 14852kb
  • [2024-12-25 12:12:01]
  • Submitted

answer

#include<bits/stdc++.h>
#define fo(i,l,r) for(int i=(l);i<=(r);++i)
#define fd(i,l,r) for(int i=(l);i>=(r);--i)
#define fu(i,l,r) for(int i=(l);i<(r);++i)
#define pi pair<int,int>
#define eb emplace_back
#define vi vector<int>
#define mp make_pair
#define ll long long
using namespace std;
void out(int x){if(x>1)out(x>>1);putchar(48+(x&1));}
const int N=2000003;
int n,m,a[N],ans[N],t1[N],t2[N],x,y,pos[N],cnt[N];
char s[N];
bool chk(int st,int z)
{
	if(x>y)
	{
		if(s[st]=='0') return 0;
		int w=pos[cnt[st]+z];
		if(w-cnt[w]-(st-cnt[st])>=x-z+1) return 1;
		return 0;
	}
	else
	{
		if(s[st]=='0') return 0;
		int w=pos[cnt[st]+z];
		if(w-cnt[w]-(st-cnt[st])>=y-z+1) return 1;
		return 0;
	}
//	int u=x,v=y;
//	fo(i,st,n+m)
//	{
//		if(s[i]=='1')
//		{
//			if(u>v)
//			{
//				if(v>=z) v--;
//				else u--;
//			}
//			else
//			{
//				if(u>=z) u--;
//				else v--;
//			}
//		}
//		else
//		{
//			if(u>v)
//			{
//				if(v>=z) return 0;
//				v--;
//				if(v<0) return 0;
//			}
//			else
//			{
//				if(u>=z) return 0;
//				u--;
//				if(u<0) return 0;
//			}
//		}
//		if(u<z&&v<z) return 1;
//	}
	return 0;
}
void work()
{
	scanf("%d%d",&n,&m);
	scanf("%s",s+1);
	x=n,y=m;
	fo(i,1,n+m+1) t1[i]=t2[i]=ans[i]=0;
	fo(i,1,n+m) pos[i]=n+m;
	fo(i,1,n+m)
	{
		cnt[i]=cnt[i-1]+(s[i]=='0');
	}
	fd(i,n+m,1) pos[cnt[i]]=i;
	fo(i,1,n+m)
	{
		int cur=s[i]-'0';
		if(x==y||!y)
		{
			t1[x]=cur;
			x--;continue;
		}
		if(!x)
		{
			t2[y]=cur;
			y--;continue;
		}
		if(cur==0)
		{
			if(x<y) t1[x]=cur,x--;
			else t2[y]=cur,y--;
			continue;
		}
		if(x>y)
		{
			if(chk(i,y)) t2[y]=cur,y--;
			else t1[x]=cur,x--;
		}
		else
		{
			if(chk(i,x)) t1[x]=cur,x--;
			else t2[y]=cur,y--;
		}
	}
//	fd(i,n+m+1,1) printf("%d",t1[i]); printf("\n");
//	fd(i,n+m+1,1) printf("%d",t2[i]); printf("\n");
	fo(i,1,n+m+1)
	{
		ans[i]+=t1[i]+t2[i];
		if(ans[i]>1) ans[i]-=2,ans[i+1]++;
	}
	int l=n+m+1;
	while(l>1&&!ans[l]) l--;
	fd(i,l,1) printf("%d",ans[i]);
	printf("\n");
}
int main()
{
	int T;scanf("%d",&T);
	while(T--) work();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 12020kb

input:

3
4 3
1000101
2 2
1111
1 1
00

output:

1101
110
0

result:

ok 3 lines

Test #2:

score: 0
Accepted
time: 60ms
memory: 14852kb

input:

11110
10 8
111011010011100100
3 5
01011000
7 6
1110101010000
9 1
0110100101
1 9
0100001110
8 10
000101101011111000
9 6
011111111000111
1 9
1011101101
10 7
00100011000100000
4 9
1000101101010
8 4
100100110000
8 9
00101111011000101
8 9
11000000101011110
7 6
1111010100110
2 9
01001110101
4 5
100010100
...

output:

10011010100
11100
10101000
110100101
100001110
10000001100
1000010111
111101101
1110100000
111101010
11110000
1000011101
1001011110
10101110
101110101
11100
1111010
1000010
1011100010
10010101001
10010001
1001010
1000000010
1110
111
1111110001
10110111
1100010101
10000000
111000011
110
11111
1100101...

result:

ok 11110 lines