Breaking

Friday 14 July 2017

Increasing Sequence

Increasing Sequence

A sequence a, a1, ..., at - 1 is called increasing if ai - 1 < ai for each i: 0 < i < t.
You are given a sequence b, 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 Format
The first line of the input contains two integer numbers n and d (2 ≤ n ≤ 20, 1 ≤ d ≤ 103). The second line contains space separated sequence b, b1, ..., bn - 1 (1 ≤ bi ≤ 103).
Output Format
In the 1st line, output the minimal number of moves needed to make the sequence increasing.
In the second line, print the integers in the increasing sequence, separated by a space. There is a trailing space at the end of the line

Sample Input
4 2
1 3 3 2
Sample Output
3
1 3 5 6


Code:

#include<stdio.h>

int main()

{

int n,k,prev,next,m=0,i;
scanf("%d%d",&n,&k);
int arr[n];
scanf("%d",&prev);
arr[0]=prev;
for(i=1;i<n;i++)
    {
        scanf("%d",&next);
        
             while(prev>=next)
             {m++;
                 next+=k;
             }
        
        prev=next;
        arr[i]=next;
    }
printf("%d\n",m);
for(i=0;i<n;i++)
{
    printf("%d ",arr[i]);
}    
return 0;
}

No comments:

Post a Comment

Like