hdu 2836 Traversal

來源:互聯網
上載者:User

標籤:des   style   blog   color   java   os   strong   io   

Traversal

Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 722    Accepted Submission(s): 268


Problem DescriptionI arrive at a big lake and I have to cross it. Luckily, I’m a very good jumper, but the lake is too big to be crossed in one jump. On the shore, I find N boxes of different heights, set in a certain order. If I throw a box into the lake, it will float and it will have the same height as on the shore. This is good, because I intend to throw some boxes into the lake and get from one shore to the other by jumping from box to box. The only things to consider are:
The lake is big, so I must throw at least 2 boxes, which means that in order to cross the lake I have to make at least 3 jumps.
Not all the boxes have to be thrown; some of them may be ignored.
The boxes can be thrown into the lake only in the order they are found on the shore and I have to jump on them in this order.
The height difference between two consecutive boxes I use must be at most H meters, because I can jump a lot in length, but I have some problems with jumping in height.The height of a box doesn’t change when I jump on it.
I’m always able to jump from the shore to a box and from a box to the shore, no matter what the height of the box is.

Facing so many possibilities that respect the above conditions, I begin counting the number of possibilities that I have, instead of actually crossing the lake. I quickly find the answer and I wonder whether you can also find it as fast as I did.

Task

Write a program that determines the number of possibilities to cross the lake in the above conditions. Since the number can be quite big, you only have to output the remainder of this number, when divided by 9901. 

 

InputThere are multiple test cases. Each test case contains two integers N and H, separated by a space, representing the number of boxes and the maximum height difference between two consecutive boxes thrown into the lake. The following N lines contain the heights of the boxes, in the order the boxes are set on the shore. The (i+1)th line contains the height of the ith box.  

 

OutputFor each test case you should output a single line, containing the number of possibilities modulo 9901.

Constraints

1 < N < 100 001
0 < H < 100 000 001
The height of any box is a strictly positive integer and does not exceed 100 000 000 

 

Sample Input4 21 3 7 5 

 

Sample Output4還是太弱,弄一題就得找題解。。。。
 1 #include<iostream> 2 #include<string> 3 #include<cstdio> 4 #include<vector> 5 #include<queue> 6 #include<stack> 7 #include<algorithm> 8 #include<cstring> 9 #include<stdlib.h>10 #include<string>11 #include<cmath>12 #include<map>13 using namespace std;14 #define mod 990115 #define pb push_back16 #define LL __int6417 #define mmax 100000+1018 LL p[mmax],love[mmax],fuck[mmax],cnt,n,m;19 int search1(LL x){     //找到>=x的第一個數20     int l=1,r=cnt,mid,tmp=cnt+1;21     while(l<=r){22         mid=(l+r)>>1;23         if(fuck[mid]>=x) r=mid-1,tmp=min(tmp,mid);24         else l=mid+1;25     }26     return tmp;27 }28 int search2(LL x){     //找到<=x的最後一個數29     int l=1,r=cnt,mid,tmp=0;30   //  cout<<"x="<<x<<endl;31     while(l<=r){32         mid=(l+r)>>1;33         if(fuck[mid]<=x){34               //  cout<<"tmp="<<tmp<<endl;35                 l=mid+1,tmp=max(tmp,mid);36               //  cout<<"tmp="<<tmp<<endl;37         }38         else r=mid-1;39     }40     return tmp;41 }42 void update(int pos,int x){43     while(pos<=cnt){44         p[pos]+=x;45         p[pos]%=mod;46         pos+=pos&(-pos);47     }48 }49 int getnum(int pos){50     LL sum=0;51     while(pos>=1){52         sum+=p[pos];53         pos-=pos&(-pos);54     }55     return sum%mod;56 }57 int main(){58  #ifndef ONLINE_JUDGE59         freopen("input.txt","r" ,stdin);60     #endif // ONLINE_JUDGE61     while(cin>>n>>m){62         cnt=0;63         for(int i=1;i<=n;i++) scanf("%I64d",&love[i]);64         fuck[++cnt]=love[1];65         for(int i=2;i<=n;i++) if(love[i]!=love[i-1]) fuck[++cnt]=love[i];66         sort(fuck+1,fuck+1+cnt);67         LL sum=0;memset(p,0,sizeof(p));68         for(int i=1;i<=n;i++){69             int a=search1(love[i]-m),b=search2(love[i]+m),c=search1(love[i]);70             int tmp=getnum(b)-getnum(a-1);71             //tmp%=mod; 不著為啥下面的改成這樣就錯了72             tmp=(tmp%mod+mod)%mod;73             sum+=tmp;74             sum%=mod;75             update(c,tmp+1);76         }77         cout<<sum<<endl;78     }79 }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.