hdu 2227 Find the nondecreasing subsequences(树状数组+排序)
来源:互联网 发布:代码统计软件 编辑:程序博客网 时间:2024/05/22 12:46
Find the nondecreasing subsequences
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1545 Accepted Submission(s): 558
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
Author
8600
Recommend
lcy | We have carefully selected several similar problems for you: 3450 2838 2642 2688 3030
题目分析:
枚举以每个点为结尾的上升子序列的个数,再求和
每个点的上升子序列的个数等于当前点前面所有不比当前点大的点所有的上升子序列的个数之和
那么利用树状数组维护这个前缀和即可,因为取值范围大
先排序,保证值从小到大,树状数组维护位置的前缀和即可,用到了转换的思想
枚举以每个点为结尾的上升子序列的个数,再求和
每个点的上升子序列的个数等于当前点前面所有不比当前点大的点所有的上升子序列的个数之和
那么利用树状数组维护这个前缀和即可,因为取值范围大
先排序,保证值从小到大,树状数组维护位置的前缀和即可,用到了转换的思想
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#define MAX 100007#define MOD 1000000007using namespace std;int n;struct Point{ int a,id; bool operator < ( const Point & x ) const { if ( a == x.a ) return id < x.id; return a < x.a; }}p[MAX];//int tid[MAX];int c[MAX];int cnt;int lowbit ( int x ){ return x&-x;}void add ( int x , int v ){ while ( x <= n ) { c[x] = (c[x]+v)%MOD; x += lowbit (x); }}int sum ( int x ){ int ret = 0; while ( x ) { ret = ( ret + c[x] ) %MOD; x -= lowbit (x); } return ret;}int main ( ){ while ( ~scanf ( "%d" , &n ) ) { cnt = 1; memset ( c , 0 , sizeof ( c ) ); for ( int i = 0 ; i < n ; i++ ) { scanf ( "%d" , &p[i].a ); p[i].id = i+1; } sort ( p , p+n ); /*tid[p[0].id] = cnt++; for ( int i = 1 ; i < n ; i++ ) if ( p[i].a != p[i-1].a ) tid[p[i].id] = cnt++; else tid[p[i].id] = cnt;*/ int ans = 0; for ( int i = 0 ; i < n ; i++ ) { int temp = (sum ( p[i].id )+1)%MOD; ans = ( ans + temp ) %MOD; add ( p[i].id , temp ); } printf ( "%d\n" , ans ); }}
0 0
- hdu 2227 Find the nondecreasing subsequences(树状数组+排序)
- hdu 2227 Find the nondecreasing subsequences(树状数组)
- HDU 2227 Find the nondecreasing subsequences 树状数组
- hdu 2227 Find the nondecreasing subsequences 树状数组+简单DP
- hdu 2227 Find the nondecreasing subsequences(树状数组+DP)
- hdu-Find the nondecreasing subsequences(树状数组)
- 树状数组 hdu2227 Find the nondecreasing subsequences
- Find the nondecreasing subsequences--(树状数组)
- hdoj 2227 Find the nondecreasing subsequences 【树状数组优化dp】
- hdu 2227 Find the nondecreasing subsequences【离散化+树状数组+DP思路】
- hdu 2227 Find the nondecreasing subsequences (树状数组+dp+离散化)
- HDU - 2227 Find the nondecreasing subsequences (树状数组 + 子序列 + 离散化)
- hdu 2227Find the nondecreasing subsequences(树状数组+dp+离散化)
- hdu-2227-Find the nondecreasing subsequences(DP+离散化+树状数组)
- hdu 2227 Find the nondecreasing subsequences
- Find the nondecreasing subsequences HDU 2227
- HDU 2227 Find the nondecreasing subsequences
- HDU - 2227 Find the nondecreasing subsequences
- ALPHA_8、ARGB_4444、ARGB_8888和RGB_565的区别
- Redis 替代品SSDB介绍及使用
- 我的第一个Java图形程序
- ISP简介(ISP--Interface Segregation Principle)
- favicon.ico网站logo-显示在浏览器地址栏前的图片
- hdu 2227 Find the nondecreasing subsequences(树状数组+排序)
- 简略版本MVC框架JWork的实现
- css框架bootstrap(练习表单)
- SQL Server 日期格式转换
- EL表达式
- air jordan pas cher not only did not eat hot dogs behavior for extra points
- 协定存款的基础
- 在Mac上用MAMP快速的搭建PHP、MySQL、Apache开发运行环境
- 【浅尝JAVA】那些JAVA我所不知道的_Oracle模考