Finding decreasing sequences in an array of numbers Ants
  
  Requirement
  
  Given a natural number \$n\$, representing the number of days the study is made, and then a number \$x\$ of \$n\$ natural numbers, representing the number of working ant flies after food, where the \$x_i\$ number of working ants is the day \$i\$. It is required to determine the number of maximum sequences, ordered strictly decreasing after the number of divisors, of minimum length \$2\$.
  
  Input data
  
  The input file furnici.in contains two lines. On the first line is the natural number \$n\$, with the meaning of the requirement and on the second line, the elements of the string \$x\$, with the meaning in the requirement.
  
  Output data
  
  The output file furnici.out will contain a natural number representing the number of maximum sequences, of minimum length \$2\$, ordered strictly downwards by the number of divisors.
  
  Restrictions and clarifications
  
  Time Limit - 0.5 seconds
  Memory limit - 64 MB Total / 8 MB Stack
  \$2 \leq n \leq 100000 \$
  \$10 \leq x_i \leq 1.000.000.000\$
  
  Example
  
  furnici.in

10
719 169 4065 813 289 101 123 516 516 1017

  
  furnici.out

2

  
  Explanation
  
  Row number of divisions is \$2\ 3\ 8\ 4\ 3\ 2\ 4\ 12\ 12\ 6\$. 
  It is noted that there are two maximum sequences, of at least two, strictly decreasing, \$8\ 4\ 3\ 2\$ and \$12\ 6\$.


My program is given an array of numbers that it has to find the divisors for and then replace the number in the array with its corresponding number of divisors. In the new created array, it will have to find and count all the descending subsequences that occur.

Here is my code:

#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;cmath&gt;
using namespace std;
int n,i,v[100001],nr,j,s;
int main()
{
    ifstream in("furnici.in");
    ofstream out("furnici.out");
    in&gt;&gt;n;
    for(i=1; i&lt;=n; i++)
    {
        in&gt;&gt;v[i];
        nr=2;
        for(j=2; j&lt;=sqrt(v[i]); j++)
        {
            if(v[i]%j==0)
            {
                nr++;
                if(j!=v[i]/j)
                    nr++;
            }
        }
        v[i]=nr;
        if(v[i]&lt;v[i-1])
        {
            j=i;
            while(v[j]&lt;v[j-1] &amp;&amp; j&lt;n)
            {
                j++;
                nr=2;
                in&gt;&gt;v[j];
                for(int k=2; k&lt;=sqrt(v[j]); k++)
                {
                    if(v[j]%k==0)
                    {
                        nr++;
                        if(k!=v[j]/k)
                            nr++;
                    }
                }
                v[j]=nr;
            }
            s++;
            i=j;
        }
    }
    out&lt;&lt;s;
    return 0;
}


furnici.in

10
719  169 4065  813  289  101 123  516  516  1017


furnici.out

2


Explained:


10 represents how many numbers are going to be read from the file;
number 719 has 2 divisors, so 719 gets replaced with 2;
number 169 has 3 divisors, so 169 gets replaced with 3;
number 4065 has 8 divisors, so 4065 gets replaced with 8;
and so on, you kind of get the point. The resulting array will have
descending sequences: 2 3 8 4 3 2 4 12 12 6 and these need to be counted.


The issue with it is that it exceeds the time limit in the last test given to it, but otherwise the answer is correct.
Some general remarks:


Don't use namespace std;, see for example Why is “using namespace std;” considered bad practice?.
Define variables at the narrowest possible scope. In particular, avoid
global variables.
Use better variable names. It is unclear what each variable in

int n,i,v[100001],nr,j,s;


stands for.
return 0; at the end of the main program can be omitted.
The range of integer types is implementation defined, the C++ standard only
guarantees that a (signed) int can hold values from -32767 to 32767,
which is too small for your numbers. Many compilers define int as a 32-bit
integer, but you can use long to be on the safe side, or use fixed-size
types like int32_t.


With respect to readability, I recommend to leave more (horizontal) space,
e.g. around operators and parentheses.

There are two places with identical code to count the divisors
of a number. This should be done in a separate function.

Your code uses a nested loop where the inner loop updates the index of the
outer loop. That is difficult to understand and error-prone. And it is 
not necessary: Instead of starting a nested loop when the start of a 
decreasing subsequence is found, set a flag instead and continue with the 
main loop.

You store all numbers from the input file in an array, which is not necessary:
each loop iteration only needs the previous number to decide if the subsequence
is (still) decreasing. It suffices to store the previously processed number
in a variable.

Summarizing the suggestions so far, the code could look like this:

#include &lt;fstream&gt;
#include &lt;cmath&gt;

long numberOfDivisors(long n) {
    long count = 0;
    for (long j = 2; j &lt;= sqrt(n); j++) {
        if (n % j == 0) {
            count++;
            if (j != n/j)
                count++;
        }
    }
    return count;
}

int main()
{
    std::ifstream inFile("furnici.in");
    std::ofstream outFile("furnici.out");

    long decreasingSequences = 0;
    bool isDescending = false;
    long lastDivisorCount = 0;
    long numDays;
    inFile &gt;&gt; numDays;
    for (long i = 1; i &lt;= numDays; i++) {
        long numAnts;
        inFile &gt;&gt; numAnts;
        long divisorCount = numberOfDivisors(numAnts);
        if (divisorCount &gt;= lastDivisorCount) {
            // No longer decreasing.
            isDescending = false;
        } else if (!isDescending) {
            // A decreasing subsequence started right here.
            isDescending = true;
            decreasingSequences += 1;
        }
        lastDivisorCount = divisorCount;
    }
    outFile &lt;&lt; decreasingSequences;
}


Now you can start to improve the performance, and the prime candidate is
of course the numberOfDivisors() function. 

An efficient method (and I'm repeating arguments from Getting all divisors from an integer now) is to use the prime factorization:  If
$$
 n = p_1^{e_1} \, p_2^{e_2} \cdots p_k^{e_k}
$$
is the factorization of \$ n \$ into prime numbers \$ p_i \$
with exponents \$ e_i \$, then 
$$
 \sigma_0(n) = (e_1+1)(e_2+1) \cdots (e_k+1)
$$
is the number of divisors of \$ n \$, see for example
Wikipedia: Divisor function. Example:
$$
 720 = 2^4 \cdot 3^2 \cdot 5^1 \Longrightarrow
  \sigma_0(720) = (4+1)(2+1)(1+1) = 30 \, .
$$

Here is a possible implementation in C:

long numberOfDivisors(long n){

    long numDivisors = 1;
    long factor = 2; // Candidate for prime factor of `n`

    // If `n` is not a prime number then it must have one factor
    // which is &lt;= `sqrt(n)`, so we try these first:
    while (factor * factor &lt;= n) {
        if (n % factor == 0) {
            // `factor` is a prime factor of `n`, determine the exponent:
            long exponent = 0;
            do {
                n /= factor;
                exponent++;
            } while (n % factor == 0);
                // `factor^exponent` is one term in the prime factorization of n,
                // this contributes as factor `exponent + 1`:
                numDivisors *= exponent + 1;
        }
        // Next possible prime factor:
        factor = factor == 2 ? 3 : factor + 2;
    }

    // Now `n` is either 1 or a prime number. In the latter case,
    // it contributes a factor 2:
    if (n &gt; 1) {
        numDivisors *= 2;
    }

    return numDivisors;
}


As a further improvement you can pre-compute the prime numbers with a 
sieving method. Note that it sufficient to pre-compute the primes in the
range \$ 2 \ldots \sqrt{N} \$ where \$ N = 10^9 \$ is the upper bound for the given input.  That should help to stay within the given memory
limit of 64 MB.