light oj 1085
来源:互联网 发布:js 指定时间转时间戳 编辑:程序博客网 时间:2024/06/05 16:51
Submit Status uDebug
Description
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
3
3
1 1 2
5
1 2 1000 1000 1001
3
1 10 11
Sample Output
Case 1: 5
Case 2: 23
Case 3: 7
Source
这题首先要对树状数组有很好的理解,注意排序时的技巧把序号大的放前面,这样就能保证序列一定是递增的,dp[i]为以结尾的上升子区间和,,,技巧性很强,自行领会
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const int N = 100010;
const int mod = 1000000007;
int dp[N], a[N], p[N];
int n;
int lowbit(int k)
{
return k&(-k);
}
int cmp(int x,int y)
{
if(a[x]!=a[y])
{
return a[x]<a[y];
}
else
{
return x>y;
}
}
int sum(int x)
{
int ans=0;
while(x>0)
{
ans=(ans+dp[x])%mod;
x-=lowbit(x);
}
return ans;
}
void inser(int x,int v)
{
while(x<=n)
{
dp[x]=(dp[x]+v)%mod;
x+=lowbit(x);
}
return ;
}
int main()
{
int t, ncase=1;
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
for(int i=1;i<=n;i++)
{
scanf("%d", &a[i]);
p[i]=i;
}
sort(p+1,p+n+1,cmp);
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++)
{
inser(p[i],sum(p[i])+1);
}
printf("Case %d: %d\n",ncase++,sum(n));
}
return 0;
}
- light oj 1085
- light oj
- light oj
- Light OJ
- Light OJ
- Light OJ 1085 - All Possible Increasing Subsequences
- Light OJ 1000
- Light OJ 1001
- Light OJ 1008
- Light OJ 1022
- Light OJ 1015
- Light OJ 1042
- light oj 1128
- Light OJ 1055 BFS
- Light OJ Beginners Problems
- Light OJ Basic Geometry
- Light OJ Basic Math
- light oj 1124
- Bootstrap出现$(...)modal is not a function错误
- 树状数组
- map的四种遍历
- android 软键盘 InputMethodManager
- CheckBoxList控件绑定数据和设置选定项
- light oj 1085
- 自己经常去的网站记录
- Oracle子查询
- Selenium框架源码解析-1 PageFactory
- Servlet的一点零碎知识
- 测试人员如何搭建自动打包部署平台?(具体详细步骤及下载地址)
- 在Mac上搭建Nginx服务器(视频直播)
- 55. Jump Game(重要)
- #451 Swap Nodes in Pairs