LightOJ 1085 - All Possible Increasing Subsequences (离散化+树状数组+dp)

1085 - All Possible Increasing Subsequences

  PDF (English) Statistics Forum
Time Limit: 3 second(s) Memory Limit: 64 MB

An increasing subsequence from a sequence A1, A2 ... An is defined by Ai1, Ai2 ... Aik, where the following properties hold

  1. i1 < i2 < i3 < ... < ik and
  2. 2.      Ai1 < Ai2 < Ai3 < ... < Aik

Now you are given a sequence, you have to find the number of all possible increasing subsequences.

Input

Input starts with an integer T (≤ 10), denoting the number of test cases.

Each case contains an integer n (1 ≤ n ≤ 105) denoting the number of elements in the initial sequence. The next line will contain n integers separated by spaces, denoting the elements of the sequence. Each of these integers will be fit into a 32 bit signed integer.

Output

For each case of input, print the case number and the number of possible increasing subsequences modulo 1000000007.

Sample Input

Output for Sample Input


3

3

1 1 2

5

1 2 1000 1000 1001

3

1 10 11


Case 1: 5

Case 2: 23

Case 3: 7

Notes

  1. For the first case, the increasing subsequences are (1), (1, 2), (1), (1, 2), 2.
  2. Dataset is huge, use faster I/O methods.

题目大意:

  就是说,给你一个长度为n的序列,然后,让你求出这个序列中所有的上升子序列的个数

解题思路:

  直接把tree[i]当做dp[i]来用,表示的是以a[i]结尾的上升序列的最大个数

代码:

 1 # include<iostream>
 2 # include<cstdio>
 3 # include<algorithm>
 4 # include<cstring>
 5
 6 using namespace std;
 7
 8 # define MAX 500000+4
 9 # define MOD 1000000007
10
11 typedef long long LL;
12
13 LL a[MAX];
14 LL data[MAX];
15 int tree[MAX];
16 int n,cc;
17
18 void discrete()
19 {
20     memset(data,0,sizeof(data));
21     for ( int i = 0;i < n;i++ )
22     {
23         data[i] = a[i];
24     }
25     sort(data,data+n);
26     cc = unique(data,data+n)-data;
27     for ( int i = 0;i < n;i++ )
28     {
29         a[i] = 1+lower_bound(data,data+cc,a[i])-data;
30     }
31 }
32
33
34 void update ( int pos,int val )
35 {
36     while ( pos <= n )
37     {
38         tree[pos]=(tree[pos]+val)%MOD;
39         pos += pos&(-pos);
40     }
41 }
42
43 LL read ( int pos )
44 {
45     LL sum = 0;
46     while ( pos>0 )
47     {
48         sum=(sum+tree[pos])%MOD;
49         pos-=pos&(-pos);
50     }
51     return sum%MOD;
52 }
53
54
55 int main(void)
56 {
57     int icase = 1;
58     int t;cin>>t;
59     while ( t-- )
60     {
61         while ( cin>>n )
62         {
63         if ( n==0 )
64             break;
65         LL ans = 0;
66         for ( int i = 0;i < n;i++ )
67         {
68             cin>>a[i];
69         }
70         discrete();
71         memset(tree,0,sizeof(tree));
72         for ( int i = 0;i < n;i++ )
73         {
74             update(a[i],read(a[i]-1)+1);
75         }
76         printf("Case %d: ",icase++);
77         cout<<read(cc)%MOD<<endl;
78         }
79     }
80
81     return 0;
82 }
时间: 2024-10-21 08:44:26

LightOJ 1085 - All Possible Increasing Subsequences (离散化+树状数组+dp)的相关文章

hdu 3030 Increasing Speed Limits (离散化+树状数组+DP思想)

Increasing Speed Limits Time Limit: 2000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 481    Accepted Submission(s): 245 Problem Description You were driving along a highway when you got caught by the road p

【Codeforces】Gym 101156E Longest Increasing Subsequences LIS+树状数组

题意 给定$n$个数,求最长上升子序列的方案数 根据数据范围要求是$O(n\log n)$ 朴素的dp方程式$f_i=max(f_j+1),a_i>a_j$,所以记方案数为$v_i$,则$v_i=v_i+v_j,(f_i=f_j+1)$,利用lis的$O(n\log n)$树状数组做法同时维护长度和方案数 从通酱博客里还看到更详尽的解释:stackoverflow 时间复杂度$O(n\log n)$ 代码 #include <bits/stdc++.h> using namespace

poj 2299 Ultra-QuickSort 离散化 + 树状数组

题目链接:http://poj.org/problem?id=2299 离散化 + 树状数组 教科书例题般的题目 #include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <cmath> #include <vector> #include <stack> #include <set> #include

hdu 3015 Disharmony Trees (离散化+树状数组)

Disharmony Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 663    Accepted Submission(s): 307 Problem Description One day Sophia finds a very big square. There are n trees in the square. T

CodeForces 540E - Infinite Inversions(离散化+树状数组)

花了近5个小时,改的乱七八糟,终于A了. 一个无限数列,1,2,3,4,...,n....,给n个数对<i,j>把数列的i,j两个元素做交换.求交换后数列的逆序对数. 很容易想到离散化+树状数组,但是发现那些没有交换的数也会产生逆序对数,但我没有算. 经明神提示, 把没有用到的数字段化成点.然后用树状数组算一下就好了. 然后我用一个数组记录每个点的长度.比如 <1,2><5,6>,1,2,3,4,5,6只有1,2,5,6用到了,那么离散化为1,2,3,4,5,f[1]=

【bzoj4756】[Usaco2017 Jan]Promotion Counting 离散化+树状数组

原文地址:http://www.cnblogs.com/GXZlegend/p/6832263.html 题目描述 The cows have once again tried to form a startup company, failing to remember from past experience that cows make terrible managers!The cows, conveniently numbered 1-N1-N (1≤N≤100,000), organi

POJ 2299 Ultra-QuickSort (离散化+树状数组)

题目链接:POJ 2299 Ultra-QuickSort 求一串序列相邻连个元素交换多少后,是一串上升的序列. 思路:求该串序列的逆序数,数据比较大,要离散化. AC代码: #include<stdio.h> #include<string.h> #include<set> #include<map> #include<algorithm> #define ll __int64 using namespace std; const ll max

2018中国大学生程序设计竞赛 - 网络选拔赛 1010 YJJ&#39;s Salesman 【离散化+树状数组维护区间最大值】

题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6447 YJJ's Salesman Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 919    Accepted Submission(s): 290 Problem Description YJJ is a salesman who h

hdu4455之树状数组+DP

Substrings Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1571    Accepted Submission(s): 459 Problem Description XXX has an array of length n. XXX wants to know that, for a given w, what is