概述
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?
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 the minimal number of moves needed to make the sequence increasing.
4 2 1 3 3 2
3
解题说明:题目的要求是通过每次对数列中的某个数增加d,让这个数列从小到大按序排列,问最少增加多少次d。做法是从前往后依次遍历,判断前后两个数的差值,如果前一个数大于后面的数,增加后面的数值,统计次数即可。
#include <iostream>
#include<algorithm>
#include<cstdio>
#include <cstring>
#include<string>
using namespace std;
int main()
{
int n,m,i,d,a[100000],c=0;
scanf("%d%d",&n,&d);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
{
if(a[i]>=a[i+1])
{
m=(a[i]-a[i+1])/d;
m++;
c+=m;
a[i+1]+=m*d;
}
}
printf("%dn",c);
return 0;
}
最后
以上就是瘦瘦鞋垫为你收集整理的A. Increasing Sequence的全部内容,希望文章能够帮你解决A. Increasing Sequence所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复