求助!!!最后两个点TLE!!!
查看原帖
求助!!!最后两个点TLE!!!
1215487
uuuuuuu000楼主2024/8/14 23:36
#include<iostream>
#include<vector>

using namespace std;

int c = 0;


void build(vector<int> a,int sta, int end)
{
	if (sta == end)
	{
		c += a[end];
		return;
	}
	int Min = 10000;
	int t = 0;
	for (int i = sta; i <= end; i++)
	{
		if (Min > a[i])
		{
			Min = a[i];
			t = i;
		}
	}
	c += a[t];
	for (int i = sta; i <= end; i++)
	{
		a[i] -= Min;
	}
	if (sta != t)
	{
		build(a, sta, t - 1);
	}
	if(t != end)
	{
		build(a, t + 1, end);
	}
	return;
}

int main()
{
	int n;
	while (cin >> n)
	{
		vector<int> a;
		int x;
		for (int i = 0; i < n; i++)
		{
			cin >> x;
			a.push_back(x);
		}
		build(a, 0, n - 1);
		cout << c << endl;
		c = 0;
	}
	return 0;
}
2024/8/14 23:36
加载中...