codeforces 466d Increase Sequence
1 second
256 megabytes
standard input
standard 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.
The first line contains two integers n, h (1 ≤ n, h ≤ 2000). The next line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 2000).
Print a single integer — the answer to the problem modulo 1000000007 (109 + 7).
3 2
1 1 1
4
5 1
1 1 1 1 1
1
4 3
3 2 1 1
0
题意:给出一个序列,可以对其进行以下操作,取一段长度,从l到r,将其中的数全部加一。操作的次数不限,但对于两个操作,要求l1!=l2 && r1!=r2。求共有几种操作集合,使得整个序列的值最后都等于h。
思路:想了好久还是想不出来,最后看的题解。反着来做,先将所有a[i]=h-a[i]。当然,为了后面方便计算,我们将边界条件a[0]=0,a[n+1]=0。由于操作中的限制(l1!=l2 && r1!=r2),所以我们很容易得出有解的情况是当且仅当-1<=a[i]-a[i-1]<=1。我们用变量cnt记录当前有多少个左端点l没有和右端点r配对。下面分类讨论:
1,a[i]-a[i-1]==1,此时i点上必定有一个左端点,所以cnt++,i-1点上没有右端点,所以不对ans进行操作。
2,a[i]==a[i-1],此时有两种情况,要嘛i点没有左端点,i-1点没有右端点,要嘛i点有左端点,i-1点有右端点。此时,第一种情况是cnt=0,第二种情况时,cnt++,ans×=cnt,cnt--。所以两种情况都可以用ans×=(cnt+1)来表示。
3,a[i]-a[i-1]=-1,此时i点没有左端点,i-1点必定有右端点,此时ans×=cnt,cnt--;
细节:因为我们统计右端点的情况是统计前一个的,所以循环的时候必须做到n+1位。
下面是代码
/*
* Author: Joshua
* Created Time: 2014年09月19日 星期五 20时36分35秒
* File Name: d.cpp
*/
#include<cstdio>
#include<iostream>
using namespace std;
#define maxn 2002
typedef long long LL;
int a[maxn],b[maxn],n,h; void solve()
{
int x;
for (int i=1;i<=n;++i)
{
scanf("%d",&x);
a[i]=h-x;
}
a[0]=0;a[n+1]=0;
for (int i=1;i<=n+1;++i)
{
b[i]=a[i]-a[i-1];
if (b[i]>1 || b[i]<-1)
{
printf("0\n");
return;
}
}
LL ans=1;
int mod = int (1e9+7),cnt=0;
for (int i=1;i<=n+1;++i)
{
if (b[i]==1)
{
cnt++;
continue;
}
if (b[i]==0) ans*=(cnt+1);
else ans*=(cnt--);
ans %=mod;
}
cout<<ans<<endl;
} int main()
{
while (scanf("%d%d",&n,&h)>0)
solve();
return 0;
}
改进:我们知道,当a[i]-a[i-1]==1的时候,cnt++,当a[i]-a[i-1]==-1的时候,cnt--,那么我们是不是就可以用a[i]来表示cnt呢,答案是可以。这样可以进一步降低时间复杂度,代码也更短,下面是改进后的代码。
/*
* Author: Joshua
* Created Time: 2014年09月19日 星期五 20时36分35秒
* File Name: d.cpp
*/
#include<cstdio>
#include<iostream>
using namespace std;
#define maxn 2002
typedef long long LL;
int a[maxn],b[maxn],n,h; void solve()
{
int x;
for (int i=1;i<=n;++i)
{
scanf("%d",&x);
a[i]=h-x;
}
a[0]=0;a[n+1]=0;
for (int i=1;i<=n+1;++i)
{
b[i]=a[i]-a[i-1];
if (b[i]>1 || b[i]<-1)
{
printf("0\n");
return;
}
}
LL ans=1;
int mod = int (1e9+7);
for (int i=1;i<=n+1;++i)
{
if (b[i]==1) continue;
if (b[i]==0) ans*=a[i]+1;
else ans*=a[i-1];
ans %=mod;
}
cout<<ans<<endl;
} int main()
{
while (scanf("%d%d",&n,&h)>0)
solve();
return 0;
}
codeforces 466d Increase Sequence的更多相关文章
- Codeforces 601B. Lipshitz Sequence(单调栈)
Codeforces 601B. Lipshitz Sequence 题意:,q个询问,每次询问给出l,r,求a数组[l,r]中所有子区间的L值的和. 思路:首先要观察到,斜率最大值只会出现在相邻两点 ...
- 【codeforces 466D】Increase Sequence
[题目链接]:http://codeforces.com/problemset/problem/466/D [题意] 给你n个数字; 让你选择若干个区间; 且这些区间[li,ri]; 左端点不能一样; ...
- Codeforces 626A Robot Sequence(模拟)
A. Robot Sequence time limit per test:2 seconds memory limit per test:256 megabytes input:standard i ...
- Curious Array Codeforces - 407C(高阶差分(?)) || sequence
https://codeforces.com/problemset/problem/407/C (自用,勿看) 手模一下找一找规律,可以发现,对于一个修改(l,r,k),相当于在[l,r]内各位分别加 ...
- codeforces 622A Infinite Sequence
A. Infinite Sequence time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces 626A Robot Sequence
A. Robot Sequence time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- CodeForces 623E Transforming Sequence 动态规划 倍增 多项式 FFT 组合数学
原文链接http://www.cnblogs.com/zhouzhendong/p/8848990.html 题目传送门 - CodeForces 623E 题意 给定$n,k$. 让你构造序列$a( ...
- CodeForces 601B Lipshitz Sequence
Lipshitz Sequence 题解: 可以通过观察得到,对于任意一个区间来说, 只有相邻的2个点的差值才会是区间的最大值. 具体观察方法,可以用数学分析, 我是通过画图得到的. 那么基于上面的观 ...
- CF思维联系–CodeForces -224C - Bracket Sequence
ACM思维题训练集合 A bracket sequence is a string, containing only characters "(", ")", ...
随机推荐
- if __name__ == '__main__' 如何正确理解
今天有个初学Python 的朋友问我这个问题,他说在网上好多回答他都不太理解.所以这里我来做说一下,希望能把这个问题说明白. 先举一个例子:假设我们有一个add.py文件,里面的代码是这样: def ...
- java数组排序(冒泡、直排)反转
package lianxi; public class maopao { public static void main(String[] args){ int[] i=new int[]{45,6 ...
- UglifyJS-- 对你的js做了什么
也不是闲着没事去看压缩代码,但今天调试自己代码的时候发现有点意思.因为是自己写的,虽然压缩了,格式化之后还是很好辨认.当然作为min的首要准则不是可读性,而是精简.那么它会尽量的缩短代码,尽量的保持一 ...
- (转).tar.gz文件和.rpm文件的区别
场景:在Linux环境下安装软件时候总是会遇到安装软件格式的选择,以及安装. 1 软件的二进制分发 Linux软件的二进制分发是指事先已经编译好二进制形式的软件包的发布形式, 其优点是安装使用容易,缺 ...
- Java 程序员技能导图 1.0
做Java开发已经一年,并非科班出身,在毕业工作三年后毅然决然辞职,参加培训机构从零开始.在这期间迷茫.失望.绝望时常伴我左右,但是在不断自我提高与努力中渐渐看到一些小小的成果使我不断坚信自己的选择并 ...
- Android学习笔记-开关按钮ToggleButton和开关Switch
本节给大家介绍的Android基本UI控件是:开关按钮ToggleButton和开关Switch,这两个其实都是开关组件,只是后者需要在Android 4.0以后才能使用 所以AndroidManif ...
- maven相关的学习资料
1, maven的settings配置文件详解: http://blog.csdn.net/jinshuaiwang/article/details/23686099 2,maven原理---翡青的博 ...
- 蓝桥杯比赛关于 BFS 算法总结方法以及套路分析
首先我们来看几道java A组的题目,都是同一年的哦!!! 搭积木 小明最近喜欢搭数字积木,一共有10块积木,每个积木上有一个数字,0~9. 搭积木规则:每个积木放到其它两个积木的上面,并且一定比下面 ...
- 防止DDoS攻击的方式
针对企业的DDoS攻击持续增长,根据Akamai的调查报告,2015年DDoS攻击增长了史无前例的180% !面对 DDoS 攻击性挑战,我们应该建立有效的防御体系来抵御攻击. 1.网站IP减少公 ...
- linux指令大全
系统信息 arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r 显示正在使用的内核版本 dmidecode -q 显示硬件系统部件 - (SMBIOS ...