hdu 5.3.8 3450 Counting sequence

来源:互联网 发布:云视通摄像头监控软件 编辑:程序博客网 时间:2024/06/08 12:18

果然对树状数组的认识还madamadadane吧……稍微改下用法就纠结了。

Counting Sequences

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/65536 K (Java/Others)Total Submission(s): 71 Accepted Submission(s): 22 
Problem Description
For a set of sequences of integers{a1,a2,a3,...an}, we define a sequence{ai1,ai2,ai3...aik}in which 1<=i1<i2<i3<...<ik<=n, as the sub-sequence of {a1,a2,a3,...an}. It is quite obvious that a sequence with the length n has 2^n sub-sequences. And for a sub-sequence{ai1,ai2,ai3...aik},if it matches the following qualities: k >= 2, and the neighboring 2 elements have the difference not larger than d, it will be defined as a Perfect Sub-sequence. Now given an integer sequence, calculate the number of its perfect sub-sequence.
 
Input
Multiple test cases The first line will contain 2 integers n, d(2<=n<=100000,1<=d=<=10000000) The second line n integers, representing the suquence
 
Output
The number of Perfect Sub-sequences mod 9901
 
Sample Input
4 21 3 7 5
 
Sample Output
4
 

还没搞懂的树+从来没搞懂的dp+究竟什么是离散化啊混蛋,唉。光推数据就哭了好久,到底是什么嘛!

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define mod 9901#define maxn 100002int n;int dp[maxn];//how any valid subsequence before dp[i]int num[maxn];//recorde input numberint a[maxn];//input number after sortint c[maxn];int pos(int x)//binary, find the max position which <=x{    a[n+1]=1<<30;//a[n+1]is max    int l=1;    int r=n+1;    while(l<r)    {        int mid=(l+r)>>1;        if(a[mid]>x)//no =            r=mid;        else            l=mid+1;    }    return l-1;//if x<=0,return 0}int lowbit(int x){return -x&x;} void add(int i,int val){     while(i<=n)     {         c[i]+=val;         i+=lowbit(i);     }}int  sum(int i){     int s=0;     while(i>0)     {         s+=c[i];         i-=lowbit(i);     }     return s;}int main(){    int d;    while(scanf("%d%d",&n,&d)==2)    /*  scanf的返回值由后面的参数决定  scanf("%d%d", &a, &b);  如果a和b都被成功读入,那么scanf的返回值就是2  如果只有a被成功读入,返回值为1  如果a和b都未被成功读入,返回值为0  如果遇到错误或遇到end of file,返回值为EOF。  且返回值为int型.*/    {         for(int i=1;i<=n;i++)        {             scanf("%d",&num[i]);                a[i]=num[i];        }        sort(a+1,a+1+n);//i is start from 1        memset(c,0,sizeof(c));        dp[0]=0;        for(int i=1;i<=n;i++)        {            int ps=pos(num[i]);            //pos like hash            int k1=pos(num[i]-d-1);            //-1, those need to minus from k2            int k2=pos(num[i]+d);            //sum(i): number smaller than i have how many combination validly            int cnt=sum(k2)-sum(k1);            //cnt: how many combination is valid with i            //cout<<sum(k2)<<" "<<sum(k1)<<" ";            //cnt=(cnt%mod+mod)%mod;            cnt%=mod;            add(ps,cnt+1);            //even no valid combination, we have add a number which can be seen as a combination it self            dp[i]=(dp[i-1]+cnt)%mod;            //f**k dynamic programming!            //cout<<ps<<" "<<k1<<" "<<k2<<" "<<" "<<cnt<<" "<<endl;            //cout<<c[1]<<c[2]<<c[3]<<c[4]<<" "<<dp[i]<<endl;        }        printf("%d\n",dp[n]);    }    return 0;}                
思路。粘贴党as usual:
首先我们利用dp[i]表示到第i个位置能够找到的相邻数字之差小
于等于H的长度大于等于1的序列的总和,那么有状态转移方程
dp[i] = sum{ dp[j], j<i, abs(a[j]-a[i]) <= H },这个做法的时间
复杂度是O(n^2),但是n很大,所以不能采用,但是我们观察到这个转移
方程是以求和的形式出现,并且有一个限制条件就是abs(a[j]-a[i])<=H
,我们可以把它简写成a[i]-H <= a[j] <= a[i]+H,那么如果我们把数
字映射到下标,并且通过二分找到a[j]的范围,就可以轻松的通过树状
数组的成段求和来统计了。
    具体做法是:由于数字较大,我们可以先将所有数字离散化,这样
每个数字就有一个 <= n 的标号,然后这个标号就可以对应树状数组的
下标了,每次从左往右在树状数组中统计[a[i]-H, a[i]+H]的解的数量
(注意,这里需要找到离散后对应的数字),然后将当前数字(离散后
的数字)插入到树状数组中,值即为先前找到的节的数量,循环结束,
累加和就是序列大于等于1的解的数量,然后再减去n就是最后的答案了
,这里注意是取模,并且保证答案不能为负数。

sum就是找比这个数小的有多少种可用的组合(包括单数),sum(i+d)-sum(i-d-1)就是到 i 符合条件的组合(最后以 i 结束)。然后把符合的数字更新到c中,+1是考虑到新增了一个数,即使单数也算可用的组合。更新dp到 i 符合条件的组合(最后不一定以 i 结束)。这一段是自己写的呦~。。话说 i 君太小了不空格空开根本看不清啦啦啦啦!!


真的不觉得自己搞懂了。毛线啊。怀疑智商^10086。

而且今天又被妹子大人郁闷到了,虽然事后想想是自己在自讨没趣,唔……QmQ

原创粉丝点击