HDU 3450 (tree array + dp)

Source: Internet
Author: User

Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 3450

Counting Sequences Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/65536 K (Java/Others)
Total submission (s): 1815 accepted submission (s): 618


Problem descriptionfor 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.
 
Inputmultiple 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
Outputthe Number of perfect sub-sequences mod 9901
 
Sample Input
4 21 3 7 5
 
Sample output
4
 
Source2010 ACM-ICPC multi-university training Contest (2) -- host by BUPT
Recommendwe have carefully selected several similar problems for you: 3030 1542 1255 2688 3449 question: a set has n numbers, and then you need to find all the perfect sub-sequences to modulo 9901 ~ Idea: it is easy to think of DP. We can use DP [I] to represent the number of perfect subsequences ending with the number of I, so sum (the total number of perfect subsequences) = DP [2] + dp [3] + dp [4] + ..... DP [N]; then what are the conditions for satisfying the perfect subsequence? | X-A [I] | <= d then there is a [I]-D <= x <= A [I] + D; then, use the tree array for Dynamic Maintenance ~~~
#include <iostream>#include <stdio.h>#include <string>#include <string.h>#include <cstdio>#include <algorithm>#include <cmath>const int N=1e5+100;const int mod=9901;using namespace std;struct node{ int v,id;}a[N];bool cmp(node a,node b){ return a.v<b.v;}int n,d,c[N],b[N];int lowbit(int x){  return x&(-x);}void update(int x,int d){  while(x<=n)  {   c[x]+=d;   if(c[x]>mod)c[x]%=mod;   x+=lowbit(x);  }}int getsum(int x){  int ans=0;  while(x>0)  {   ans+=c[x];   if(ans>mod)ans%=mod;   x-=lowbit(x);  }  return ans;}int find(int x){  if(x>=a[n].v)return n;  if(x<a[1].v)return 0;  int l=1,r=n,ret=0;  while(l<=r)  {   int mid=(l+r)/2;   if(x>=a[mid].v)    {      ret=mid;      l=mid+1;    }   else    r=mid-1;  }  return ret;}int main(){        while(scanf("%d%d",&n,&d)!=EOF)        {          memset(c,0,sizeof(c));          memset(b,0,sizeof(b));          for(int i=1;i<=n;i++)          {            scanf("%d",&a[i].v);            a[i].id=i;          }          sort(a+1,a+1+n,cmp);          for(int i=1;i<=n;i++)b[a[i].id]=i;          int sum=0;          for(int i=1;i<=n;i++)          {            int p=find(a[b[i]].v+d);            int q=find(a[b[i]].v-d-1);            int temp=getsum(p)-getsum(q);                temp=(temp+mod)%mod;                sum=(sum+temp)%mod;                update(b[i],temp+1);          }          printf("%d\n",sum);        }        return 0;}


HDU 3450 (tree array + dp)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.