重复T次的LIS的dp Codeforces Round #323 (Div. 2) D
http://codeforces.com/contest/583/problem/D
原题: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.
题目大意:有长度为n的数组a(n <= 100),其中a[i] <= 300,这个a数组可以重复T次,问他的最长上升子序列是多少?
思路:我们可以发现,这个数组如果要全部都算上的,那么在t<=n的情况下,他的最长上升子序列一定会遍历一次a数组。所以我们就只需要把原来的数组扩大n倍,然后求他的LIS。
这样以后我们发现,后面的重复的次数一定是原来数组里面出现次数(假定重复次数k为最多)最多的数值,所以ans = Lis的长度 + k * T - min(n, T);
复杂度 O(n*n*logn)
//看看会不会爆int!数组会不会少了一维!
//取物问题一定要小心先手胜利的条件
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker,"/STACK:102400000,102400000")
#define LL long long
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define haha printf("haha\n")
const int maxn = + ;
int a[maxn * maxn];
int n, T;
vector<int> ve; int solve(int t){
int len = t * n;
for (int i = ; i < t; i++){
for (int j = ; j <= n; j++){
a[n * i + j] = a[j];
}
}
for (int i = ; i <= n * t; i++){
int pos = upper_bound(ve.begin(), ve.end(), a[i]) - ve.begin();
if (pos == ve.size()) ve.push_back(a[i]);
else ve[pos] = a[i];
}
return ve.size();
} int cnt[maxn * maxn];
int main(){
cin >> n >> T;
int maxval = , k = ;
for (int i = ; i <= n; i++){
scanf("%d", a + i);
cnt[a[i]]++;
k = max(cnt[a[i]], k);
}
int ans = solve(min(n, T));
//printf("ans = %d k = %d T - min(n, T) = %d\n", ans, k, T - min(n, T));
printf("%d\n", ans + k * (T - min(n, T)));
return ;
}
重复T次的LIS的dp Codeforces Round #323 (Div. 2) D的更多相关文章
- DP Codeforces Round #303 (Div. 2) C. Woodcutters
题目传送门 /* 题意:每棵树给出坐标和高度,可以往左右倒,也可以不倒 问最多能砍到多少棵树 DP:dp[i][0/1/2] 表示到了第i棵树时,它倒左或右或不动能倒多少棵树 分情况讨论,若符合就取最 ...
- DP Codeforces Round #260 (Div. 1) A. Boredom
题目传送门 /* 题意:选择a[k]然后a[k]-1和a[k]+1的全部删除,得到点数a[k],问最大点数 DP:状态转移方程:dp[i] = max (dp[i-1], dp[i-2] + (ll) ...
- 数学+DP Codeforces Round #304 (Div. 2) D. Soldier and Number Game
题目传送门 /* 题意:这题就是求b+1到a的因子个数和. 数学+DP:a[i]保存i的最小因子,dp[i] = dp[i/a[i]] +1;再来一个前缀和 */ /***************** ...
- 树形DP Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland
题目传送门 /* 题意:求一个点为根节点,使得到其他所有点的距离最短,是有向边,反向的距离+1 树形DP:首先假设1为根节点,自下而上计算dp[1](根节点到其他点的距离),然后再从1开始,自上而下计 ...
- DP Codeforces Round #FF (Div. 1) A. DZY Loves Sequences
题目传送门 /* DP:先用l,r数组记录前缀后缀上升长度,最大值会在三种情况中产生: 1. a[i-1] + 1 < a[i+1],可以改a[i],那么值为l[i-1] + r[i+1] + ...
- 递推DP Codeforces Round #260 (Div. 1) A. Boredom
题目传送门 /* DP:从1到最大值,dp[i][1/0] 选或不选,递推更新最大值 */ #include <cstdio> #include <algorithm> #in ...
- Codeforces Round #323 (Div. 2) Once Again... CodeForces - 582B 最长非下降子序列【dp】(不明白)
B. Once Again... time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- dp - Codeforces Round #313 (Div. 1) C. Gerald and Giant Chess
Gerald and Giant Chess Problem's Link: http://codeforces.com/contest/559/problem/C Mean: 一个n*m的网格,让你 ...
- 拓扑序+dp Codeforces Round #374 (Div. 2) C
http://codeforces.com/contest/721/problem/C 题目大意:给你有向路,每条路都有一个权值t,你从1走到n,最多花费不能超过T,问在T时间内最多能访问多少城市? ...
随机推荐
- auDemo
Option Explicit Private Const MODULE_NAME = "auDemo.WSC" Dim oEccomOperation,dbDemo Set oE ...
- SpringMVC同时使用<mvc:resources … />和日期转换Formatter时出现问题的解决方法
很久没更新博文了,不是没有学习,而是很多东西记在OneNote里面,收获很多啊,因为各种杂事,所以对于博客很久没更新了. 个人觉得:博客比起OneNote或者为知笔记之类的云笔不同在于博客应该记载的是 ...
- markdown 自定义一个锚点
//自定义锚点 s "m[": function mlink( text ) { var orig = String(text); // Inline content is pos ...
- Redis性能问题排查解决手册
转自:http://www.cnblogs.com/mushroom/p/4738170.html 阅读目录: 性能相关的数据指标 内存使用率used_memory 命令处理总数total_comma ...
- 制作jar包
1.打开cmd 2.通过cd切换到要打包的工程所在的bin目录(一定是bin目录) 运行jar -cvf aa.jar *.* jar是打包的命令 -cvf可以自行查看一下文档解释(jar -help ...
- Mybatis一对多查询得不到多方结果
一对多查询:一个年级对应多个学生,现在要查询年级(带学生)信息. 查询结果: [main] INFO com.java1234.service.GradeTest - 查询年级(带学生)[main] ...
- drupal7 开发自定义上传、下载模块的上传功能
关键点有两个:1.在页面上显示出上传的控件,2.代码实现文件上传到服务器的功能 一.显示控件: 先来看关键点1: 实现页面显示出上传控件, 关键代码: $form['my_file_field'] = ...
- fork()子进程与waitpid()
#!/usr/bin/perl use warnings; use strict; use POSIX ":sys_wait_h"; $SIG{CHLD} = sub{ my $p ...
- 实战JAVA虚拟机 JVM故障诊断与性能优化 pdf
需要的小伙伴拿走,百度云盘:http://pan.baidu.com/s/1nvm6RHZ
- dao代码模板
提供数据源以及回收资源的工具类DbUtils: public class DbUtils { private static ComboPooledDataSource dataSource = new ...