hdu4991 树状数组辅助DP
2017-03-29 22:02
183 查看
Ordered Subsequence
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 221 Accepted Submission(s): 112
Problem Description
A numeric sequence of ai is ordered if a1<a2<……<aN. Let the subsequence of the given numeric sequence (a1, a2,……, aN) be any sequence (ai1, ai2,……, aiK),
where 1<=i1<i2 <……<iK<=N. For example, sequence (1, 7, 3, 5, 9, 4, 8) has ordered subsequences, eg. (1, 7), (3, 4, 8) and many others.
Your program, when given the numeric sequence, must find the number of its ordered subsequence with exact m numbers.
Input
Multi test cases. Each case contain two lines. The first line contains two integers n and m, n is the length of the sequence and m represent the size of the subsequence you need to find. The second line contains the elements of sequence - n integers in the
range from 0 to 987654321 each.
Process to the end of file.
[Technical Specification]
1<=n<=10000
1<=m<=100
Output
For each case, output answer % 123456789.
Sample Input
3 2
1 1 2
7 3
1 7 3 5 9 4 8
Sample Output
2
12
题意:从无序串中求长度为k的严格上升序列的个数。
设dp[ i ][ j ]为以位置i的数为结尾的长度为j的严格上升序列的个数。
则dp[ i ][ j ] = sum { dp[ k ][ j - 1 ] | 1 <= k <= i - 1 }
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
using namespace std;
typedef long long ll;
#define PI 3.1415926535897932
#define E 2.718281828459045
#define INF 0x3f3f3f3f
#define mod 123456789
const int M=1005;
int n,m;
int cnt;
int sx,sy,sz;
int mp[M][12];
int pa[M*10],rankk[M];
int head[M*6],vis[M*10];
double dis[M][10];
ll prime[M*1000];
bool isprime[M*1000];
int lowcost[M],closet[M];
char st1[5050],st2[5050];
int len[M*6];
typedef pair<int ,int> ac;
vector<int> g[M*10];
ll dp[10005][205];
int has[105][105];
int month[13]= {0,31,59,90,120,151,181,212,243,273,304,334,0};
int dir[8][2]= {{0,1},{0,-1},{-1,0},{1,0},{1,1},{1,-1},{-1,1},{-1,-1}};
void getpri()
{
ll i;
int j;
cnt=0;
memset(isprime,false,sizeof(isprime));
for(i=2; i<1000000LL; i++)
{
if(!isprime[i])prime[cnt++]=i;
for(j=0; j<cnt&&prime[j]*i<1000000LL; j++)
{
isprime[i*prime[j]]=1;
if(i%prime[j]==0)break;
}
}
}
ll qk_mul(ll a,ll b,ll mo)
{
ll t=0;
while(b)
{
if(b&1)
t=(t+a)%mo;
a=(a<<1)%mo;
b>>=1;
}
t%=mo;
return t;
}
ll qk_mod(ll a,ll b,ll mo)
{
ll ans=1;
while(b)
{
if(b&1) ans=qk_mul(ans,a,mo);
a=qk_mul(a,a,mo);
b>>=1;
}
ans%=mo;
return ans;
}
int gcd(int a,int b)
{
return b == 0 ? a : gcd(b, a%b);
}
int lcm(int a,int b)
{
return a*b/gcd(a,b);
}
int findx(int t)
{
if(t!=pa[t])
pa[t]=findx(pa[t]);
return pa[t];
}
void unionx(int x,int y)
{
x=findx(x);
y=findx(y);
if(x!=y)
{
pa[y]=x;
}
}
//queue<nodei *>qu;
void init()
{
for(int i=0; i<101; i++)
pa[i]=i;
}
int heap[100005];
void push(int x)
{
int i=++sz;
while(i>1) //i>0
{
int p=i/2; //(i-1)/2
if(heap[p]<=x)break;
heap[i]=heap[p];
i=p;
}
heap[i]=x;
/* a[++sz] = x;
int t = a[sz];
int tson = sz;
while( (tson > 1)&&( a[tson/2] > t))
{
a[tson] = a[tson/2];
tson = tson/2;
}
a[tson] = x;
*/
}
int pop()
{
int ret=heap[1];//int ret=a[0]
int x=heap[sz--];//a[--sz]
int i=1;// i=0
while(2*i<sz) //2*i+1
{
int a=i*2,b=i*2+1;
if(b<sz&&heap[b]<heap[a])a=b;
if(heap[a]>=x)break;
heap[i]=heap[a];
i=a;
}
heap[i]=x;
return ret;
}
ll a[10010],b[10010];
int lowbit(int i)
{
return i&(-i);
}
/*
void add(int i,int j,ll v)//修改值并向父节点修改
{
while(i<=cnt)
{
dp[i][j]+=v;//c[i]开始都是0,每经过一个数,它的c[i]+1表示c[i]
i+=lowbit(i);
}
}*/
void add(int i,int j,ll v)//修改值并向父节点修改
{
while(i<=cnt)
{
dp[i][j]+=v;//c[i]开始都是0,每经过一个数,它的c[i]+1表示c[i]
i+=lowbit(i);
}
}
ll sum(int i,int j)//求和
{
ll s=0;
while(i>=1)
{
s=(s+dp[i][j])%mod;
i-=lowbit(i);
}
return s;
}
int main()
{
int i,j,t;
while(~scanf("%d%d",&n,&m))
{
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
b[i]=a[i];
}
sort(b+1,b+1+n);
cnt=(int)(unique(b+1,b+1+n)-(b+1));//去除数组中紧接的重复的值
memset(dp,0,sizeof(dp));
//for(i=1;i<=n;i++)dp[i][1]=1;
for(i=1;i<=n;i++)
{
//memset(c,0,sizeof(c));
int id=lower_bound(b+1,b+1+cnt,a[i])-b;//
求编号(相当于离散化,因为数据原本很大,这样可以缩小到1-10000)
add(id,1,1); //树状数组区间初始化 dp[i][1]=1
for(j=2;j<=m;j++)
{
ll s=sum(id-1,j-1);//dp[i][j]=sum(dp[k][j-1])&&k<i&&a[k]<a[i]
add(id,j,s);//把 dp 数组的 id位置更新成 s 以便于 下一个 数字的时候 求和
}
}
//for(i=1;i<=n;i++)
printf("%I64d\n",sum(cnt,m));
/*ll ans=0;
for(i=1;i<=n;i++)
ans=(ans+dp[i][m-1])%mod;
printf("%I64d\n",ans%mod);*/
}
return 0;
}
相关文章推荐
- 【HDU4991】dp 树状数组优化
- BZOJ 3594 方伯伯的玉米田 (二维树状数组优化dp)
- HDU 5293 Annoying problem 树形dp dfs序 树状数组 lca
- hdu 3450(树状数组优化dp)
- LightOJ 1085 All Possible Increasing Subsequences (DP&离散化&树状数组)
- Codeforces Round #333 (Div. 1) C. Kleofáš and the n-thlon 树状数组优化dp
- 【树状数组 + 离散化 + DP】 HDU 5542
- zoj 3349 简单DP 线段树或树状数组优化
- bzoj 3594(树状数组优化dp)
- 2015 CCPC C题 【树状数组优化dp】
- LightOJ-1085-树状数组,离散化,dp
- hdu5542 树状数组优化dp
- USACO-Section 3.3 Home on the Range (树状数组||DP)
- 2015 Multi-University Training Contest 10 (hdu 5406-5416)数据结构+dp+矩阵快速幂+bitset优化拓扑排序+(dp&树状数组)
- hdu4991 - Ordered Subsequence (树状数组优化)
- [DP] [1D1D优化] [树状数组] [最短路] 遭遇战 (clean)
- BZOJ 3688 树状数组优化DP 解题报告
- UVA11775 Unique Story dp+二维树状数组优化
- 【区间DP】【二维线段树】【二维树状数组】2017.5.20 T3 deliver 题解
- Codeforces 629D Babaei and Birthday Cake(树状数组优化dp)