Codeforces 582B Once Again... 【LIS变形】
2016-04-27 13:58
363 查看
题目链接:Codeforces 582B Once Again…
B. Once Again…
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.
Examples
input
4 3
3 1 4 2
output
5
Note
The array given in the sample looks like that: 3, 1, 4, 2, 3, 1, 4, 2, 3, 1, 4, 2. The elements in bold form the largest non-decreasing
题意:给定n个元素的序列,现在将它重复T次。问你可以得到的LIS。
思路:当n个元素各不相同时,在有n+1段的情况下会导致有一段的贡献必定为1。这样的话就好办了,我们取前n段为一个序列A[],后n段为一个序列B[]。
dp1[i]记录以A[i]结尾的LIS,dp2[i]记录以B[i]开头的LIS。我们暴力起点i、终点j,中间序列取合法的A[k](A[i] <= A[k] <= A[j])且A[k]数量最多。扫描一遍就好了。
AC代码:
B. Once Again…
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.
Examples
input
4 3
3 1 4 2
output
5
Note
The array given in the sample looks like that: 3, 1, 4, 2, 3, 1, 4, 2, 3, 1, 4, 2. The elements in bold form the largest non-decreasing
题意:给定n个元素的序列,现在将它重复T次。问你可以得到的LIS。
思路:当n个元素各不相同时,在有n+1段的情况下会导致有一段的贡献必定为1。这样的话就好办了,我们取前n段为一个序列A[],后n段为一个序列B[]。
dp1[i]记录以A[i]结尾的LIS,dp2[i]记录以B[i]开头的LIS。我们暴力起点i、终点j,中间序列取合法的A[k](A[i] <= A[k] <= A[j])且A[k]数量最多。扫描一遍就好了。
AC代码:
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <map> #include <set> #include <string> #include <queue> #define CLR(a, b) memset(a, (b), sizeof(a)) using namespace std; typedef long long LL; typedef pair<int, int> pii; const int MAXN = 2*1e4 + 10; const int MOD = 1e9 + 7; const int INF = 0x3f3f3f3f; int g[MAXN], a[MAXN]; int dp1[MAXN], dp2[MAXN]; int cnt[400]; int main() { int n, T; while(scanf("%d%d", &n, &T) != EOF) { for(int i = 1; i <= n; i++) { scanf("%d", &a[i]); g[i] = INF; } int ans = 0; if(T <= 2*n) { for(int i = n+1; i <= T * n; i++) { a[i] = a[i % n == 0 ? n : i % n]; g[i] = INF; } for(int i = 1; i <= T * n; i++) { int k = upper_bound(g+1, g+T*n+1, a[i]) - g; dp1[i] = k; g[k] = min(g[k], a[i]); ans = max(ans, dp1[i]); } printf("%d\n", ans); continue; } for(int i = n+1; i <= n * n; i++) { a[i] = a[i % n == 0 ? n : i % n]; g[i] = INF; } for(int i = 1; i <= n * n; i++) { int k = upper_bound(g+1, g+n*n+1, a[i]) - g; dp1[i] = k; g[k] = min(g[k], a[i]); } CLR(g, INF); for(int i = n * n; i >= 1; i--) { int k = upper_bound(g+1, g+n*n+1, -a[i]) - g; dp2[i] = k; g[k] = min(g[k], -a[i]); } for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { if(a[i] <= a[j]) { int Max = 0; CLR(cnt, 0); for(int k = 1; k <= n; k++) { if(a[k] >= a[i] && a[k] <= a[j]) { cnt[a[k]]++; } Max = max(Max, cnt[a[k]]); } ans = max(ans, dp1[(n - 1) * n + i] + dp2[j] + Max * (T - 2*n)); } } } printf("%d\n", ans); } return 0; }
相关文章推荐
- 【翻译】Xibo官方文档6-CMS Maintenance
- 朴素贝叶斯 垃圾邮件检测 Naive Bayes Spam detection
- CSAcademy Beta Round #4 Swap Pairing
- 山东省第五届ACM省赛题——Painting Cottages(求点集的划分)
- CGRectContainsPoint的具体用法解释
- 理解 %IOWAIT (%WIO)
- TIME_WAIT和CLOSE_WAIT状态区别
- You have new mail in /var/spool/mail/root
- leetcode 11. Container With Most Water
- fn contains 包含字符串 三种方法判断
- Troubleshooting 'library cache: mutex X' waits. (文档 ID 1357946.1)
- 习近平主席关心人工智能事业的发展
- WAITEVENT: "library cache: mutex X" (文档 ID 727400.1)
- RAID的介绍和RAID的安装
- HDU 1302 The Snail(模拟题)
- 最新型的TurboMail智慧邮件服务器平台
- 先搜集下关于行动列表和行为树的AI制作
- 10252_Common_permutaion
- 关于服务器raid的一个记录
- 关于服务器raid的一个记录