你最愿意做的哪件事,才是你的天赋所在

0%

HDU5542-LIS

题目链接

HDU5542

题目大意

求长度为n的数组中长度为m的非递减子序列

思路

首先离散化数组,然后建立N个树状数组tree[N][N],每次查询query(i,j)表示长度为i以j结尾的子序列有多少个,然后dp更新即可

代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
inline bool isprime(ll num)
{if(num==2||num==3)return true;
if(num%6!=1&&num%6!=5)return false;
for(int i=5;1ll*i*i<=num;i+=6){if(num%i==0||num%(i+2)==0)return false;}
return true;}
const int mod = 1e9+7;
inline ll mul(ll a,ll b,ll c){return (a*b-(ll)((ld)a*b/c)*c+c)%c;}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll g = exgcd(b,a%b,y,x);y-=a/b*x;return g;}
inline ll quick_pow(ll a,ll b,ll mod){ll res=1;while(b){if(b&1)res=mul(res,a,mod);a=mul(a,a,mod);b>>=1;}return res;}
inline ll quick_pow(ll a,ll b){ll res=1;while(b){if(b&1)res=mul(res,a,mod);a=mul(a,a,mod);b>>=1;}return res;}
inline ll inv(ll x){return quick_pow(x,mod-2);}
inline ll inv(ll x,ll mod){return quick_pow(x,mod-2,mod);}
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
const int N = 1e3+10;
ll tree[N][N];
vector<int>v;
int a[N];
int n,m;
inline int getid(int x){
return lower_bound(v.begin(),v.end(),x)-v.begin()+1;
}
inline int lowbit(int x){
return x&(-x);
}
void add(int x,ll k,int pos){
k%=mod;
while(x<=n){
(tree[pos][x] += k)%=mod;
x+=lowbit(x);
}
}
ll query(int x,int pos){
ll ans = 0;
while(x>0){
ans = (ans+tree[pos][x])%mod;
x -= lowbit(x);
}
return ans;
}
void solve(int cas){
scanf("%d%d",&n,&m);
v.clear();
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
v.push_back(a[i]);
}
sort(v.begin(),v.end());
v.erase(unique(v.begin(),v.end()),v.end());
for(int i=1;i<=n;i++)a[i] = getid(a[i]);
memset(tree,0,sizeof(tree));
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(j==1)add(a[i],1,1);
else{
ll sum =query(a[i]-1,j-1);
add(a[i],sum,j);
}
}
}
ll ans = query(n,m);
printf("Case #%d: %lld\n",cas,ans);
}
int main(){
int t;
scanf("%d",&t);
for(int i=1;i<=t;i++)solve(i);
}
-------------你最愿意做的哪件事才是你的天赋所在-------------