HDU 2227 Find the nondecreasing subsequences(DP)
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}.
3
1 2 3
7
一看n达到了1e5的级别。就知道得nlogn算法。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<bitset>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
typedef long long LL;
typedef pair<int,int>pil;
const int INF = 0x3f3f3f3f;
const int MOD=1e9+7;
const int maxn=1e5+10;
int a[maxn],n,b[maxn+100];
LL dp[maxn],c[maxn+100];
int lowbit(int x)
{
return x&(-x);
}
void update(int x,LL val)
{
while(x<maxn)
{
c[x]=(c[x]+val)%MOD;
x+=lowbit(x);
}
}
LL query(int x)
{
LL sum=0;
while(x>0)
{
sum=(sum+c[x])%MOD;
x-=lowbit(x);
}
return sum;
}
int main()
{
while(~scanf("%d",&n))
{
int cnt=1;
CLEAR(c,0);
REPF(i,1,n)
{
scanf("%d",&a[i]);
b[cnt++]=a[i];
}
sort(b+1,b+cnt);
cnt=unique(b+1,b+cnt)-(b+1);
for(int i=1;i<=n;i++)
dp[i]=1;
LL ans=0;
for(int i=1;i<=n;i++)
{
int x=lower_bound(b+1,b+1+cnt,a[i])-b;
dp[i]=(dp[i]+query(x))%MOD;
update(x,dp[i]);
ans=(ans+dp[i])%MOD;
}
printf("%I64d\n",ans);
}
return 0;
} /* */
版权声明:本文博主原创文章。博客,未经同意不得转载。
HDU 2227 Find the nondecreasing subsequences(DP)的更多相关文章
- HDU 2227 Find the nondecreasing subsequences (DP+树状数组+离散化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 Find the nondecreasing subsequences ...
- HDU 2227 Find the nondecreasing subsequences dp思想 + 树状数组
http://acm.hdu.edu.cn/showproblem.php?pid=2227 用dp[i]表示以第i个数为结尾的nondecreasing串有多少个. 那么对于每个a[i] 要去找 & ...
- HDU 2227 Find the nondecreasing subsequences (线段树)
Find the nondecreasing subsequences Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/3 ...
- HDU 2227 Find the nondecreasing subsequences (数状数组)
Find the nondecreasing subsequences Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/3 ...
- HDU 2227 Find the nondecreasing subsequences
题目大意:给定一个序列,求出其所有的上升子序列. 题解:一开始我以为是动态规划,后来发现离散后树状数组很好做,首先,c保存的是第i位上升子系列有几个,那么树状数组的sum就直接是现在的答案了,不过更新 ...
- hdu 2227(树状数组+dp)
Find the nondecreasing subsequences Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/3 ...
- ACM学习历程——HDU2227 Find the nondecreasing subsequences(线段树 && dp)
Description How many nondecreasing subsequences can you find in the sequence S = {s1, s2, s3, ...., ...
- HDU 1024 Max Sum Plus Plus --- dp+滚动数组
HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值, ...
- HDU 1231 最大连续子序列 --- 入门DP
HDU 1231 题目大意以及解题思路见: HDU 1003题解,此题和HDU 1003只是记录的信息不同,处理完全相同. /* HDU 1231 最大连续子序列 --- 入门DP */ #inclu ...
随机推荐
- dede 首页或列表页调用文章内容页body内容
在使用dede过程,有的朋友会调调出文章的列表的内容出来,怎么调呢?当然是用dede的传参的数据查询语句了,方法如下: {dede:arclist flag=h typeid=2 row=1 titl ...
- pygame系列_draw游戏画图
说到画图,pygame提供了一些很有用的方法进行draw画图. ''' pygame.draw.rect - draw a rectangle shape draw a rectangle shape ...
- spoj Balanced Numbers(数位dp)
一个数字是Balanced Numbers,当且仅当组成这个数字的数,奇数出现偶数次,偶数出现奇数次 一下子就相到了三进制状压,数组开小了,一直wa,都不报re, 使用记忆化搜索,dp[i][s] 表 ...
- Google Map API V2密钥申请
之前用的都是v1,用的是MapView,好吧,仅仅能认命了.废话不再多说,開始android 的Google Maps Android API v2吧 之前參考了http://www.cnblogs. ...
- 【迷你微信】基于MINA、Hibernate、Spring、Protobuf的即时聊天系统:3.技术简介之MinaFilter——LoggingFilter (转)
欢迎阅读我的开源项目<迷你微信>服务器与<迷你微信>客户端 LoggingFilter 接下来,使我们对Filter介绍的最后一个——LoggingFilter. 与Proto ...
- OWIN与Katana
OWIN与Katana详解 前言 我胡汉三又回来了,!!!!, 最近忙成狗,实在没空写博文,实在对不起自己,博客园上逛了逛发现 我大微软还是很给力的 asp.net core 1.0 .net c ...
- Sqlserver中Over函数
Over函数不能单独使用,要和分析函数:rank(),dense_rank(),row_number()等一起使用. 其参数:over(partition by columnname1 order ...
- 检验身份证的正确性(C语言版本)
/* check id_card * write by sndnvaps<sndnvaps@gmail.com> * ai -> a1 , a2, a3, a4, a5, a6... ...
- git tag使用
#git tag command git tag [-a | -s | -u <key-id>] [-f] [-m <msg> | -F <file>] ...
- 【原创】leetCodeOj --- Interleaving String 解题报告
题目地址: https://oj.leetcode.com/problems/interleaving-string/ 题目内容: Given s1, s2, s3, find whether s3 ...