Codeforces Round #323
2015-10-06 16:13
232 查看
B. Once Again…
题面
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: n, T (1 ≤ n ≤ 100, 1 ≤ 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.
题意
T组数字,每组n个数字,满足a[i] = a[i-n] ,求LIS解法
注意到n的范围是100LIS的转移方程是 dp[i] = max(dp[j]+1) (0 < j < i && A[i] >= A[j])
即第i个dp值 由 前 i - 1 个dp值转移过来,考虑序列具有周期性,有以下结论:
dp值只由前n个值转移而来
证明:
假设存在 j ( 0 < j < i - n )使得任意k (i - n <= k < n) 有 dp[j] > dp[k] && A[i] >= A[j] && A[i] >= A[k]
令 k = j + n
那么 dp[k] 一定可以由 dp[j] 转移而变成 dp[k] + 1, 矛盾。
得证。
这样复杂度由 O(n^2*T^2) 变为 O(n^2*T)
T是1e7,还是比较虚。
在N < T的情况下, 最多转移 N 次即可, 剩下的只需要找到序列里面出现次数那个数,出现次数*(T-N) + 前面的结果即可
因为LIS最多上升N次,不上升的情况当然是贪心的选择上升次数最多的那个数即可。
#include<bits/stdc++.h> using namespace std; typedef long long ll; int N, T; int A[105]; int cnt[305]; ll res = 0; int maxcnt = 0; ll dp[105]; int main(){ scanf("%d %d",&N, &T); for(int i = 1; i <= N; i++){ scanf("%d",&A[i]); cnt[A[i]]++; if(cnt[A[i]] > maxcnt) maxcnt = cnt[A[i]]; } for(int i = 1; i <= N && i <= T; i++){ for(int p = 1; p <= N; p++){ dp[p]++; for(int j = 1; j <= N; j++){ if(j != p && A[p] >= A[j]) dp[p] = max(dp[p], dp[j] + 1); } } //for(int p = 1; p <= N; p++) printf("dp=%d\n",dp[p]); } for(int i = 1; i <= N; i++) if(dp[i] > res) res = dp[i]; ll tt = T - N; //printf("%I64d\n",res); if(tt > 0){ res += tt * maxcnt; } printf("%I64d\n",res); return 0; }
相关文章推荐
- 登山
- 关于ActivityA中的FragmentA 启动 ActivityB,FragmentA的startActivityForResult回调的问题
- Jsoup进阶之获取指定数据
- web项目中使用log4net
- 关于jsp利用EL和struts2标签来遍历ValueStack的东东 ------> List<Map<K,V>> 以及 Map<K,<List<xxx>>> 的结构遍历
- Android 内部启动其他应用,以及打开指定qq聊天界面
- Android读书笔记-------IPC机制
- 剑指offer:数字在排序数组中出现的次数
- Git详细教程
- 数据库范式那些事(清晰)
- [NOIP模拟2015.10.06]C
- css样式表的引入方式
- 【bzoj1978】 BEIJING2010 取数游戏 game dp优化
- CoreAnimation 核心动画二 锚点
- C语言文件输出,把内容输入到新建文件中
- 增删改查数据库用的组件
- (转)数据库范式那些事(清晰)
- UEFI工程编译命令Build用法
- [LeetCode-172] Factorial Trailing Zeroes(n 阶乘后面有几个0)
- 20151006模拟赛总结