Similar Subsequence
Accepted : Submit :
Time Limit : MS Memory Limit : KB Similar Subsequence For given sequence A=(a1,a2,…,an), a sequence S=(s1,s2,…,sn) has shape A if and only if: si=min{si,si+,…,sn} for all ai=;
si=max{si,si+,…,sn} for all ai=. Given sequence B=(b1,b2,…,bm), Bobo would like to know the number of subsequences of length n which have shape A modulo (+).
Input The input contains zero or more test cases and is terminated by end-of-file. For each test case: The first line contains two integers n and m. The second line contains n integers a1,a2,…,an. The thrid line contains m integers b1,b2,…,bm. The number of test cases does not exceed .
≤n≤
≤m≤
≤ai≤
≤bi≤m
b1,b2,…,bm are distinct. Output For each case, output an integer which denotes the number of subsequences modulo (+).
Sample Input Sample Output Note For the first sample, all three subsequences of length are of shape A. Source
XTU OnlineJudge /**
题目:Similar Subsequence
链接:http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1269
题意:给定A序列和B序列。A序列的数为0或1。 从B中取出一个长度和A相同的子序列S满足
si=min{si,si+1,…,sn} for all ai=0;
si=max{si,si+1,…,sn} for all ai=1. 问这样的S序列有多少种。结果mod 1e9+7;
个人分析过程:
显然要对A序列来处理。
假设最后一个是0,表明选的数,必须满足min{si,si+1,...,sn}。
那么如何确定B序列中的某个位置作为它。
最后一个数B序列任何位置皆可,无论a=0,1都可以满足min,max关系,因为只有它一个数。
倒数第二个数如何确认?
假设确定的最后一个数为x。
an-1 = 0; 那么这个数要比x小。
an-1 = 1; 那么这个数要比x大。 an-2 = 0; 那么这个数要比前面选过的数都小。
an-2 = 1; 那么这个数要比前面选过的数都大。
1: sn-2>sn-1
sn-1>x
sn = x;
sn-1<x
0: sn-2<sn-1 维护当前选好的序列中的最大和最小。
dp[i][j][k][flag]表示还要选i个数,当前选好的序列的最大值位置为j,最小值位置为k,
上一次选的位置为flag,flag=0表示为j位置,flag=1表示为k位置,的方法数。
因为:每次选了一个数后,位置一定是j或者k中的一个。
if(a[i]==0){ 通过flag来判断上次的选的位置,然后向前枚举遍历找一个满足<a[k]的位置pos; dp[i][j][k][flag] += dfs(i-1,j,pos,1);}
if(a[i]==1){ 通过flag来判断上次的选的位置,然后向前枚举遍历找一个满足>a[j]的位置pos; dp[i][j][k][flag] += dfs(i-1,pos,k,0);} if(i==0) return 1; 等一下:先试试对A序列从左到右考虑。 a1 = 0; 那么选择的第一个数为x必须满足B序列的x的右边比x大的数的集合为s,|s|>=n-1;
a1 = 1; 那么选择的第一个数为x必须满足B序列的x的右边比x小的数的集合为s,|s|>=n-1; a2 = 0;从前一个更新的集合s中,选择一个数,选择的x必须满足B序列的x的右边比x大的数的集合为s,|s|>=n-2; a3 = ?; 同理。 暂时不知怎么处理。 再回去试试从右往左考虑A序列。 正确思路:
在上面分析的基础上,由于时间超限了,对向前枚举遍历找一个满足<a[k]的位置pos;向前枚举遍历找一个满足>a[j]的位置pos;
这种操作优化一下。预处理mis[i]表示j<i, b[j]<b[i]的所有的j。mas[i]表示j<i, b[j]>b[i]的所有的j。
时间刚刚好卡过去了。。9000多ms。题目时限10s。 */ #include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> P;
const int maxn = 1e5+;
const int mod = 1e9+;
LL dp[][][][];
vector<int> mis[];///mis[i]表示j<i, b[j]<b[i]的所有的j。
vector<int> mas[505];///mas[i]表示j<i, b[j]>b[i]的所有的j。
int n, m;
int a[], b[];
void init()
{
for(int i = ; i <= m; i++){
mis[i].clear();
mas[i].clear();
}
for(int i = ; i <= m; i++){
for(int j = ; j < i; j++){
if(b[j]<b[i]){
mis[i].push_back(j);
}
if(b[j]>b[i]){
mas[i].push_back(j);
}
}
} }
LL dfs(int i,int j,int k,int flag)
{
if(i==) return ;
LL &res = dp[i][j][k][flag];
if(res!=-) return res;
res = ;
if(flag==){///j
if(j-<i) return res = ;
if(a[i]==){
/// j 的前面 比b[k]小的数。j<k;
int len = mis[k].size();
for(int d = ; d < len; d++){
if(mis[k][d]>=j) break;
res = (res+dfs(i-,j,mis[k][d],))%mod;
}
}else
{
int len = mas[j].size();
for(int d = ; d < len; d++){
res = (res+dfs(i-,mas[j][d],k,))%mod;
}
}
}else///k
{
if(k-<i) return res = ;
if(a[i]==){
int len = mis[k].size();
for(int d = ; d < len; d++){
res = (res+dfs(i-,j,mis[k][d],))%mod;
}
}else
{
int len = mas[j].size();
for(int d = ; d < len; d++){
if(mas[j][d]>=k) break;
res = (res+dfs(i-,mas[j][d],k,))%mod;
}
}
}
return res;
}
int main()
{
while(scanf("%d%d",&n,&m)==)
{
for(int i = ; i <= n; i++) scanf("%d",&a[i]);
for(int i = ; i <= m; i++) scanf("%d",&b[i]);
init();
memset(dp, -, sizeof dp);
LL ans = ;
for(int i = m; i >= n; i--){
ans += dfs(n-,i,i,);
ans %= mod;
}
printf("%I64d\n",ans%mod);
}
return ;
}

2017-5-14 湘潭市赛 Similar Subsequence 分析+四维dp+一些简单优化的更多相关文章

  1. 模拟赛20181015 Uva1078 bfs+四维dp

    题意:一张网格图,多组数据,输入n,m,sx,sy,tx,ty大小,起终点 接下来共有2n-1行,奇数行有m-1个数,表示横向的边权,偶数行有m个数,表示纵向的边权 样例输入: 4  4  1  1  ...

  2. 「模拟赛20190327」 第二题 DP+决策单调性优化

    题目描述 小火车虽然很穷,但是他还是得送礼物给妹子,所以他前往了二次元寻找不需要钱的礼物. 小火车准备玩玩二次元的游戏,游戏当然是在一个二维网格中展开的,网格大小是\(n\times m\)的,某些格 ...

  3. Gitlab一键端的安装汉化及问题解决(2017/12/14目前版本为10.2.4)

    Gitlab的安装汉化及问题解决 一.前言 Gitlab需要安装的包太TM多了,源码安装能愁死个人,一直出错,后来发现几行命令就装的真是遇到的新大陆一样... ... 装完之后感觉太简单,加了汉化补丁 ...

  4. hdu6212[区间dp] 2017青岛ACM-ICPC网络赛

    原题: BZOJ1032 (原题数据有问题) /*hdu6212[区间dp] 2017青岛ACM-ICPC网络赛*/ #include <bits/stdc++.h> using name ...

  5. [徐州网络赛]Longest subsequence

    [徐州网络赛]Longest subsequence 可以分成两个部分,前面相同,然后下一个字符比对应位置上的大. 枚举这个位置 用序列自动机进行s字符串的下标转移 注意最后一个字符 #include ...

  6. 鸿蒙内核源码分析(编译过程篇) | 简单案例窥视GCC编译全过程 | 百篇博客分析OpenHarmony源码| v57.01

    百篇博客系列篇.本篇为: v57.xx 鸿蒙内核源码分析(编译过程篇) | 简单案例窥视编译全过程 | 51.c.h.o 编译构建相关篇为: v50.xx 鸿蒙内核源码分析(编译环境篇) | 编译鸿蒙 ...

  7. 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP) 链接:https://ac.nowcoder.com/acm/contest/163/ ...

  8. HDU6578 2019HDU多校训练赛第一场 1001 (dp)

    HDU6578 2019HDU多校训练赛第一场 1001 (dp) 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6578 题意: 你有n个空需要去填,有 ...

  9. 2017-5-14 湘潭市赛 Longest Common Subsequence 想法题

    Longest Common Subsequence Accepted : Submit : Time Limit : MS Memory Limit : KB Longest Common Subs ...

随机推荐

  1. Windows 系统下设置Nodejs NPM全局路径和环境变量配置

    在nodejs的安装目录中找到node_modules\npm\.npmrc文件 修改如下即可: prefix = D:\tool\nodejs\node_globalcache = D:\tool\ ...

  2. linux-改变文件属主权限-chown

    http://www.cnblogs.com/peida/archive/2012/12/04/2800684.html chown将指定文件的拥有者改为指定的用户或组,用户可以是用户名或者用户ID: ...

  3. Orcale自增/Hibernate 配置

    -- 自增  create sequence SEQ_T_APP_USER start with 1 increment by 1; -- 触发器 create trigger DECTUSER_T_ ...

  4. JNI之常用函数大全

    要素  :1. 该函数大全是基于C语言方式的,对于C++方式可以直接转换 ,例如,对于生成一个jstring类型的方法转换分别如下: C编程环境中使用方法为:(*env) ->NewString ...

  5. ActiveMQ实战-集群

    原文:http://blog.csdn.net/lifetragedy/article/details/51869032 ActiveMQ的集群 内嵌代理所引发的问题: 消息过载 管理混乱 如何解决这 ...

  6. 所见即所得的网页设计工具 Macaw

    所见即所得的网页设计工具 Macaw: 2014年最具前景的12款创新产品 1\   http://macaw.co/ 2\   http://www.kickstarter.com/projects ...

  7. 打补丁以及WebLogic Server的版本

    12.1.2开始采用了Oracle传统的opatch打补丁的方式,但在此之前,包括 10.3.x版本以及12.1.1版本还是通过bea的smart update方式来进行. smart update基 ...

  8. [Python爬虫] 之十九:Selenium +phantomjs 利用 pyquery抓取超级TV网数据

    一.介绍 本例子用Selenium +phantomjs爬取超级TV(http://www.chaojitv.com/news/index.html)的资讯信息,输入给定关键字抓取资讯信息. 给定关键 ...

  9. 对jsp中Url含中文字符的编码处理

    有一段url="/app/index/index.jsp?userName='测试'":在传入到jsp页面后. 用 <%  String userName=request.g ...

  10. [Javascript] Conditionally spread entries to a JavaScript object

    In JavaScript, we often end up composing one object out of several other objects. Luckily there's a ...