Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

用了个dfs……LTE……请路过的建议一个好的剪枝

Posted by tankadozmy at 2010-01-31 15:44:54 on Problem 2533
#include<iostream>
#include<stack>
using namespace std;
void dfs(int);
int n;
int* a;
stack<int> s;
int count=0;
int best_c=0;
int main()
{
	cin>>n;
	int i;
	a=new int[n];
	for(i=0;i<n;i++)
	{
		cin>>a[i];
	}
	dfs(0);
	cout<<best_c<<endl;
	delete [] a;
	return 0;
}

void dfs(int i)
{
	if(i==n)
	{
		if(s.size()>best_c) best_c=s.size();
		return;
	}
	if(s.empty()||a[i]>s.top())
	{
		s.push(a[i]);
		dfs(i+1);
		s.pop();
	}
	if(s.size()+n-i-1>best_c) dfs(i+1);
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator