码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-05-07 18:28:10      阅读:475      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   class   code   java   

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 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
 

 

Source
 

 

Recommend
gaojie   |   We have carefully selected several similar problems for you:  3450 2227 2642 1255 3743 
 

 

在奔溃的边缘a了,搞了近一天= =!!

题意开始也没弄懂,后来知道是由一个数组推出目标数组(s[]):

for(int i=0;i<m;i++)
    scanf("%d",&a[i]);
for(int i=0;i<n;i++){
    s[i]=a[i%m];
    t[i]=s[i]; //用于离散化处理
    a[i%m]=(x*a[i%m]+y*(i+1))%z;
}

然后离散化处理:

sort(t,t+n);
cnt=0;
a[++cnt]=t[0];
for(int i=1;i<n;i++){
    if(t[i]!=t[i-1]){
        a[++cnt]=t[i];
    }
}

此处是简单的处理,实际运用则是在二分查找里(search());

最后处理目标数组s[],由dp思想可以从前状态推出后状态!然后用树状数组实现,时间复杂度就是O(n*lgn),贡献了了好多次TLE,一开始用map,后来才换成二分,map耗时较大,明白了简单的不一定好,出来混迟早要还的!!

bubuko.com,布布扣
 1 //2218MS    8136K    1668 B    G++    
 2 #include<iostream>
 3 #include<map>
 4 #include<algorithm>
 5 #define M 1000000007
 6 #define N 500005
 7 #define ll __int64
 8 using namespace std;
 9 int c[N],a[N],t[N],s[N];
10 int cnt;
11 inline int lowbit(int k)
12 {
13     return (-k)&k;
14 }
15 inline void update(int k,int detal)
16 {
17     for(int i=k;i<=cnt;i+=lowbit(i)){
18         c[i]+=detal;
19         if(c[i]>=M) c[i]%=M;
20     }
21 }
22 inline int getsum(int k)
23 {
24     int s=0;
25     for(int i=k;i>0;i-=lowbit(i)){
26         s+=c[i];
27         if(s>=M) s%=M;
28     }
29     return s;
30 }
31 inline int search(int a0[],int m)
32 {
33     int l=1,r=cnt,mid;
34     while(l<r){
35         mid=(l+r)>>1;
36         if(a0[mid]<m) l=mid+1;
37         else r=mid;
38     }
39     return l;
40 }
41 int main(void)
42 {
43     int cas,n,m,k=1;
44     ll x,y,z;
45     scanf("%d",&cas);
46     while(cas--)
47     {
48         memset(c,0,sizeof(c));
49         scanf("%d%d%I64d%I64d%I64d",&n,&m,&x,&y,&z);
50         for(int i=0;i<m;i++)
51             scanf("%d",&a[i]);
52         for(int i=0;i<n;i++){
53             s[i]=a[i%m];
54             t[i]=s[i];
55             a[i%m]=(x*a[i%m]+y*(i+1))%z;
56         }   
57         sort(t,t+n);
58         cnt=0;
59         //map<int,int>Map;
60         //Map[t[0]]=++cnt;
61         a[++cnt]=t[0];
62         for(int i=1;i<n;i++){
63             if(t[i]!=t[i-1]){
64                 //Map[t[i]]=++cnt;
65                 a[++cnt]=t[i];
66             }
67         }
68         ll ans=0;
69         update(1,1);
70         for(int i=0;i<n;i++){
71             int id=search(a,s[i]); //离散化的二分查找 
72             int temp=getsum(id); 
73             ans+=temp; //dp思想 
74             if(ans>=M) ans%=M;
75             update(id+1,temp);
76         }
77         printf("Case #%d: %I64d\n",k++,ans);
78     }
79     return 0;
80 }
bubuko.com,布布扣

 

hdu 3030 Increasing Speed Limits (离散化+树状数组+DP思想),布布扣,bubuko.com

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

标签:des   style   blog   class   code   java   

原文地址:http://www.cnblogs.com/GO-NO-1/p/3709887.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!