Sequence Game
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
由于你帮助 Alice 回答得非常好,Sept 又找到了 Bob,希望能难倒他。
他给了要求 Bob 组成一个长度为 nn 的新的数列 aa,其中数列 aa 的每一个元素 aiai 都有 kk 个取值。
求所有可能的数列 aa 中的最长上升子序列的的最大长度。
由于 Sept 怕题目钛难,所以他答应 Bob,对于每个 ii,kk 个取值不降。
输入描述:
第一行两个数 k,nk,n,意义如题述。
接下来 nn 行,每行 kk 个数,即 aiai 的 kk 个取值。
输出描述:
仅一行一个整数,即所有可能的数列 aa 中的最长上升子序列的最大长度。
示例1
输入
2 2 1 3 1 2
输出
2
说明
数列 {an}{an} 可能为 {1,2}{1,2},这时最长上升子序列的长度为 22,是最长的长度。
备注:
对于 100%100% 的数据,有 1≤k≤5×1031≤k≤5×103,1≤n≤1031≤n≤103,每个取值都是非负数,不超过 103103。
/*
*@Author: GuoJinlong
*@Language: C++
*/
//#include <bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<list>
#include<set>
#include<iomanip>
#include<cstring>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<cassert>
#include<sstream>
#include<algorithm>
using namespace std;
const int mod=1e9+7;
typedef long long ll;
#define ls (p<<1)
#define rs (p<<1|1)
#define mid (l+r)/2
#define over(i,s,t) for(register long long i=s;i<=t;++i)
#define lver(i,t,s) for(register long long i=t;i>=s;--i)
const int MAXN = 305;
const int INF = 0x3f3f3f3f;
const int N=5e4+7;
const int maxn=1e5+5;
const double EPS=1e-10;
const double Pi=3.1415926535897;
//inline double max(double a,double b){
// return a>b?a:b;
//}
//inline double min(double a,double b){
// return a<b?a:b;
//}
int xd[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int yd[8] = {1, 0, -1, 0, -1, 1, -1, 1};
//void Fire(){
// queue<node> p;
// p.push({fx,fy,0});
// memset(fire, -1, sizeof(fire));
// fire[fx][fy]=0;
// while(!p.empty()){
// node temp=p.front();
// p.pop();
// for(int i=0;i<8;i++){
// int x=temp.x+xd[i];
// int y=temp.y+yd[i];
// if(x<0||x>=n||y<0||y>=m||fire[x][y]!=-1){
// continue;
// }
// fire[x][y]=temp.val+1;
// p.push({x,y,temp.val+1});
// }
// }
//}
//int bfs(){
// queue<node> p;
// memset(vis, 0, sizeof(vis));
// p.push({sx,sy,0});
// while (!p.empty()) {
// node temp=p.front();
// vis[temp.x][temp.y]=1;
// p.pop();
// for(int i=0;i<4;i++){
// int x=temp.x+xd[i];
// int y=temp.y+yd[i];
// if(x<0||x>=n||y<0||y>=m) continue;
// if(x==ex&&y==ey&&temp.val+1<=fire[x][y]) return temp.val+1;
// if(vis[x][y]||temp.val+1>=fire[x][y]||a[x][y]=='#') continue;
// p.push({x,y,temp.val+1});
// }
// }
// return -1;
//}
//一维哈希
//int n;
//string s;
//int bas=131;
//typedef unsigned long long ull;
//const ull mod1=100001651;
//ull a[100010];
//ull Hash(string s){
// ll ans=0;
// for(int i=0;i<s.size();i++){
// ans*=bas;
// ans+=int(s[i]);
// ans%=mod1;
// }
// return ans;
//}
//二维哈希
//using lom=unsigned long long ;
//const lom bas1=131,bas2=233;
//const int M=505;
//int n,m;
//char a[M][M];
//lom _hash[M][M];
//lom p1[M],p2[M];
//
//
//void init(){
// p1[0]=1;
// p2[0]=1;
// for(int i=1;i<=505;i++){
// p1[i]=p1[i-1]*bas1;
// p2[i]=p2[i-1]*bas2;
//
// }
//}
//void Hash(){
// _hash[0][0]=_hash[0][1]=_hash[1][0]=0;
// for(int i=1;i<=n;i++){ //前缀和
// for(int j=1;j<=m;j++){
// _hash[i][j]=_hash[i][j-1]*bas1+a[i][j]-'a';
// }
// }
// for(int i=1;i<=n;i++){ //二维前缀和
// for(int j=1;j<=m;j++){
// _hash[i][j]+=_hash[i-1][j]*bas2;
// }
// }
//
//}
int n,k;
int a[1010][5010]; //取值打表
int dp[1010][5010];
int main(){
cin>>k>>n;
int x;
for(int i=1;i<=n;i++){
for(int j=1;j<=k;j++){
cin>>x;
a[i][x]=1; //第i位可以取到的值进行打表
}
}
int ans;
for(int i=1;i<=n;i++){
for(int j=1;j<=1010;j++){ //因为最大取值不超过1000
dp[i][j]=dp[i-1][j]; //从上一位取到的最大值往上取
dp[i][j]=max(max(dp[i][j],dp[i][j-1]),dp[i-1][j-1]+a[i][j]); //每位从小往大取,可取可不取
ans=max(ans,dp[i][j]);
}
}
cout<<ans;
}