11AIncreasing Sequence

来源:互联网 发布:用mac输入英文破折号 编辑:程序博客网 时间:2024/06/05 17:00

模拟+小技巧

题意给你一个长度小于等于2000的序列,你可以给任何一个元素加上不限次数给定的d值,让b序列变成严格的递增序列。如果单纯暴力,b[n-1]<=b[i]时,就给b[i]+=d就会超时。所以我们需要定义一个x保存他们的差值,因为当前差值可能不是d的倍数,不能直接把x家在b[i]上,所以看看能加几个整数倍的d,最后再来暴力,30ms过。


                                    A. Increasing Sequence                                        time limit per test1 second                                        memory limit per test64 megabytes                                        inputstandard input                                        outputstandard 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.

Examples
input
4 2
1 3 3 2
output
3

#include<cstdio>#include<iostream>#include<algorithm>using namespace std;int main(){    int n,d,ans;    int b[2010];    while(cin>>n>>d)    {        ans=0;        for(int i=0; i<n; i++)        {            cin>>b[i];            if(i>=1&&b[i]<=b[i-1])            {                int x=b[i-1]-b[i];                b[i]+=x/d*d;                ans+=x/d;                while(b[i]<=b[i-1])                {                    b[i]+=d;                    ans++;                }            }        }        cout<<ans<<endl;    }    return 0;}
原创粉丝点击