HDOJ 题目2227 Find the nondecreasing subsequences(树状数组,离散化,DP)
2015-11-02 20:44
495 查看
Find the nondecreasing subsequences
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1728 Accepted Submission(s): 631
Problem Description
How many nondecreasing subsequences can you find in the sequence S = {s1, s2, s3, ...., sn} ? For example, we assume that S = {1, 2, 3}, and you can find seven nondecreasing subsequences, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}.
Input
The input consists of multiple test cases. Each case begins with a line containing a positive integer n that is the length of the sequence S, the next line contains n integers {s1, s2, s3, ...., sn}, 1 <= n <= 100000, 0 <= si <= 2^31.
Output
For each test case, output one line containing the number of nondecreasing subsequences you can find from the sequence S, the answer should % 1000000007.
Sample Input
3 1 2 3
Sample Output
7
Author
8600
Recommend
lcy | We have carefully selected several similar problems for you: 3450 2642 3030 3015 3016
ac代码
15352698 | 2015-11-02 18:35:20 | Accepted | 2227 | 1123MS | 7632K | 1190 B | G++ | XY_ |
#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> #include<map> #define INF 0x3f3f3f3f #define LL long long #define mod 1000000007 using namespace std; int val[100010],t[100010]; int a[100010],cnt; int lowbit(int x) { return x&(-x); } void add(int p,int val) { while(p<=cnt) { a[p]=(a[p]+val)%mod; p+=lowbit(p); } } int getsum(int x) { int ans=0; while(x>0) { ans=(ans+a[x])%mod; x-=lowbit(x); } return ans; } int main() { int n; while(scanf("%d",&n)!=EOF) { int i; memset(a,0,sizeof(a)); for(i=1;i<=n;i++) { scanf("%d",&val[i]); t[i]=val[i]; } sort(t+1,t+n+1); cnt=unique(t+1,t+1+n)-(t+1); map<int ,int>mp; for(i=1;i<=cnt;i++) { mp[t[i]]=i; } int ans=0; add(1,1); for(i=1;i<=n;i++) { int x=getsum(mp[val[i]]); // printf("%d\n",x); ans=(ans+x)%mod ; add(mp[val[i]],x); } printf("%d\n",ans); } }
相关文章推荐
- UIMenuController
- WeUI
- EasyUI - DataGrid 组建 - [ 删除,修改 ]
- protocol isis issues(续1)
- UIView视图
- N皇后回溯解法 leetcode N-Queens
- Java图形开发--GUI
- UIImagePickerController 选照片 拍照 录视频
- BestCoder Round #58 Beauty of Sequence 即hdu5496 (递推)
- UISearchBar基本属性
- hdu2454 Degree Sequence of Graph G (Havel-Hakimi定理,可图化判定)
- BestCoder Round #60 GT and sequence (排序)
- UIScrollview嵌套UITableview时,点击Statasubar不能滚动到顶部的问题
- Mac OS 10.9.4下配置APUE中的apue.h
- UIScrollView
- Generating Unique Random Numbers in Java
- 关于解决Installation failed due to invalid APK file! Please check logcat output for more details.Launch
- UIslider滑块大小
- 仿造微信界面左右拖拽的UI
- UIButton 的setImageEdgeInsets以及setTitleEdgeInsets