c++ - How to make this code shorter to do faster -
I am a new C ++ learner. I have logged in the Codeforces site and this is the 11th question: A. The order A0, a1, ..., is increasing at 1 if AI-1 and Lt; For each eye AI: 0 You have given sequence B, B1, ..., BN-1 and a positive integer D. In each trick, you can select an element of the given sequence and add D to it. What is the minimum number of moves required to increase the given sequence? In the first line of input input, there are two integer n and d (2 ≤ n ≤ 2000, 1 ≤ d ≤ 106). In the second line, space is a different sequence b0, b1, ..., bn-1 (1 ≤ bi ≤ 106). The output of the minimum trick required to increase the sequence. I will write this code for this question: This works fine. But enter a test codeforces server 2000 number. The time limit is 1 second. But calculate it up to 1 second. How to reduce this code to calculate faster?
#include & lt; Iostream & gt; using namespace std; Int main () {long long int n, d, ci, i, s; S = 0; CIN & gt; & Gt; N & gt; & Gt; D; Int a [n]; For {CI = 0; ci & lt; n; ci ++} {cin & gt; & Gt; A [ci]; } (I = 0; i
While you can get rid of the loop. Your program should not run without
#include & lt; Iostream & gt; using namespace std; Int main () {long int n, d, ci, i, s; S = 0; CIN & gt; & Gt; N & gt; & Gt; D; Int a [n]; For {CI = 0; ci & lt; n; ci ++} {cin & gt; & Gt; A [ci]; } (I = 0; i gt; = a [i + 1]) {int x = ((a [i] - one [I + 1]) / d) + 1; S + = x; A [i + 1] = x * d; }} Cout & lt; & Lt; S; Return 0; }
Comments
Post a Comment