您的位置:首页 > 产品设计 > UI/UE

【水贪心】#11 A. Increasing Sequence

2014-04-25 01:31 357 查看
A. Increasing Sequence

time limit per test
1 second

memory limit per test
64 megabytes

input
standard input

output
standard output

A sequence a0, a1, ..., at - 1 is
called increasing if ai - 1 < ai for
each i: 0 < i < t.

You are given a sequence b0, b1, ..., bn - 1 and
a positive integer d. In each move you may choose one element of the given sequence and add d to
it. What is the least number of moves required to make the given sequence increasing?

Input

The first line of the input contains two integer numbers n and d (2 ≤ n ≤ 2000, 1 ≤ d ≤ 106).
The second line contains space separated sequence b0, b1, ..., bn - 1 (1 ≤ bi ≤ 106).

Output

Output the minimal number of moves needed to make the sequence increasing.

Sample test(s)

input
4 2
1 3 3 2


output
3


一道模拟题,其实直接模拟就出了,不过用一下除法来提升效率还是养成习惯的好。

贪心:遍历一次,每个当前数比自己前一个数大即可

C++:

#include <algorithm>
#include <iostream>
#include <cstdio>
using namespace std;

int main()
{
	int n,d,cnt=0;
	scanf("%d%d",&n,&d);
	int val
;
	for(int i=0;i<n;i++)
		scanf("%d",&val[i]);
	
	for(int i=1;i<n;i++)
	{
			if(val[i]>val[i-1])continue;
			else 
			{
				int temp=val[i-1]-val[i];
				val[i]+=(temp/d+1)*d;
				cnt+=(temp/d+1);
			}
		
	}
	printf("%d",cnt);
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: