萌新求调有注释代码 玄关+6r
查看原帖
萌新求调有注释代码 玄关+6r
672044
Graph_Theory楼主2024/7/17 11:02

wa on test4

4
Time: 30 ms, memory: 12 KB
Verdict: WRONG_ANSWER
Input
20
7 0 9 0 5 0 15 0 0 1 17 0 11 19 0 0 3 0 13 0
Participant's output
11
Jury's answer
15
Checker comment
wrong answer 1st numbers differ - expected: '15', found: '11'
#include <bits/stdc++.h>
using namespace std;
int n;
int a[101] = {3}, two, one, ans = 0, sumof0 = 0;
struct zyd
{
	int len, cz, jr;
} of[101];
bool book[101];
bool cmp(zyd aaa,zyd bbb)
{
	return aaa.len<bbb.len;
}
int main()
{
	cin >> n;
	if (n % 2 == 1) two = (n - 1) / 2, one = two + 1;
	else       one = n / 2, two = n / 2;
	for (int i = 1; i <= n; i++)
	{
		int x;
		cin >> x;
		book[x] = 1;
		if (x == 0)   		a[i] = 0;
		else if (x % 2 == 1) a[i] = 1, one--;//只保留他的奇偶性,
		else if (x % 2 == 0) a[i] = 2, two--;//计算剩余可放置的奇数和偶数的数量
		if (i != 1 && a[i] != 0 && a[i - 1] != 0 && a[i] != a[i - 1]) ans++;
	}														//本身存在的一对数
	int l = 0, r = 0;
	while (l <= n && r <= n)//寻找每一个0串
	{
		if (a[l] != 0)
		{
			l++;
			continue;
		}
		r = l;
		int flag = 0;
		while (r <= n)
		{
			r++;
			if (a[r] != 0)
			{
				flag = 1;
				break;
			}
		}

		of[++sumof0].len = abs(l - r);
		int st = a[l - 1], ed = a[r];
		if (flag == 0)
		{
			if (a[n] == 0)
			{
				of[sumof0].cz = 0;
				if (st % 2 == 1)
				{
					of[sumof0].jr = 1;
				}
				else
				{
					of[sumof0].jr = 2;
				}
				break;
			}
		}
		else if (st == 3)
		{
			of[sumof0].cz = 3;
			if (ed % 2 == 1)
			{
				of[sumof0].jr = 1;
			}
			else
			{
				of[sumof0].jr = 2;
			}
		}
		else if (st != ed)
		{
			of[sumof0].cz = 1;
		}
		else if (st == ed)
		{
			of[sumof0].cz = 2;
			if (st == 1)
			{
				of[sumof0].jr = 1;
			}
			else
			{
				of[sumof0].jr = 2;
			}
		}
		l = r;
	}
	sort(of + 1, of + sumof0 + 1,cmp);
	for (int i = 1; i <= sumof0; i++)
	{
		int cz = of[i].cz, num0 = of[i].len, jr = of[i].jr;
		//下面计算每一个0的区间的特点,并计算答案
		if (cz == 0) //当前零的区间是最后一个,
		{
			//且当前零的区间的最后一个零是整个数列的最后一个。
			if (jr == 1)
			{
				if (one >= num0) one -= num0;
				else ans++;
			}
			else
			{
				if (two >= num0) two -= num0;
				else ans++;
			}
			break;
		}
		else if (cz == 3)
		{
			//当前零的区间是第一个,
			//且当前零的区间的第一个零是整个数列的第一个。
			if (jr == 1)
			{
				if (one >= num0) one -= num0;
				else ans++;
			}
			else
			{
				if (two >= num0) two -= num0;
				else ans++;
			}
		}
		else if (cz == 1) ans++; //当前0的区间前后奇偶性不相同
		else if (cz == 2) //当前0的区间前后奇偶性相同
		{
			if (jr == 1)
			{
				if (one >= num0) one -= num0;
				else ans += 2;
			}
			else
			{
				if (two >= num0) two -= num0;
				else ans += 2;
			}
		}
	}
	cout << ans;
	return 0;
}
2024/7/17 11:02
加载中...