HDU_2227_FindTheNondecreasingSubsequences
2015-08-17 14:14
351 查看
Find the nondecreasing subsequences
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1688 Accepted Submission(s): 612
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
水题
坐标离散化一下
然后每个点的和应该是这个点之前的子串个数
因此每次add加的操作是加的之前所有满足条件子串的个数+1
1 2 3 4
如读到1 则前面没有 就0加1
读到2 则前面的有1 1+1=2;
读到3 则前面的有 1 2 1+2+1=4;
读到4 则前面的有 1 2 3 1+2+4+1=8;
因此总共有1+2+4+8=15
注意下数据量还有取模
#include <iostream> #include <stdio.h> #include <algorithm> #include <string.h> using namespace std; typedef long long LL; const int M=1e5+5; const int MO=1e9+7; LL tree[M]; struct NODE { int v,p; }node[M]; bool cmp1(NODE a,NODE b) { if(a.v==b.v) return a.p<b.p; return a.v<b.v; //因为输入没有保证数字不重复,防止排序和离散造成原本可以形成的序列被否定 } bool cmp2(NODE a,NODE b) { return a.p<b.p; } inline int lowbit(int x) { return x&(-x); } LL getsum(int x) { LL s=0; for(int i=x;i;i-=lowbit(i)) s=(s+tree[i])%MO; return s; } void add(int x,LL v) { for(int i=x;i<M;i+=lowbit(i)) tree[i]=(tree[i]+v)%MO; } int main() { int n; while(scanf("%d",&n)!=EOF) { memset(tree,0,sizeof(tree)); for(int i=0;i<n;i++) { scanf("%d",&node[i].v); node[i].p=i; } sort(node,node+n,cmp1); for(int i=0;i<n;i++) node[i].v=i+1; sort(node,node+n,cmp2); for(int i=0;i<n;i++) add(node[i].v,getsum(node[i].v)+1); //加的是之前满足条件数字情况的和 printf("%I64d\n",getsum(M-1)); } return 0; }
相关文章推荐
- intent中的请求码与结果码
- 64位简体中文 Win10最新版Build 10240.16425镜像下载
- Atlassian Confluence 安装
- POJ_2299_Ultra-QuickSort
- 关于 UITextField 和 键盘 的通知、代理调用顺序
- How to Build a RESTful API Web Service with PHP
- UI设计师不可不知的安卓屏幕知识
- Windows 7 SP1 多国语言包(MUI language packs)官方下载
- UI星辰:UI代码创建视图和Label设置
- 栈 Deque
- poj 3080 Blue Jeans 暴力
- UILabel 多行文本及动态计算其高度
- UIView与CALayer的区别
- in query_vcvarsall raise ValueError(str(list(result.keys()))) ValueError: [u'path'] 解决
- hdu 5301 Buildings 2015 Multi-University Training Contest 2
- iOS7 UIKit动力学-碰撞特性UICollisionBehavior 上
- iiOS开发UI篇--IOS动画(Core Animation)总结
- QueryInterface
- AVplayer的使用详解 完整注释
- iOS 9界面适配利器:详解Xcode 7的新特性UIStackView