UVa 12589 Learning Vector

本文探讨了在给定向量集合的情况下,如何通过特定排序策略最大化形成平面图形的面积。详细介绍了算法实现过程,包括状态转移方程的定义、记录数组的使用以及递归求解方法。

题目链接:

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4034


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

// record[i][j][k]代表当前形成高度为k, 从第i,i+1,...,N个向量中选j个,形成的最大面积
double record[55][55][2505];

// flag[i][j][k] = g_count 代表record[i][j][k]被计算过
int flag[55][55][2505];


// 向量结构体
typedef struct node
{
	double x, y;
	double k;	// 向量斜率
	bool operator < (const struct node& a) const
	{
		return k > a.k;
	}
}node;


node array[55];

int N, K;

int inf = (1<<30);

int g_count;


double get_max(int a, int b, int h);

int main()
{
	int t;
	scanf("%d", &t);
	memset(flag, 0, sizeof(flag));	
	g_count = 1;
	while(g_count <= t)
	{
		scanf("%d %d", &N, &K);
		for(int i = 1; i <= N; i++)	
		{
			scanf("%lf %lf", &array[i].x, &array[i].y);
			if(array[i].x == 0)
				array[i].k = inf;
			else
				array[i].k = array[i].y / array[i].x;
		}
		// 按斜率从大到小排序
		sort(array+1, array+1+N);

		// 计算结果
		printf("Case %d: %.0f\n", g_count, get_max(1, K, 0));
		g_count++;			
	}		
	return 0;		
}

// 计算record[a][b][h]代表当前形成高度为h, 从第a,a+1,...,N个向量中选b个,形成的最大面积结果
double get_max(int a, int b, int h)
{
	if(flag[a][b][h] == g_count)
		return record[a][b][h];

	flag[a][b][h] = g_count;
	
	if(a == N+1 || b == 0)
	{
		record[a][b][h] = 0;
		return record[a][b][h];
	}		

	// 分选第a个和不选第a个两种情况
	double area = h*array[a].x*2 + array[a].x*array[a].y;
	double r1 =  area + get_max(a+1, b-1, h+array[a].y);
	double r2 = get_max(a+1, b, h);

	record[a][b][h] = max(r1, r2);
	return record[a][b][h]; 		
}


这道题没想出来。主要是没有想明白给出若干向量,如何排列使得面积最大。

应该是按照斜率从大到小排序得到的面积最大。(如何证明还未想好,以后想好)

所以选择向量前按斜率从大到小排序,因为这样挑选以后面积最大。

用状态d(i,j)表示从第i, ..., n个向量中选择j个向量使得面积最大是不够的,还应该考虑前面已经生成的高度才对。

所以状态为d(i,j,k)表示从第i,...,n个向量中选择j个向量,在现有高度为h情况下的最大面积。

### UVA编程竞赛中的向量使用 在UVA编程竞赛中,`std::vector` 是一种非常常用的数据结构,用于动态数组管理。这种容器能够自动调整大小并提供方便的操作接口。 对于输入文件流设置部分[^1]: ```cpp // Set up the input and answer streams. std::ifstream in(argv[1]); std::ifstream ans(argv[2]); ``` 这段代码展示了如何读取命令行参数来打开不同的输入文件。然而,在实际比赛中处理数据时,经常需要用到 `std::vector` 来存储和操作这些来自文件的数据。 下面是一个简单的例子展示如何利用 `std::vector` 解决典型问题: 假设有一个题目要求计算一系列整数的最大子序列和。可以这样实现: ```cpp #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int n; cin >> n; // 输入元素数量 vector<int> nums(n); for (auto& num : nums) { cin >> num; // 读入n个整数值到vector中 } int max_sum = *max_element(nums.begin(), nums.end()); // 初始化最大值为最小可能值 int current_sum = 0; for (const auto &num : nums) { current_sum += num; if (current_sum > max_sum) { max_sum = current_sum; } if (current_sum < 0) { current_sum = 0; } } cout << "Maximum subarray sum is " << max_sum << endl; } ``` 此程序首先定义了一个名为 `nums` 的 `std::vector<int>` 类型变量用来保存从标准输入获取的一系列整数。接着通过遍历这个向量找到最大的连续子序列之和,并输出结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值