#Codeforces 323 [div2] D. Once Again 【优化dp】

本文探讨了在大规模重复的数列中寻找最长非递减子序列的有效算法。通过优化动态规划方法,针对循环周期大的情况提出了高效解决方案。

题目

Once Again

PROBLEM

You are given an array of positive integers a1, a2, ..., an × T of length n × T. We know that for any i > n it is true that ai = ai - n. Find the length of the longest non-decreasing sequence of the given array.

Input

The first line contains two space-separated integers: nT (1 ≤ n ≤ 1001 ≤ T ≤ 107). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 300).

Output

Print a single number — the length of a sought sequence.

Sample test(s)
input
4 3
3 1 4 2
output
5
Note

The array given in the sample looks like that: 3, 1, 4, 23, 1, 4, 2, 3, 1, 4, 2. The elements in bold form the largest non-decreasing subsequence.



题目意为,100个随机数循环 T 次形成一个数列,求该数列的最长不递减子数列。

循环节T数量级较大,不可能直接DP遍历所有的数字。对于每一节,在DP时只需要对本节自己之前以及上一节所有的数字比较(每个循环节至少会增加一个数字)。若T小于100,那么在T个循环内必然会出现结果,直接DP即可。在T较大时,只有在每个循环节增加数字大于1时才会出现选择的情况。在T等于100时必然会成为稳定状态,之后为将数字最大化,把循环中次数最多的数字插入在剩余的循环节内。


#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	long long int dp[105][105];
	static int data2[305];
	int data[105];
	static int maxn,n,t,mmx;
	static long long int ans;
	cin >> n >> t;
	mmx = min(n*n, n*t);

	for (size_t i = 0; i < n; i++)
	{
		cin >> data[i];
		data2[data[i]]++;
	}
	for (size_t i = 0; i < 305; i++)
	{
		maxn = max(maxn, data2[i]);
	}
	for (size_t i = 0; i <mmx/n; i++)
	{
		for (size_t s = 0; s < n; s++)
		{
			for (size_t q = 0; q < n; q++)
			{
				if (i==0)
				{
					dp[i][s] = 1;
					break;
				}
				if (data[s]>=data[q])
				{
					dp[i][s] = max(dp[i][s], dp[i - 1][q]+1);
				}
			}
			for (size_t q = 0; q < s; q++)
			{
				if (data[s] >= data[q])
				{
					dp[i][s] = max(dp[i][s], dp[i][q]+1);
				}
			}
		}
		
	}

	for (size_t i = 0; i < n; i++)
	{
		ans = max(ans, dp[mmx / n - 1][i]);
	}


	cout << ans + maxn*(t - mmx / n);
	return 0;
}


对于Codeforces Round 1005 Div. 2中的D题解答或解释,当前提供的引用资料并未直接涉及该轮次的比赛题目详情。然而,可以基于过往比赛的经验以及相似类型的编程竞赛问题提供一般性的指导。 ### 解决方案概述 通常情况下,在解决此类算法竞赛题目时,会遵循特定的方法论来处理输入数据并计算所需的结果。虽然具体到Codeforces Round 1005 Div. 2 Problem D的信息未被提及,但可以根据以往经验推测可能涉及到的数据结构和算法技术: - **读取测试案例数量**:程序首先应该能够接收多个独立的测试案例数目\(t\),其中每一个案例都包含了不同的参数集[^3]。 - **解析数组元素**:针对每个测试案例,需解析给定长度为\(n\)的一系列整数\[a_1, a_2,...,a_n\]作为操作对象[^2]。 - **查询次数限制**:需要注意的是,所有测试案例中查询总数不得超过设定的最大值,比如这里提到不超过\(2 \times 10^5\)次查询[^1]。 - **输出格式规定**:当准备打印最终答案时,应按照指定格式输出结果,并继续处理下一个测试案例直到完成全部测试[^4]。 考虑到这些通用原则,如果要设计一个适用于此类型问题的解决方案框架,则可能会如下所示: ```python def solve_problem(): import sys input = sys.stdin.read data = input().split() index = 0 results = [] t = int(data[index]) index += 1 for _ in range(t): n = int(data[index]) index += 1 numbers = list(map(int, data[index:index+n])) index += n # 假设这里是解决问题的核心逻辑部分, # 需要根据具体的Problem D描述调整这部分代码实现。 result_for_case = "Example Result" results.append(result_for_case) print("\n".join(results)) ``` 上述伪代码展示了如何构建基本架构用于批量处理多组测试用例,但是核心业务逻辑需要依据实际问题定义进行填充和完善。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值