HDU 3030 - Increasing Speed Limits

本文介绍了一种算法,用于解决特定条件下高速公路上车辆行驶速度限制子序列的问题。通过树状数组结合离散化的技术手段,实现了对递增子序列数量的有效计算。
Problem Description
You were driving along a highway when you got caught by the road police for speeding. It turns out that they\'ve been following you, and they were amazed by the fact that you were accelerating the whole time without using the brakes! And now you desperately need an excuse to explain that.

You've decided that it would be reasonable to say "all the speed limit signs I saw were in increasing order, that\'s why I've been accelerating". The police officer laughs in reply, and tells you all the signs that are placed along the segment of highway you drove, and says that's unlikely that you were so lucky just to see some part of these signs that were in increasing order.

Now you need to estimate that likelihood, or, in other words, find out how many different subsequences of the given sequence are strictly increasing. The empty subsequence does not count since that would imply you didn't look at any speed limits signs at all!

For example, (1, 2, 5) is an increasing subsequence of (1, 4, 2, 3, 5, 5), and we count it twice because there are two ways to select (1, 2, 5) from the list.
 
Input
The first line of input gives the number of cases, N. N test cases follow. The first line of each case contains n, m, X, Y and Z each separated by a space. n will be the length of the sequence of speed limits. m will be the length of the generating array A. The next m lines will contain the m elements of A, one integer per line (from A[0] to A[m-1]).

Using A, X, Y and Z, the following pseudocode will print the speed limit sequence in order. mod indicates the remainder operation.

for i = 0 to n-1
print A[i mod m]
A[i mod m] = (X * A[i mod m] + Y * (i + 1)) mod Z

Note: The way that the input is generated has nothing to do with the intended solution and exists solely to keep the size of the input files low.

1 ≤ m ≤ n ≤ 500 000
 
Output
For each test case you should output one line containing "Case #T: S" (quotes for clarity) where T is the number of the test case and S is the number of non-empty increasing subsequences mod 1 000 000 007.
 
Sample Input
2 5 5 0 0 5 1 2 1 2 3 6 2 2 1000000000 6 1 2
 
Sample Output
Case #1: 15 Case #2: 13

大致题意:

  求上升子序列的个数

  序列怎么出来的呢,好难懂:

    for i = 0 to n-1
    print A[i mod m]
    A[i mod m] = (X * A[i mod m] + Y * (i + 1)) mod Z

  取m=3为例 输入完a[] 以后,a[]不是序列 要按照他的循环 打印 a[0] ,a[1], a[2],a[0],a[1],a[2]....如此,每打印一个做一次A第三行的变换,最后就是0 -> n-1 的序列了。

解题思路:

  树状数组+离散化。

  动规求法: dp[i]=∑dp[j](j<i&&ans[j]<ans[i])

  依据树状数组快速统计可加类区间数据的应用,

  可转化成 dp[i]=sum(f[i]-1)+1 ;即以前f[i]-1个数据为底的个数再加上自身。

 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <cstring>
 4 using namespace std;
 5 #define N  500005
 6 #define mod 1000000007
 7 long long c[N],a[N],b[N],f[N],T,n,m,x,y,z,ans,size;
 8 void modify(int x,int num){while(x<=n)c[x]+=num,c[x]%=mod,x+=x&-x;}
 9 long long sum(int x){int s=0;while(x>0)s+=c[x],s%=mod,x-=x&-x;return s;}
10 void ini(){
11     memset(c,0,sizeof(c));
12     ans=0;
13     scanf("%d%d%lld%lld%lld",&n,&m,&x,&y,&z);
14     for(int i=0;i<m;i++) scanf("%lld",&a[i]);
15     for(int i=0;i<n;i++){
16         f[i]=b[i+1]=a[i%m];
17         a[i%m]=(x*a[i%m]+y*(i+1))%z; 
18     }
19 }
20 int main(){
21     scanf("%d",&T);
22     for(int K=1;K<=T;K++)
23     {
24         ini();
25         sort(b+1,b+n+1);//离散化 
26         size=unique(b+1,b+n+1)-(b+1);
27         for(int i=0;i<n;i++){
28             int p=lower_bound(b+1,b+size+1,f[i])-b;
29             long long tot=sum(p-1)+1;
30             ans+=tot;
31             ans%=mod;
32             modify(p,tot); 
33         } printf("Case #%d: %lld\n",K,ans);
34     } return 0;
35 }

 

转载于:https://www.cnblogs.com/nicetomeetu/p/5170327.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值