Uva 10131-Is Bigger Smarter?(DP)

题目链接:点击打开链接

DAG(有向无环图)上的最长路+打印路径

建图很简单,对于两点 a b, 能够由a到b的条件是w[a]<w[b]&&s[a]>s[b] 注意是有向图。

设dp[i] 为以i为起点的最长路的长度,dp[i]= max(dp[i],dp[j]+1)  枚举j (j是和i相连的点)

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cctype>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define maxn 1010
#define _ll __int64
#define ll long long
#define INF 0x3f3f3f3f
#define Mod 1000000007
#define pp pair<int,int>
#define ull unsigned long long
using namespace std;
int p,w[maxn],s[maxn],dp[maxn];
bool ma[maxn][maxn];
void build()
{
	memset(ma,0,sizeof(ma));
	for(int i=1;i<p;i++)
	{
		for(int j=i+1;j<p;j++)
		{
			if(w[i]<w[j]&&s[i]>s[j])
				ma[i][j]=1;
			else if(w[j]<w[i]&&s[j]>s[i])
				ma[j][i]=1;
		}
	}
}
int dfs(int x)
{
	int& ans=dp[x];
	if(ans>0) return ans;
	ans=1;
	for(int i=1;i<p;i++)
		if(ma[x][i])ans=max(ans,dfs(i)+1);
	return ans;
}
void output(int x)
{

	printf("%d\n",x);
	for(int i=1;i<p;i++)
		if(ma[x][i]&&dp[x]==dp[i]+1)
		{
			output(i);
			break;
	    }
}
int main()
{
	p=1;
	while(scanf("%d %d",w+p,s+p)!=EOF)++p;
	build();
	memset(dp,-1,sizeof(dp));
	int ans=-INF,indx;
	for(int i=1;i<p;i++)
		if(ans<dfs(i))
	    {
			ans=dp[i];
			indx=i;
	    }
	printf("%d\n",ans);
	output(indx);
    return 0;
}
时间: 2024-08-05 00:49:34

Uva 10131-Is Bigger Smarter?(DP)的相关文章

UVA 10131 Is Bigger Smarter?(DP最长上升子序列)

Description Question 1: Is Bigger Smarter? The Problem Some people think that the bigger an elephant is, the smarter it is. To disprove this, you want to take the data on a collection of elephants and put as large a subset of this data as possible in

uva 10131 Is Bigger Smarter? (DAG)

uva 10131 Is Bigger Smarter? 题目大意:当一只大象的体重大于另一只的体重,且智商小于另一只的智商,该大象便可以"嵌套"另一只大象.问,最长的嵌套方式.(答案不唯一) 解题思路:DAG. #include<stdio.h> #include<string.h> #include<stdlib.h> #include<algorithm> using namespace std; struct ELE{ int w

UVa 10131 Is Bigger Smarter? (LDS+数据结构排序)

Is Bigger Smarter? Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Question 1: Is Bigger Smarter? The Problem Some people think that the bigger an elephant is, the smarter it is. To disprove this, y

UVA 10131 Is Bigger Smarter? 【严格单调递增子序列】

题目:UVA 10131 Is Bigger Smarter 题意:给出大象的身高和体重,求身高递增且体重递减的最长序列,都是严格的,并打印序列. 分析:就是先对身高按自增排序,然后求一个单调递减子序列,严格单调的,所以加一句判断,然后打印序列,用一个数组保存就好了 开始想的是先预处理掉重复的,提交wa了,这样不行,因为你不知道体重是最高的还是最低的,可能开始留高的好,后面低的比较好.所以..... AC代码: #include<iostream> #include<cstdio>

uva 10635 Prince and Princess(DP)

uva 10635 Prince and Princess(DP) In an n x n chessboard, Prince and Princess plays a game. The squares in the chessboard are numbered 1, 2, 3 ... n*n, as shown below: Prince stands in square 1, make p jumps and finally reach square n*n. He enters a

Uva 10131 Is Bigger Smarter? (LIS,打印路径)

option=com_onlinejudge&Itemid=8&page=show_problem&problem=1072">链接:UVa 10131 题意:给定若干大象的体重及智商值.求满足大象体重严格递增,智商严格递减的序列的最大个数. 并打印随意一组取得最大值的序列的大象编号 分析:这个是LIS的应用,仅仅只是推断条件有两个,能够先对大象的体重排序,可是要打印路径. 那就必须得回溯求路径.能够直接逆序循环求,当然递归也是一个好的选择 #include<

UVA 10564-Paths through the Hourglass(DP)

题目大意:给出一个沙漏,包含一个倒三角和一个三角,每个方格有一个0到9的数字,从顶上某格走到底下某格,求得到一个特殊值的路径有多少条,并输出字典序最小的.路径用一个起点和一系列'L'和'R'的字符表示. 用a[i][j]表示第i行第j列的数字.由于要求字典序最小的,所以从下往上递推.用d[i][j][u]表示在(i,j)且和为u的路径有多少条,在上半部分是倒三角,可以由d[i+1][j][u-a[i][j]]和d[i+1][j+1][u-a[i][j]]相加递推而来,在下半部分是正三角,可以由d

UVA 10131 Is Bigger Smarter?(DP)

Some people think that the bigger an elephant is, the smarter it is. To disprove this, you want to take the data on a collection of elephants and put as large a subset of this data as possible into a sequence so that the weights are increasing, but t

uva 10313 Pay the Price (DP)

uva 10313 Pay the Price 题目大意:现在有300种面额的硬币(1~300),给出一个金额数,问这300种面额的硬币组成该金额数的方式有多少种.注意:该题的输入有三种模式,1个数n:n为金额数:2个数n, a:n为金额数,a为硬币个数上限:3个数n, a,b:n为金额数,a b为硬币个数的下限和上限. 解题思路:dp[i][j]表示面额i的金额,在硬币数不超过j的情况下,有几种组成方式.这个题目涉及到一个结论,用不超过i个硬币凑出面值j的方案种数,是和用面值不超过i的硬币凑出