【17.07%】【codeforces 583D】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 subsequence.
【题目链接】:http://codeforces.com/contest/583/problem/D
【题解】
每次都选出现最多个数的数?
3 4 1 2
难道你要随便选一个数?
显然
12 3 4 4 4 4 4…
更优
其中1、2是在a[1..n]中选的,3、4是在a[n+1..2n]中选的;
当然你也可以这样选
1 2 2 2 2 ….. 2 2 3 4
或
1 2 3 3 3 3 3 3 …. 3 3 3 4
所以不能直接选出现次数最多的数字;
还得用一些“聪明的技巧”
聪明的技巧当然就是DP了;
可以考虑
5 6 3 4 1 2
可以看到;
我们第一次会选 12
第二次会选 3 4
第三次会选 5 6
所以最少把原序列复制6/2==3次才行;
在这n*(n/2)个数字里面进行DP;
那
6 5 4 3 2 1咋办?
这种情况我们完全不用想;
直接随便取一个数字就行了;
然后每次都选那个数字;
又例如
5 6 4 3 2 1
则第一次选5 6 其余的都选6就好:
然后复制6/2==3次完全够了
则进行DP的时候会选出
5 6 6 6
剩余的全部填6;
又例如
5 5 6 4 3 2 1
进行6/2==3次DP
会选出
5 5 5 5 5 5 6
其他的每次都选两次5选在最前面就好;
即我们选择的出现次数最多的数字是肯定有地方放的;所以不用担心;
总结一下就是
把n个数字复制n/2次;然后对这n*(n/2)个数字进行DP;求出这n*(n/2)个数字的最长不下降子序列;
(求n*(n/2)个数字的最长不下降子序列的时候只要往前找n个位置更新f[i]就可以了,具体的自己想,很简单的);
然后在初始的n个数字中选择出现次数最多的数字的次数key;
答案就是DP算出来的最长+(t-(n/2))*key
如果n==1要特判下,不然会搞笑.
【完整代码】
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5010;
int n,t,a[MAXN],lim,f[MAXN],ans = 0,mf = 0,b[350] = {0};
int main()
{
//freopen("F:\\rush.txt","r",stdin);
scanf("%d%d",&n,&t);
for (int i = 1;i <= n;i++)
{
scanf("%d",&a[i]);
b[a[i]]++;
mf = max(mf,b[a[i]]);
}
lim = n*min(n/2,t);
if (n==1)
lim = n;
for (int i = n+1;i <= lim;i++)
a[i] = a[i-n];
for (int i = 1;i <= lim;i++)
{
f[i] = 1;
for (int j = 1;j <= i-1;j++)//faster algorithm ->for (int j = max(1,i-n);j<=i-1;j++)
if (a[j] <= a[i])
f[i] = max(f[i],f[j]+1);
ans = max(ans,f[i]);
}
if (n==1)
n++;
if (t <= (n/2))
printf("%d\n",ans);
else
printf("%d\n",ans+(t-(n/2))*mf);
return 0;
}
【17.07%】【codeforces 583D】Once Again...的更多相关文章
- 【黑金教程笔记之008】【建模篇】【Lab 07 数码管电路驱动】—笔记
实验七的目的是设计实现最大为99数字在2个数码管上.采用同步动态扫描.即行信号和列信号同步扫描.这里数码管是共阳极的.选择端口也是共阳极的. 模块: /************************ ...
- 【 BowWow and the Timetable CodeForces - 1204A 】【思维】
题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...
- 【24.17%】【codeforces 721D】Maxim and Array
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【15.07%】【codeforces 625A】Guest From the Past
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- 【42.07%】【codeforces 558A】Lala Land and Apple Trees
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【40.17%】【codeforces 569B】Inventory
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces】【比赛题解】#855 Codefest 17
神秘比赛,以<哈利波特>为主题……有点难. C题我熬夜切终于是写出来了,可惜比赛结束了,气啊. 比赛链接:点我. [A]汤姆·里德尔的日记 题意: 哈利波特正在摧毁神秘人的分灵体(魂器). ...
- 【codeforces 514D】R2D2 and Droid Army
[题目链接]:http://codeforces.com/contest/514/problem/D [题意] 给你每个机器人的m种属性p1..pm 然后r2d2每次可以选择m种属性中的一种,进行一次 ...
- 【codeforces 799D】Field expansion
[题目链接]:http://codeforces.com/contest/799/problem/D [题意] 给你长方形的两条边h,w; 你每次可以从n个数字中选出一个数字x; 然后把h或w乘上x; ...
随机推荐
- Android开发之搜芽项目的图片载入问题(使用Volley进行网络图片载入)
搜芽的移动开发这几天进度相对来说很的快. 可是美中不足的就是网络图片的载入问题. 我有两套方案: 1)沿用迅雷动漫的图片载入.迅雷动漫也是用的一个开源的库.可是不知道是我使用出了问题还是真的是它的问题 ...
- [Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''<h1 style="text-align: center;">php
[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL s ...
- Python 实用第三方库
1. youtube 视频下载 使用:you-get pip install you-get you-get 'https://www.youtube.com/watch?v=jNQXAC9IVRw'
- 百度2019校招Web前端工程师笔试卷(9月14日)
8月27日晚,在实习公司加班.当时正在调试页面,偶然打开百度首页console,发现彩蛋,于是投了简历. 9月14日晚,七点-九点,在公司笔试. 笔试题型(有出入): 一.单选20道 1.难度不难,考 ...
- hdu2049(组合数学)
题意:每位新娘打扮得差点儿一模一样,并盖上大大的红盖头随机坐成一排;然后,让各位新郎寻找自己的新娘.每人仅仅准找一个,而且不同意多人找一个.最后,揭开盖头,如果找错了对象就要当众跪搓衣板...如果一共 ...
- 51nod Bash游戏(V1,V2,V3,V4(斐波那契博弈))
Bash游戏V1 有一堆石子共同拥有N个. A B两个人轮流拿.A先拿.每次最少拿1颗.最多拿K颗.拿到最后1颗石子的人获胜.如果A B都很聪明,拿石子的过程中不会出现失误.给出N和K,问最后谁能赢得 ...
- Mosquito的优化——订阅树优化(八)
本文由逍遥子撰写.转发请标注原址: http://blog.csdn.net/houjixin/article/details/46413783 或 http://houjixin.blog.163. ...
- Oracle 带回滚的存储过程
create or replace procedure PROC_insertUserAmount ( userid number, msgtype number, amountvalue numbe ...
- php 如何写一个自己项目的安装程序
版权声明:此篇文章只是用作笔记,如果版权冲突,请邮件通知一下(15201155501@163.com) https://blog.csdn.net/shenpengchao/article/detai ...
- Android(Lollipop/5.0) Material Design(四) 创建列表和卡片
Material Design系列 Android(Lollipop/5.0)Material Design(一) 简单介绍 Android(Lollipop/5.0)Material Design( ...