Problem 2236 第十四个目标

Accept: 17    Submit: 35 Time Limit: 1000 mSec    Memory Limit : 32768 KB

 Problem Description

目暮警官、妃英里、阿笠博士等人接连遭到不明身份之人的暗算,柯南追踪伤害阿笠博士的凶手,根据几起案件现场留下的线索发现凶手按照扑克牌的顺序行凶。在经过一系列的推理后,柯南发现受害者的名字均包含扑克牌的数值,且扑克牌的大小是严格递增的,此外遇害者与毛利小五郎有关。

为了避免下一个遇害者的出现,柯南将可能遭到暗算的人中的数字按关联程度排列了出来,即顺序不可改变。柯南需要知道共有多少种可能结果,满足受害人名字出现的数字严格递增,但是他柯南要找出关键的证据所在,所以这个任务就交给你了。

(如果你看不懂上面在说什么,这题是求一个数列中严格递增子序列的个数。比如数列(1,3,2)的严格递增子序列有(1)、(3)、(2)、(1,3)、(1,2),共5个。长得一样的但是位置不同的算不同的子序列,比如数列(3,3)的答案是2。)

 Input

多组数据(<=10),处理到EOF。

第一行输入正整数N(N≤100 000),表示共有N个人。

第二行共有N个整数Ai(1≤Ai≤10^9),表示第i个人名字中的数字。

 Output

每组数据输出一个整数,表示所有可能的结果。由于结果可能较大,对1 000 000 007取模后输出。

 Sample Input

3 1 3 2

 Sample Output

5

 Source

福州大学第十三届程序设计竞赛

题解:dp的思路很好想,dp[i]代表i结尾的递增序列的个数;
dp[i] = 1 + sum{dp[j]}(j < i, a[j] < a[i]);
看到sum(dp[j]),a[]j < a[i],我们很容易想到树状数组;由于数字过大,那么我们离散化下就好了;
代码:
#include<iostream>
#include<cmath>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<queue>
#include<vector>
using namespace std;
const int MAXN = ;
int dp[MAXN];
int a[MAXN], b[MAXN];
int tree[MAXN];
typedef long long LL;
const int MOD = 1e9 + ;
int lowbit(int x){return x & (-x);}
void update(int i, int x){
while(i < MAXN){
tree[i] += x;
tree[i] %= MOD;
i += lowbit(i);
}
}
int sum(int i){
int ans = ;
while(i > ){
ans += tree[i];
ans %= MOD;
i -= lowbit(i);
}
return ans;
}
int main()
{
int n;
while (~scanf("%d", &n)) {
for (int i = ; i < n; i++) {
scanf("%d", a + i);
b[i] = a[i];
}
sort(a, a + n);
memset(dp, , sizeof(dp));
memset(tree, , sizeof(tree));
LL ans = ;
int k = unique(a, a + n) - a;
for (int i = ; i < n; i++) {
int p = lower_bound(a, a + k, b[i]) - a + ;
dp[i] = sum(p - ) + ;
update(p, dp[i]);
}
for(int i = ; i < n; i++){
ans += dp[i];
ans %= MOD;
}
printf("%lld\n", ans % MOD);
}
return ;
}

比赛的时候刚开始想着用优先队列优化的,没什么用,还是超时;

代码:

#include<iostream>
#include<cmath>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<queue>
using namespace std;
const int MAXN = ;
int dp[MAXN];
int a[MAXN];
int tree[MAXN];
typedef long long LL;
const int MOD = 1e9 + ;
struct Node{
int v,p;
friend bool operator < (Node a, Node b){
return a.v > b.v;
}
};
int main()
{
int n;
while (~scanf("%d", &n)) {
for (int i = ; i <= n; i++) {
scanf("%d", a + i);
}
memset(dp, , sizeof(dp));
LL ans = ;
priority_queue<Node>Q, Q1;
Node x;
for (int i = ; i <= n; i++) {
dp[i] = ;
x.v = a[i];x.p = i;
Q.push(x);
while(Q.top().v < a[i]){
dp[i] = (dp[i] + dp[Q.top().p])%MOD;
Q1.push(Q.top());
Q.pop();
}
while(!Q1.empty()){
Q.push(Q1.top());
Q1.pop();
}
}
for(int i = ; i <= n; i++){
ans += dp[i];
ans %= MOD;
}
printf("%lld\n", ans % MOD);
}
return ;
}

线段树又写了下,发现还是用二分好,用了结构体各种错。。。

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll root<<1
#define rr root<<1|1
#define lson ll,l,mid
#define rson rr,mid+1,r
const int MOD = 1e9 + ;
const int MAXN = ;
typedef long long LL;
LL tree[MAXN << ];
LL dp[MAXN];
void pushup(int root){
tree[root] = (tree[ll] + tree[rr]) % MOD;
}
void update(int root, int l, int r, int a, int b){
int mid = (l + r) >> ;
if(a == l && a == r){
tree[root] += b;
return;
}
if(mid >= a)
update(lson, a, b);
else
update(rson, a, b);
pushup(root);
}
LL ans;
void query(int root, int l, int r, int a, int b){
int mid = (l + r) >> ;
if(l >= a && r <= b){
ans += tree[root];
ans %= MOD;
return;
}
LL ans = ;
if(mid >= a)
query(lson, a, b);
if(mid < b)
query(rson, a, b);
return ;
}
int a[MAXN], b[MAXN];
int main(){
int N;
while(~scanf("%d", &N)){
for (int i = ; i < N; i++) {
scanf("%d", a + i);
b[i] = a[i];
}
sort(a, a + N);
memset(tree, , sizeof(tree));
memset(dp, , sizeof(dp));
int k = unique(a, a + N) - a;
for(int i = ; i < N; i++){
int p = lower_bound(a, a + k, b[i]) - a + ;
ans = ;
query(, , N + , , p - );
dp[i] = ans + ;
dp[i] %= MOD;
update(, , N + , p, dp[i]);
}
printf("%lld\n", tree[] % MOD);
}
return ;
}

第十四个目标(dp + 树状数组 + 线段树)的更多相关文章

  1. 洛谷P2414 阿狸的打字机 [NOI2011] AC自动机+树状数组/线段树

    正解:AC自动机+树状数组/线段树 解题报告: 传送门! 这道题,首先想到暴力思路还是不难的,首先看到y有那么多个,菜鸡如我还不怎么会可持久化之类的,那就直接排个序什么的然后按顺序做就好,这样听说有7 ...

  2. 树状数组 && 线段树应用 -- 求逆序数

    参考:算法学习(二)——树状数组求逆序数 .线段树或树状数组求逆序数(附例题) 应用树状数组 || 线段树求逆序数是一种很巧妙的技巧,这个技巧的关键在于如何把原来单纯的求区间和操作转换为 求小于等于a ...

  3. hdu1394(枚举/树状数组/线段树单点更新&区间求和)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题意:给出一个循环数组,求其逆序对最少为多少: 思路:对于逆序对: 交换两个相邻数,逆序数 +1 ...

  4. hdu 5147 Sequence II【树状数组/线段树】

    Sequence IITime Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...

  5. hdu 1166:敌兵布阵(树状数组 / 线段树,入门练习题)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  6. 51nod 1680区间求和 (dp+树状数组/线段树)

    不妨考虑已知一个区间[l,r]的k=1.k=2....k=r-l+1这些数的答案ans(只是这一个区间,不包含子区间) 那么如果加入一个新的数字a[i](i = r+1) 则新区间[l, i]的答案为 ...

  7. HDU 5293 Train chain Problem - 树链剖分(树状数组) + 线段树+ 树型dp

    传送门 题目大意: 一颗n个点的树,给出m条链,第i条链的权值是\(w_i\),可以选择若干条不相交的链,求最大权值和. 题目分析: 树型dp: dp[u][0]表示不经过u节点,其子树的最优值,dp ...

  8. 数据结构--树状数组&&线段树--基本操作

    随笔目的:方便以后对树状数组(BIT)以及基本线段树的回顾 例题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 例题:hdu 1166 敌兵布阵 T ...

  9. BZOJ_1901_&_ZJU_2112_Dynamic_Rankings_(主席树+树状数组/线段树+(Treap/Splay))

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1901 给出一个长度为n的数列A,有m次询问,询问分两种:1.修改某一位置的值;2.求区间[l, ...

随机推荐

  1. WebGIS在行业中应用的演变

    结合我本身的项目及WebGIS在公检法行业中的应用,对此作了一个演变过程的总结:         第一阶段:GIS基本功能的应用:Data Show(数据展示):Search(搜索):Search b ...

  2. 设计模式之Application Programs and Toolkits

    Application Programs 应用程序 If you're building an application programsuch as a document editor or spre ...

  3. 阿里云安装docker

    选centos6.5输入操作系统  yum install docker-io docker -d 提示没有备用IP地址可以用来桥接卡 接下来的网卡中编辑eth0 DEVICE=eth0 ONBOOT ...

  4. Unity 触屏缩放模型

    现在的手机都是触屏控制的,那么在游戏中我们想通过手指在屏幕上滑动捕获相应的动作呢?Unity官网API中提供了Input类和Touch类,在该类里提供了许多接口.相信只要我们稍微看下,就可以自己应用了 ...

  5. C# 异步和委托学习

    IAsyncResult是接口: IAsyncResult 异步设计模式通过名为 BeginOperationName 和 EndOperationName 的两个方法来实现原同步方法的异步调用,如 ...

  6. ibatis之##与$$的 使用

    /** 主要讲一下ibatis中$$的使用: 是为了传递参数; 参数一定在Action层用''包裹起来: */ List <SysRole> userList= systemService ...

  7. CPU使用率和Load Average的关系

    看了几篇博客总结的区别,自己终于明白了含义,在这里将理解总结一下: 对于定义和解释,感觉淘测试上的更容易理解: 引用如下: CPU使用率:  一段时间内CPU的使用状况,从这个指标可以看出某一段时间内 ...

  8. Javascript进阶篇——(DOM—getAttribute()、setAttribute()方法)—笔记整理

    getAttribute()方法通过元素节点的属性名称获取属性的值.语法: elementNode.getAttribute(name) 1. elementNode:使用getElementById ...

  9. javascsript 去除数组重复数据

    function uniqid(arr){ var newArr = []; var c; for(var i = 0 ;i <= arr.length ;i++){ c = false; fo ...

  10. .net 网站发布 Web.Config中的<compilation debug="true"/>

    Web.Config中的<compilation debug="true"/> <compilation debug="true"/> ...