Find the nondecreasing subsequences
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 503 Accepted Submission(s): 192
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
Sample Output
题目大意:给你一个串,求这个串中不递减的子串有多少个?初一看,完全想不到会是用树状数组,但是他就是这么神奇,不递减就想到逆序数,逆序数又想到了树状数组,居然是用他。他是求子串有多少个,又要用到DP,这里DP就是用树状数组慢慢推上去,最后是注意是求和会溢出,记得%1000000007。
代码:
#include <iostream>
#include <stdio.h>
#include <memory.h>
#include <algorithm>
using namespace std;
struct node
{
int val, id;
}a[100005];
bool cmp(node a, node b)
{
return a.val < b.val;
}
int b[100005], c[100005], s[100005], n;
int lowbit(int i)
{
return i&(-i);
}
void update(int i, int x)
{
while(i <= n)
{
s[i] += x;
if(s[i] >= 1000000007)
s[i] %= 1000000007;
i += lowbit(i);
}
}
int sum(int i)
{
int sum = 0;
while(i > 0)
{
sum += s[i];
if(sum >= 1000000007)
sum %= 1000000007;
i -= lowbit(i);
}
return sum;
}
int main()
{
int i, res;
while(scanf("%d", &n) != EOF)
{
memset(b, 0, sizeof(b));
memset(s, 0, sizeof(s));
for(i = 1; i <= n; i++)
{
scanf("%d", &a[i].val);
a[i].id = i;
}
sort(a+1, a+n+1, cmp);
b[a[1].id] = 1;
for(i = 2; i <= n; i++)
{
if(a[i].val != a[i-1].val)
b[a[i].id] = i;
else b[a[i].id] = b[a[i-1].id];
}
res = 0;
for(i = 1; i <= n; i++)
{
c[i] = sum(b[i]);
update(b[i], c[i]+1);
}
printf("%d\n", sum(n));
}
return 0;
}
分享到:
相关推荐
HDU的一题........HDU DP动态规
hdu 1695 GCD(欧拉函数+容斥原理).docx
acm hdu as easy as a+b
动态规划(dynamic programming)是运筹学的一个分支,是求解决策过程(decision process)最优化的数学方法。20世纪50年代初美国数学家R.E.Bellman等人在研究多阶段决策过程(multistep decision process)的优化问题时,...
hdu 3341(ac自动机+状态压缩) 题意:容易理解... 思路:首先一开始容易想到要用到dp,开设一个dp[41][41][41][41][501]的数组来解决,但是明显内存已经超出范围了,于是就想如何减少内存呢?只要知道A、T、C、G其中...
HDU上DP大集合,里面包括题,题解,代码,对DP入门者很实用,对DP老手也是有很大的提高
ACM题库,一些题目和答案,以及解题报告,传上来共享
动态规划DP题解 POJ HDU部分动态规划DP题解
本人准备2020年保研机试时刷的题目(虽然最后机试取消了,...来自某中流985,在HDU和vjudge平台上大概刷了400道。本文件地图(excel表格)包含了绝大部分我刷过的题目,笔记中具有思路、代码、总结和心得。 大佬勿入!
杭电OnlineJudge 200-2099的解题报告
300+ AC 代码 。 大数 , 线段树 , 字符串 , dp.....
hdu 1005.比较简单的一道题,有兴趣的可以看看。
HDU的1250,主要是利用高精度加法,但是代码有点繁琐,效率不是很高
Please write a program to find the smallest positive integer n that (f(n,m)?n)⊕n=k, or determine it is impossible. Input The first line of the input contains an integer T(1≤T≤10), denoting the ...
HDU1059的代码
杭电ACMhdu1163
应同学之邀帮忙发布的一篇勘误 【HDU 3993】田忌赛马 题解+勘误 题解这里就略写一下了,主要是勘误。 这道题是2011年之前的多校...数据很小,可以考虑状态压缩+记忆化搜索 直接定义二维状态有很多浪费的空间,可以考虑u
hdu1001解题报告
hdu 1574 passed sorce
hdu acm 教案 搜索入门 hdu acm 教案 搜索入门