寒假训练(动态规划)

**

HDU1069(DP动态规划)

**

A group of researchers are designing an experiment to test the IQ of a monkey. They will hang a banana at the roof of a building, and at the mean time, provide the monkey with some blocks. If the monkey is clever enough, it shall be able to reach the banana by placing one block on the top another to build a tower and climb up to get its favorite food.

The researchers have n types of blocks, and an unlimited supply of blocks of each type. Each type-i block was a rectangular solid with linear dimensions (xi, yi, zi). A block could be reoriented so that any two of its three dimensions determined the dimensions of the base and the other dimension was the height.

They want to make sure that the tallest tower possible by stacking blocks can reach the roof. The problem is that, in building a tower, one block could only be placed on top of another block as long as the two base dimensions of the upper block were both strictly smaller than the corresponding base dimensions of the lower block because there has to be some space for the monkey to step on. This meant, for example, that blocks oriented to have equal-sized bases couldn’t be stacked.

Your job is to write a program that determines the height of the tallest tower the monkey can build with a given set of blocks.
Input
The input file will contain one or more test cases. The first line of each test case contains an integer n,
representing the number of different blocks in the following data set. The maximum value for n is 30.
Each of the next n lines contains three integers representing the values xi, yi and zi.
Input is terminated by a value of zero (0) for n.
Output
For each test case, print one line containing the case number (they are numbered sequentially starting from 1) and the height of the tallest possible tower in the format “Case case: maximum height = height”.
Sample Input
1
10 20 30
2
6 8 10
5 5 5
7
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
7 7 7
5
31 41 59
26 53 58
97 93 23
84 62 64
33 83 27
0
Sample Output
Case 1: maximum height = 40
Case 2: maximum height = 21
Case 3: maximum height = 28
Case 4: maximum height = 342

问题简述:给出n种方块和这些方块的长宽高,求用这些方块能堆成的最大高度,其中上面方块的长宽都要小于下面方块的长宽。
问题分析:先考虑到一种方块的六种摆法,全部储存到结构数组中,然后对这个数组对长和宽从小到大预处理排序,最后一层一层往下dp。

#include <iostream>
#include<algorithm>
using namespace std;
struct node
{
	int x, y, h;

}b[181];
int dp[181];//dp[i]第i个方块时最高高度;
int cmp(node a, node b)
{
	if (a.x < b.x)return 1;
	if (a.x == b.x&&a.y < b.y)return 1;
	else return 0;
}
int main()
{
	int n,a1,a2,a3,num=0;
	while (cin >> n)
	{
		if (n == 0)break;
		int k = 0,ans=-1;
		for (int i = 0; i < n; i++)
		{
			cin >> a1 >> a2 >> a3;
			b[k].x = a1; b[k].y = a2; b[k].h = a3; k++;
			b[k].x = a1; b[k].y = a3; b[k].h = a2; k++;
			b[k].x = a2; b[k].y = a3; b[k].h = a1; k++;
			b[k].x = a2; b[k].y = a1; b[k].h = a3; k++;
			b[k].x = a3; b[k].y = a1; b[k].h = a2; k++;
			b[k].x = a3; b[k].y = a2; b[k].h = a1; k++;//六种情况
		}
		sort(b, b + k, cmp);//排序
		for (int i = 0; i < k; i++)
		{
			dp[i] = b[i].h;//初始化dp[];
		}
		for (int i = 0; i < k; i++)
		{
			for (int j = 0; j < i; j++)//第i个方块时更新dp的循环;
			{
				if(b[i].x>b[j].x&&b[i].y>b[j].y)//长宽条件限制
				dp[i] = max(dp[j] + b[i].h, dp[i]);
				
			}
			if (ans < dp[i])ans = dp[i];
		}num++;
		cout << "Case "<<num<<": maximum height = "<<ans << endl;
	}
}

HDU1081

Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1 x 1 or greater located within the whole array. The sum of a rectangle is the sum of all the elements in that rectangle. In this problem the sub-rectangle with the largest sum is referred to as the maximal sub-rectangle.

As an example, the maximal sub-rectangle of the array:

0 -2 -7 0
9 2 -6 2
-4 1 -4 1
-1 8 0 -2

is in the lower left corner:

9 2
-4 1
-1 8

and has a sum of 15.
Input
The input consists of an N x N array of integers. The input begins with a single positive integer N on a line by itself, indicating the size of the square two-dimensional array. This is followed by N 2 integers separated by whitespace (spaces and newlines). These are the N 2 integers of the array, presented in row-major order. That is, all numbers in the first row, left to right, then all numbers in the second row, left to right, etc. N may be as large as 100. The numbers in the array will be in the range [-127,127].
Output
Output the sum of the maximal sub-rectangle.
Sample Input
4
0 -2 -7 0 9 2 -6 2
-4 1 -4 1 -1
8 0 -2
Sample Output
15

问题简介:输入一个矩阵,输出最大子矩阵和。
问题分析:做一个预处理,用另一个矩阵储存每一列的和;例如新矩阵a[3][1]=ori[0][1]+ori[1][1]+ori[2][1]+ori[3][1];则子矩阵的和就是新矩阵对应的最下行减去对应的最上的上一行的差的和。问题就变成求这个差构成的数组的最大连续的子数列。

#include <iostream>
using namespace std;
int main()
{
	int n;
	while (cin >> n)
	{
		int m;
		int a[101][101] = { 0 };
		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j <= n; j++)
			{
				cin >> m; a[i][j] = m + a[i - 1][j];//构建新矩阵a
			}
		}
		int ans = 0, sum = 0;
		for (int i = 1; i <= n; i++)//矩阵上界
		{
			for (int j = i; j <= n; j++)//矩阵下界
			{
				for (int k = 1; k <= n; k++)//列右界
				{
					m = a[j][k] - a[i - 1][k]; sum += m;
					if (sum < 0)sum = 0;//列左界,负的就不加了。
					if (sum > ans)ans = sum;
				}
				sum = 0;
			}
		}
		cout << ans << endl;
	}
}

HDU1159

A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = <x1, x2, …, xm> another sequence Z = <z1, z2, …, zk> is a subsequence of X if there exists a strictly increasing sequence <i1, i2, …, ik> of indices of X such that for all j = 1,2,…,k, xij = zj. For example, Z = <a, b, f, c> is a subsequence of X = <a, b, c, f, b, c> with index sequence <1, 2, 4, 6>. Given two sequences X and Y the problem is to find the length of the maximum-length common subsequence of X and Y.
The program input is from a text file. Each data set in the file contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct. For each set of data the program prints on the standard output the length of the maximum-length common subsequence from the beginning of a separate line.
Input
abcfbc abfcab
programming contest
abcd mnp
Output
4
2
0
Sample Input
abcfbc abfcab
programming contest
abcd mnp
Sample Output
4
2
0

问题简介:输入两个字符串,输出最大公共子序列的长度。
问题分析:用两个字符数组储存两个字符串,a[i][j]为第一个字符串前i个字和第二个字符串前j个字最大公共子串的长度。

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int main()
{
	static int a[1001][1001] = {0};
	char s1[1000], s2[1000];
	while(cin >> s1 >> s2)
	{
		memset(a, 0, sizeof(a));
		for (int i = 1; i <= strlen(s1); i++)
		{
		for (int k = 1; k <= strlen(s2); k++)
			{
				if (s1[i - 1] == s2[k - 1])a[i][k] = a[i - 1][k - 1] + 1;
				else a[i][k] = max(a[i][k - 1], a[i - 1][k]);//不等继承,相等加1
			}
		}
		cout << a[strlen(s1)][strlen(s2)] << endl;
	}
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值