Codeforces 466 D. Increase Sequence,codeforcesincrease

來源:互聯網
上載者:User

Codeforces 466 D. Increase Sequence,codeforcesincrease


一道不錯的想法題。。。。。

D. Increase Sequencetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

Peter has a sequence of integers a1, a2, ..., an. Peter wants all numbers in the sequence to equal h. He can perform the operation of "adding one on the segment [l, r]": add one to all elements of the sequence with indices from l to r (inclusive). At that, Peter never chooses any element as the beginning of the segment twice. Similarly, Peter never chooses any element as the end of the segment twice. In other words, for any two segments [l1, r1] and [l2, r2], where Peter added one, the following inequalities hold: l1 ≠ l2 and r1 ≠ r2.

How many distinct ways are there to make all numbers in the sequence equal h? Print this number of ways modulo 1000000007 (109 + 7). Two ways are considered distinct if one of them has a segment that isn't in the other way.

Input

The first line contains two integers n, h (1 ≤ n, h ≤ 2000). The next line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 2000).

Output

Print a single integer — the answer to the problem modulo 1000000007 (109 + 7).

Sample test(s)input
3 21 1 1
output
4
input
5 11 1 1 1 1
output
1
input
4 33 2 1 1
output
0

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;typedef long long int LL;const LL maxn=2200,MOD=1e9+7;LL a[maxn],b[maxn];int main(){int n,h;cin>>n>>h;for(int i=1;i<=n;i++) {cin>>a[i];a[i]=h-a[i];}for(int i=1;i<=n+1;i++){b[i]=a[i]-a[i-1];}LL ans=1,cnt=0;for(int i=1;i<=n+1;i++){if(b[i]==1){cnt++;}else if(b[i]==0){if(cnt) ans=(ans*(cnt+1))%MOD;}else if(b[i]==-1){ans=(ans*cnt)%MOD;cnt--;}else ans=0;}cout<<ans<<endl;return 0;}










聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.