Find the nondecreasing subsequences
来源:互联网 发布:淘宝分销平台系统 编辑:程序博客网 时间:2024/06/16 12:17
31 2 3
7
保存输入的数和序号在结构体中,然后按数的大小排序。
之后b数组保存排序后的顺序。相当用树状数组求逆序数。
for i=1:n
b[i]
按输入的原来顺序(b[i]保存排序后的顺序)
更新树状数组。统计出比他小的数总共有几个。最后sum[n]为所求。
#include<iostream>
#include<stdio.h>
#include<memory.h>
#include<algorithm>
using namespace std;
#define MAX 100005
#define BASE 1000000007
struct node{
int val,id;
}a[MAX];
bool cmp(node a,node b)
{
return a.val<b.val;
}
int b[MAX],c[MAX],s[MAX],n;
int lowbit(int i)
{
return i&(-i);
}
void update(int i,int x)
{
while(i<=n)
{
s[i]+=x;
if(s[i]>=BASE)
s[i]%=BASE;
i+=lowbit(i);
}
}
int sum(int i)
{
int sum=0;
while(i>0)
{
sum+=s[i];
if(sum>=BASE)
sum%=BASE;
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;
}
- Find the nondecreasing subsequences
- HDU2227:Find the nondecreasing subsequences
- HDU find the nondecreasing subsequences
- hdu 2227 Find the nondecreasing subsequences
- Find the nondecreasing subsequences HDU 2227
- 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)
- 树状数组 hdu2227 Find the nondecreasing subsequences
- HDU 2227 Find the nondecreasing subsequences
- HDU2227(Find the nondecreasing subsequences)
- HDOJ 2227 Find the nondecreasing subsequences
- Find the nondecreasing subsequences--(树状数组)
- 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
- setTimeout和setInterval的使用
- MFC Button按钮的用法
- 高清音、视频数据截取存储问题请教!
- Present ViewController详解
- 2013.8.11-数学日2的总结
- Find the nondecreasing subsequences
- 浙江大学PAT上机题解析之1050. String Subtraction (20)
- bootargs参数详解
- 培训理论
- 完全用Linux工作,摈弃Windows
- NSFileHandle & NSFileManager 文件创建及写入例程
- skin care
- iOS 不同版本 判断
- 【业务员必备十个最有成效的销售成交法】