UVa Problem 111 - History Grading

本文介绍了解决UVaProblem111的算法,该问题要求通过求解最长上升子序列来解决历史评分问题。通过详细解释算法过程,包括如何将问题转化为最长上升子序列问题,以及使用C++实现的代码示例,旨在帮助读者理解并解决类似问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// UVa Problem 111 - History Grading
// Verdict: Accepted
// Submission Date: 2011-11-25
// UVa Run Time: 0.052s
//
// 版权所有(C)2011,邱秋。metaphysis # yeah dot net
//
// [解题方法]
// 可以将问题转化为求最长上升子序列的问题。

#include <iostream>
#include <sstream>

using namespace std;

#define MAXN 25

int order[MAXN], events[MAXN], length[MAXN], n;

int getScores(void)
{
	for (int i = 1; i <= n; i++)
		length[i] = 1;

	for (int i = 1; i <= n; i++)
		for (int j = 1; j < i; j++)
			if (events[j] < events[i] && (length[j] + 1) > length[i])
				length[i] = length[j] + 1;
	int maxLength = 1;
	for (int i = 1; i <= n; i++)
		maxLength = max(maxLength, length[i]);

	return maxLength;
}

int getIndex(int index)
{
	for (int i = 1; i <= n; i++)
		if (order[i] == index)
			return i;
}

int main (int argc, char const* argv[])
{
	string line;
	int index;

	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		cin >> index;
		order[index] = i;
	}

	cin.ignore();

	while (getline(cin, line))
	{
		istringstream iss(line);

		for (int i = 1; i <= n; i++)
		{
			iss >> index;
			events[index] = getIndex(i);
		}
		
		cout << getScores() << endl;
	}

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值