WA 50pts 萌新求助
查看原帖
WA 50pts 萌新求助
377873
EricWan楼主2023/8/20 18:38

贪心与分制,分治的逻辑与 EstasTonne 大佬的题解差不多,但码风独特,只有 50 分,求调。

#include <bits/stdc++.h>
// #define int long long
using namespace std;
int n, l[100005], r[100005], sz[100005], ls[100005], rs[100005], cnt = 1;
vector<int> Yuan[50005];
void dfsinput(int id, int size)
{
	sz[id] = size;
	if (size <= 1)
	{
		return;
	}
	cin >> l[id];
	r[id] = size - l[id] - 1;
	ls[id] = (++cnt);
	dfsinput(ls[id],l[id]);
	rs[id] = (++cnt);
	dfsinput(rs[id],r[id]);
}
// void //putv(vector<int> v)
// {
// 	for (auto i : v)
// 	{
// 		cout << i << " ";
// 	}
// 	cout << endl;
// }
vector<int> yuan(int id)
{
//	//cout << "yuan " << id << endl;
	vector<int> ans;
	ans.clear();
//	//cout << sz[id];
	if (Yuan[id].size())
	{
		return Yuan[id];
	}
	if (sz[id] <= 1)
	{
		return ans;
	}
	ans.push_back(l[id]);
	vector<int> a = yuan(ls[id]);
	vector<int> b = yuan(rs[id]);
//	//cout << "!";
	for (auto i : a)
	{
		ans.push_back(i);
	}
//	//cout << "!";
	for (auto i : b)
	{
		ans.push_back(i);
	}
//	//cout << "!";
	Yuan[id] = ans;
	return ans;
}
pair<vector<int>,bool> solve(int id)
{
	//cout << "solve " << id << endl;
	vector<int> ans;
	ans.clear();
	if (sz[id] <= 1)
	{
		//cout << "solve " << id << " return false(leaf)\n";
		return {ans,0};
	}
	pair<vector<int>,bool> a__ = solve(ls[id]);
	vector<int> a_ = a__.first;
	//cout << "solve " << id << " a: ";
	//putv(a_);
	vector<int> a;
	a.clear();
	a.push_back(l[id]);
	for (auto i : a_)
	{
		a.push_back(i);
	}
	for (int i = r[id] - 1; i >= 1; i--)
	{
		a.push_back(0);
	}
	//cout << "solve " << id << " a(end): ";
	//putv(a);
	//putv(yuan(id));
	//cout << id << "                                                     " << bool(yuan(id) != a) << endl;
	if (yuan(id) != a)
	{
		a__.second = 1;
	}
	pair<vector<int>,bool> c_ = solve(rs[id]);
	vector<int> c = c_.first;
	vector<int> b_ = yuan(ls[id]);
	//cout << "solve " << id << " b: ";
	//putv(b_);
	//cout << "solve " << id << " c: ";
	//putv(c);
	vector<int> b;
	b.clear();
	b.push_back(l[id]);
	for (auto i : b_)
	{
		b.push_back(i);
	}
	for (auto i : c)
	{
		b.push_back(i);
	}
	//cout << "solve " << id << " b(end): ";
	//putv(b);
	//cout << id << " " << c_.second << " " << a__.second << endl;
	if (c_.second == 0 && a__.second == 0)
	{
		if (r[id] == 0)
		{
			//cout << "solve " << id << " return false\n";
			return {a,0};
		}
		ans.push_back(l[id] + 1);
		for (int i = 1; i < sz[id] - 1; i++)
		{
			ans.push_back(0);
		}
		//cout << "solve " << id << "l+1r-1 ";
		//putv(ans);
		return {ans,1};
	}
	//cout << "solve " << id << " a " << a__.second << " b " << c_.second << endl;
	//putv(a);
	//putv(b);
	if (c_.second == 0)
	{
		//cout << "solve " << id << " return a\n";
		return {a,1};
	}
	if (a__.second == 0)
	{
		//cout << "solve " << id << " return b\n";
		return {b,1};
	}
	for (int i = 0; i < min(a.size(),b.size()); i++)
	{
		if (a[i] > b[i])
		{
			//cout << "solve " << id << " return b\n";
			return {b,1};
		}
		if (a[i] < b[i])
		{
			//cout << "solve " << id << " return a\n";
			return {a,1};
		}
	}
	//cout << "solve " << id << "a == b\n";
	return {a,1};
}
signed main()
{
	cin >> n;
	cout << n << " ";
	dfsinput(1,n);
// 	for (int i = 1; i <= cnt; i++)
// 	{
// 		cout << i << ": " << sz[i] << " " << l[i] << " " << r[i] << " " << ls[i] << " " << rs[i] << endl;
// 	}
	pair<vector<int>,bool> ans = solve(1);
	if (ans.second == 0)
	{
	    cout << n + 1 << endl;
	    for (int i = 1; i <= n; i++)
	    {
	        cout << "0 ";
	    }
		return 0;
	}
	for (auto i : ans.first)
	{
		cout << i << " ";
	}
	return 0;
}
2023/8/20 18:38
加载中...