Codeforces 582B Once Again
http://codeforces.com/contest/582/problem/B
题目大意:给出一个序列,是由一个长度为n的序列复制T次得到的,问最长非下降子序列的长度。
思路:我们建立一个n*n的矩阵,a[i][j]代表第一段以i为末尾,第二段以j为末尾,拼接起来能增加多少长度,这样只要有一个空的矩阵c,我们让c乘上a^t,记住,这里的乘里面是:c[i][j]=max(c[i][j],a[i][k]*b[k][j]),略微和其他的矩乘不同。然后c里面元素最大那个就是答案。
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<iostream>
template<typename T>
class mx {
public:
T r[][];
int n;
public:
mx<T>(int _n):n(_n){}
mx<T>():n(){}
};
int n,T,b[];
int read(){
int t=,f=;char ch=getchar();
while (ch<''||ch>''){if (ch=='-') f=-;ch=getchar();}
while (''<=ch&&ch<=''){t=t*+ch-'';ch=getchar();}
return t*f;
}
template<typename T>
mx<T> operator *(mx<T> a,mx<T> b){
mx<T> c;
c.n=a.n;
for (int i=;i<=a.n;i++)
for (int j=;j<=a.n;j++)
c.r[i][j]=-;
for (int i=;i<=a.n;i++)
for (int j=;j<=a.n;j++)
for (int k=;k<=a.n;k++)
c.r[i][j]=std::max(c.r[i][j],a.r[i][k]+b.r[k][j]);
return c;
}
int main(){
n=read();T=read();
for (int i=;i<=n;i++)
for (int j=;j<=n;j++)
a.r[i][j]=c.r[i][j]=;
for (int i=;i<=n;i++) b[i]=read();
for (int i=;i<=n;i++)
for (int j=;j<=n;j++)
if (b[i]<=b[j]) {
a.r[i][j]=;
for (int k=;k<j;k++)
if (b[k]<=b[j])
a.r[i][j]=std::max(a.r[i][j],a.r[i][k]+);
}else a.r[i][j]=-;
a.n=n;
c.n=n;
while (T){
if (T%) c=c*a;
T/=;
a=a*a;
}
int ans=;
for (int i=;i<=n;i++)
for (int j=;j<=n;j++)
ans=std::max(ans,c.r[i][j]);
printf("%d\n",ans);
return ;
}
Codeforces 582B Once Again的更多相关文章
- 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 ...
- Codeforces Round #323 (Div. 2) D 582B Once Again...(快速幂)
A[i][j]表示在循环节下标i开头j结尾的最长不减子序列,这个序列的长度为p,另外一个长度为q的序列对应的矩阵为B[i][j], 将两序列合并,新的序列对应矩阵C[i][j] = max(A[i][ ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
随机推荐
- 这样就算会了PHP么?-7
循环之类的例子 <script language="javascript"> function calculate(a, b) { return a * b; } do ...
- altium designer14的Import wizard 为空的解决方法
1.首先将安装盘放到光驱里面,如果是虚拟光驱安装,请运行iso文件. 2. 点击DXP-->>Externtion and updates 3. 出现下列界面,选择右面的configure ...
- Cmake ,Out of Source Build
Out of Source build呢,就是让Cmake产生的临时垃圾文件,不关乎于项目实际本身的文件放到一个目录里,一般我们把这个目录放在项目根目录(也可以认为是根CmakeLists.txt)下 ...
- jquery图片3D旋绕效果 rotate3Di的操作
这是一个图片效果,很简单实用,只需要一个"rotate3Di.js"的插件就行, 关于rotate的用法有如下几种: $(选择器).rotate3Di(30); //把图片3D旋转 ...
- struts的ognl.NoConversionPossible错误
JSP页面便利集合的时候,代码如下 <s:iterator value="storageList" id="stList" status="st ...
- Android BLE开发之Android手机与BLE终端通信
本文来自http://blog.csdn.net/hellogv/ ,引用必须注明出处! 近期穿戴设备发展得非常火.把相关技术也带旺了,当中一项是BLE(Bluetooth Low Energy).B ...
- hdu 4666 Hyperspace
曼哈顿距离,两个点设为(x1,y1),(x2,y2),其距离为|x1-x2|+|y1-y2| #include <cstdio> #include <set> #include ...
- [Angular 2] Using the @Inject decorator
TypeScript is used heavily as we build up our application, but TypeScript isn’t required. If you wan ...
- [Redux] Extracting Presentational Components -- Todo, TodoList
Code to be refactored: let nextTodoId = 0; class TodoApp extends Component { render() { const { todo ...
- OLE操作Excel编译错误处理
Excel在公司用的很多,而这个东西我用的不是很好,就想用程序来处理,遇到很多错误.这几天研究了下OLE操作Excel.环境:VS2008 SP1+Excel 2007 加入OLE Type Li ...