好兄弟们,看看这个题把
查看原帖
好兄弟们,看看这个题把
346355
空kong楼主2023/6/5 18:31
#include <iostream>
using namespace std;


void NewNewFunc(int index,int ban);
int n = 0, B = 0;
int nums[100000] = { 0 };
int mycount = 0;
int leftCount[50000] = { 0 };
int rightCount[50000] = { 0 };
int rightCountValue[2][100000] = { 0 };
int main()
{	
	cin >> n >> B;
	
	int index = 0;
	bool isLeft = true;
	for (int i = 0; i < n; i++)
	{
		cin >> nums[i];
		
		if (nums[i] == B)//是这个数
		{
			index = i;
			isLeft = false;
			nums[i] = 0;
		}
		else if (nums[i] > B)
		{
			nums[i] = 1;
		}
		else if (nums[i] < B)
		{
			nums[i] = -1;
		}
		if (isLeft)
		{
			if (i-1 >= 0)
			{
				leftCount[i] = leftCount[i - 1] + nums[i];
			}
			else
			{
				leftCount[i] = nums[i];
			}
		}
		else if(index!=i)
		{
			if (i-index - 2 >= 0)
			{
				rightCount[i-index-1] = rightCount[i-index - 2] + nums[i];
			}
			else
			{
				rightCount[i-index-1] = nums[i];
			}
			
		}
	}
	
	NewNewFunc(index,n/2);
	
	cout << mycount;
	return 0;
}

void NewNewFunc(int index,int ban)
{
	for (int i = 0; i <= n-index-1; i ++)//加几个右边
	{
		int rightValue = 0;
		if (i > 0)
		{
			rightValue = rightCount[i - 1];
		}
		if (i % 2 == 0)
		{
			if (rightCountValue[0][rightValue + ban] != 0)
			{
				mycount += rightCountValue[0][rightValue + ban];
				continue;
			}
		}
		else
		{
			if (rightCountValue[1][rightValue + ban] != 0)
			{
				mycount += rightCountValue[1][rightValue + ban];
				continue;
			}
		}
		
		
		if (rightValue == 0)
		{
			mycount++;
			
		}
		if (i % 2 == 0)//偶数个右边,则要左边偶数个
		{
			for (int j = 2; j <= index; j+=2)//左边数量
			{
				int leftValue = leftCount[index - 1];
				if (index - j - 1 >= 0)
				{
					leftValue = leftCount[index - 1] - leftCount[index - j - 1];
				}
				if (leftValue + rightValue == 0)
				{
					rightCountValue[0][rightValue + ban]++;
					mycount++;
				}
			}
		}
		else
		{
			//左边加奇数个
			for (int j = 1; j <= index; j += 2)//左边数量
			{
				int leftValue = leftCount[index - 1];
				if (index - j - 1 >= 0)
				{
					leftValue = leftCount[index - 1] - leftCount[index - j - 1];
				}
				
				if (leftValue + rightValue == 0)
				{
					rightCountValue[1][rightValue + ban]++;
					mycount++;
				}
			}
		}
		
	}
}


我的代码是这样的,为什么第2个就是错的呢

2023/6/5 18:31
加载中...