lightoj1085 - All Possible Increasing Subsequences(树状数组)
来源:互联网 发布:java内部类ppt 编辑:程序博客网 时间:2024/05/16 18:03
题意:给你一个数列,问你有几个递增子序列(单个数字也算一个)
a1, a2, a3....an以ai结尾的递增子序列数量为在此之前出现的并且ak(k<i&&ak<ai)比他小的ak结尾的递增子序列数量之和+1(它本身)
这样就可以用树状数组做了,不断插入并且计算在这之前比他小的子序列和.
因为ai可能很大,所以此题需要离散化一下。
还有别忘了取模。
代码:
#include<bits/stdc++.h>using namespace std;const int maxn = 1e5+5;const int mod = 1e9+7;int tree[maxn], b[maxn];struct node{ int index, val; bool operator < (const node &a) const { return val < a.val; }}a[maxn];int lowbit(int x) { return x&(-x); }void update(int pos, int val){ while(pos < maxn) { tree[pos] = (tree[pos]+val)%mod; pos += lowbit(pos); }}int query(int pos){ int sum = 0; while(pos) { sum = (sum+tree[pos])%mod; pos -= lowbit(pos); } return sum;}int main(void){ int n, t, ca = 1; cin >> t; while(t--) { memset(tree, 0, sizeof(tree)); scanf("%d", &n); for(int i = 1; i <= n; i++) scanf("%d", &a[i].val), a[i].index = i; sort(a+1, a+1+n); for(int i = 1; i <= n; i++) { b[a[i].index] = i; if(a[i].val == a[i-1].val) b[a[i].index] = b[a[i-1].index]; } int temp, sum = 0; for(int i = 1; i <= n; i++) { temp = (query(b[i]-1)+1)%mod; sum = (sum+temp)%mod; update(b[i], temp); } printf("Case %d: %d\n", ca++, sum); } return 0;}
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. 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.
- lightoj1085 - All Possible Increasing Subsequences(树状数组)
- lightoj1085 All Possible Increasing Subsequences
- 1085 - All Possible Increasing Subsequences[树状数组]
- [树状数组]LightOJ 1085 - All Possible Increasing Subsequences
- Lightoj 1085 All Possible Increasing Subsequences (树状数组+DP)
- lightoj 1085 - All Possible Increasing Subsequences 【树状数组优化dp】
- lightoj 1085 All Possible Increasing Subsequences(递推式+离散化+树状数组维护)
- LightOJ 1085 All Possible Increasing Subsequences (DP&离散化&树状数组)
- light oj1085All Possible Increasing Subsequences(树状数组+离散化+递推)
- lightoj 1085 All Possible Increasing Subsequences
- Light OJ 1085 - All Possible Increasing Subsequences
- LightOJ 1085 - All Possible Increasing Subsequences(DP + 线段树 + 离散)
- sdut 2605 Mountain Subsequences(树状数组)
- Find the nondecreasing subsequences--(树状数组)
- Increasing Subsequences
- Increasing Subsequences
- CF - 314C - Sereja and Subsequences(树状数组+dp)
- hdu-Find the nondecreasing subsequences(树状数组)
- 通俗易懂JSONP讲解
- Android 性能优化 概念介绍
- E - Repository (字典树)
- 冒泡排序原理
- ListView中的观察者模式
- lightoj1085 - All Possible Increasing Subsequences(树状数组)
- 《CANOpen》 学习笔记3
- 用Android studio直接获取debug和release的SHA1和MD5值
- 第八周项目2——建立链串的算法库
- 【XJOI】NOIP2016提高组冲剌题1
- Node.js+webStrom 环境配置
- leetcode 1 two sum
- eclipse启动tomcat无法访问的解决方法
- UBUNTU 14.04 在 vmware workstation10下无法进入unity模式