hdu-2227-Find the nondecreasing subsequences(DP+离散化+树状数组)
来源:互联网 发布:vue.js 遮罩层 编辑:程序博客网 时间:2024/06/05 19:17
Find the nondecreasing subsequences
Time Limit : 10000/5000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 2 Accepted Submission(s) : 2
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
31 2 3
Sample Output
7
题意:
给定序列集合 求有多少个非递减子序列。
思路:每多加一个数的方法数是之前所有小于等于它的方法数之和加一,数据较大所以 所以需要 离散化。然后用树状数组维护。
code:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#define ll long long#define mod 1000000007#define maxn 100005#define lowbit(x) (x&(x^(x-1)))using namespace std;struct node{ int key;///保存原序列的次序,离散化后用来唯一代表这个val值 ll val;///序列的大小};node a[maxn];int c[maxn];int b[maxn];int n;int sum(int p){ ll ans=0; while(p>0){ ans=(ans+c[p])%mod; p-=lowbit(p); } return ans;}void update(int p,int x){ while(p<=n){ c[p]=(c[p]+x)%mod; p+=lowbit(p); }}bool cmp(node a,node b){ return a.val<b.val;}int main(){ while(~scanf("%d",&n)){ for(int i=1;i<=n;i++){ scanf("%lld",&a[i].val); a[i].key=i; } sort(a+1,a+n+1,cmp); b[ a[1].key ]=1; int cnt=1; for(int i=2;i<=n;i++){ if(a[i].val!=a[i-1].val) cnt++; b[a[i].key]=cnt;///cnt 用来代表它的大小 } memset(c,0,sizeof(c)); for(int i=1;i<=n;i++){ int temp=sum(b[i]); update(b[i],temp+1); } printf("%d\n",sum(n)); } return 0;}
阅读全文
0 0
- hdu 2227 Find the nondecreasing subsequences (树状数组+dp+离散化)
- hdu-2227-Find the nondecreasing subsequences(DP+离散化+树状数组)
- hdu 2227 Find the nondecreasing subsequences【离散化+树状数组+DP思路】
- hdu 2227Find the nondecreasing subsequences(树状数组+dp+离散化)
- hdu 2227 Find the nondecreasing subsequences(树状数组+DP)
- HDOJ 题目2227 Find the nondecreasing subsequences(树状数组,离散化,DP)
- hdu 2227 Find the nondecreasing subsequences 树状数组+简单DP
- HDU2227 Find the nondecreasing subsequences(树状数组+DP+离散化)
- HDU - 2227 Find the nondecreasing subsequences (树状数组 + 子序列 + 离散化)
- HDU 2227 Find the nondecreasing subsequences(线段树+离散化+DP)
- hdoj 2227 Find the nondecreasing subsequences 【树状数组优化dp】
- hdu 2227 Find the nondecreasing subsequences(树状数组)
- HDU 2227 Find the nondecreasing subsequences 树状数组
- hdu 2227 Find the nondecreasing subsequences(树状数组+排序)
- hdu-Find the nondecreasing subsequences(树状数组)
- Find the nondecreasing subsequences--(树状数组)
- Find the nondecreasing subsequences(数状数组+离散化+dp)
- HDU 2227 Find the nondecreasing subsequences(DP)
- I/O复用的不同机制间区别(select/poll/epoll)
- IT欲与人类试比情
- 湘潭大学2017年下学期程序设计实践-模拟测试1 题解
- 模拟实现C++中的ListIterator函数
- python---flask_sqlalchemy执行sql
- hdu-2227-Find the nondecreasing subsequences(DP+离散化+树状数组)
- Hive 内表与外表的区别
- matlab正余弦画心形图案
- 数组的复制和排序
- 决策树算法
- 《设计模式》学习笔记2——简单工厂模式
- Linux系统文件编程
- 数据结构实验之图论八:欧拉回路
- Python日积月累