序列型动态规划

如codevs 1576 最长严格上升子序列 1044 拦截导弹

大致代码如下,随情况变化部分即可

#include 
#include 
#include 
#include 
using namespace std;

int max(int, int);


int blocknum(vector<int>& high,int step, int highh)
{
	if (step >= high.size())
		return 0;
	if (highh == 0)
	{
		return max(blocknum(high,  step + 1, 0), blocknum(high,  step + 1, high[step]) + 1);
	}
	if (high[step] >= highh)
	{
	
		return max(blocknum(high,  step + 1, highh), blocknum(high,  step + 1, high[step]) + 1);
	}
	else
	{
		return blocknum(high,  step + 1, highh);
	}	
}

int max(int a, int b)
{
	if (a > b)
		return a;
	else
		return b;
}


int main()
{
	int highnum,highh;
	cin >> highnum;
	vector<int> high;
	int i = 0;
	while (i<highnum)
	{
		cin >> highh;
		high.push_back(highh);
		i++;
	}
	cout<<blocknum(high,0,0)<<endl;
	system("pause");
	return 0;
}

你可能感兴趣的:(算法与数据结构)