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

动态规划,最长下降子序列问题

Posted by ACMore_txj at 2014-04-21 22:42:52 on Problem 1065
/*先对第一关键字升序排序后再对第二关键字求最长下降子序列*/

#include<cstdio>
#include<iostream>
#include<algorithm>

using namespace std;

#define MAXN 5005

struct node
{
	int len;
	int wei;
}per[MAXN] = {0};

int dp[MAXN];

bool cmp(node a,node b)
{
	return (a.len <= b.len);
}

int main()
{
	int T,i,j,n;
	cin>>T;
	while(T)
	{
		cin>>n;
		for(i = 1;i <= n;i ++)
			scanf("%d %d",&per[i].len,&per[i].wei);
		sort(per+1,per+n+1,cmp);
		for(i = 1;i <= n;i ++)
		{
			dp[i] = 1;
			for(j = 1;j < i;j ++)
				if(per[j].wei > per[i].wei && dp[j] + 1 > dp[i])
					dp[i] = dp[j] + 1;
		}
		int Max = 0;
		for(i = 1;i <= n;i ++)
			Max = max(Max,dp[i]);
		cout<<Max<<endl;
		T --;
	}
	return 0;
}

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