hdu 3450(树状数组+dp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=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 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 2
1 3 7 5

Sample Output

4

Source

2010 ACM-ICPC Multi-University Training Contest(2)——Host
by BUPT

Recommend

We have carefully selected several similar problems for you:  3030 1542 1255 2688 3449

题意:一个集合有n个数,然后要你找出所有的完美子序列对9901取模~

思路:很容易想到dp,我们可以用dp[i]表示以第i个数为结尾的完美子序列的个数,那么sum(总的完美子序列的个数)=dp[2]+dp[3]+dp[4]+.....dp[n];

然后接着想满足完美子序列的条件是什么? |x-a[i]|<=d  那么就有      a[i]-d <=x <=a[i]+d;

然后用树状数组动态维护即可~~~

#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;
}
时间: 2024-10-12 20:16:32

hdu 3450(树状数组+dp)的相关文章

hdu 3450 树状数组优化dp

题意就不说了: hdu2227差不多只不过这道题不是递增  而是相邻差不超过d   其实是为都一样,  也有差别: 这道题没说范围  并且树之间的大小关系对结果有影响,所以树状数组里根据原来id来求,由于数值很大 所以还是用到离散化  这里的离散化感觉和映射差不多,离散化用来查找id: 当num[i]的id是x是,dp[i]表示方案数  很明显dp[i]=sun(dp[j],j<i&&abs(num[j]-num[i])<=d)  这里在树状数组里面就是求sum[1到num[i

hdu 2227 树状数组+dp

题意是求一个数列的不递减的子序列的个数: 很显然,如果只用dp来做的话时间是O(n*n) 因为dp[i]为前i个数可能的方案,则状态转移方程为dp[i]=sum(dp[j],j<i&&num[j]<=num[i])+1 对小数据坑定能过 但大数据就超时了,这里用到了树状数组来优化: 先对num按数来进行排序,(这道题因为数据较大  用到了离散化) 因为更新是是按原序更新的,及i之前的num[j]一定比num[i]小,维护了不递减的原则,更新是从mark[i](表示原序列i在排序

hdu 3030 Increasing Speed Limits (离散化+树状数组+DP思想)

Increasing Speed Limits Time Limit: 2000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 481    Accepted Submission(s): 245 Problem Description You were driving along a highway when you got caught by the road p

hdu 3333 树状数组+离线处理

http://acm.hdu.edu.cn/showproblem.php?pid=3333 不错的题,想了很久不知道怎么处理,而且答案没看懂,然后找个例子模拟下别人的代码马上懂了---以后看不懂的话就拿个例子模拟下别人的代码 举个例子:1 3 3 5 3 5 查询 a, 2 4 b, 2 5 最初是这么想的:对于a查询,倘若把第二个数第三个数变成1个3,那么到b查询,又出现了两个3,再做处理似乎还是O(n),而且如果先出现2,5查询,后出现2,4查询,那么还需要把删除的数补回来.....o(╯

Hdu 3887树状数组+模拟栈

题目链接 Counting Offspring Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1757    Accepted Submission(s): 582 Problem Description You are given a tree, it’s root is p, and the node is numbered fr

HDU 1754 树状数组 解法

mnesia在频繁操作数据的过程可能会报错:** WARNING ** Mnesia is overloaded: {dump_log, write_threshold},可以看出,mnesia应该是过载了.这个警告在mnesia dump操作会发生这个问题,表类型为disc_only_copies .disc_copies都可能会发生. 如何重现这个问题,例子的场景是多个进程同时在不断地mnesia:dirty_write/2 mnesia过载分析 1.抛出警告是在mnesia 增加dump

HDU 1166(树状数组)

用树状数组把HDU1166再写了一次  感觉树状数组简洁 1 #include <cstdio> 2 #include <iostream> 3 #include <string.h> 4 using namespace std; 5 int c[50002],lv[50002],n; 6 int lowbit(int x){return x&(-x);} 7 int sum(int b){ 8 int sum=0; 9 while(b>0){ 10 su

hdu 4368 树状数组 离线维护

http://acm.hdu.edu.cn/showproblem.php?pid=4638 Problem Description There are n men ,every man has an ID(1..n).their ID is unique. Whose ID is i and i-1 are friends, Whose ID is i and i+1 are friends. These n men stand in line. Now we select an interv

hdu4455之树状数组+DP

Substrings Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1571    Accepted Submission(s): 459 Problem Description XXX has an array of length n. XXX wants to know that, for a given w, what is