Pagini recente » Cod sursa (job #2643560) | Cod sursa (job #2350112) | Atasamentele paginii Clasament oni_2015_11_12_bv_1 | Istoria paginii runda/4fbgtznd/clasament | Cod sursa (job #1209732)
#include <stdlib.h>
#include <stdio.h>
using namespace std;
//I'll retain into an array only the ascending subarrays from the given input
int main ()
{
int k;
int i;
int nrValues;
int nrElements;
int nr1;
int minus;
int auxStartIndex;
auxStartIndex = 0;
int startIndex;//the index from wich the longest subarray starts
startIndex = 0;
int maxLength;
int auxMaxLength;
int maxAscendingSubarray[100003];// can be bigger than 1000
FILE *infile;
FILE *outfile;
auxMaxLength = -1;
maxLength = 1;
infile = fopen ("scmax.in","r");
if (infile != NULL)
{
if(fscanf(infile, "%d", &nrValues));
for( i =0; i< nrValues; i++)
{
if(fscanf(infile, "%d", &nr1));
maxAscendingSubarray[i] = nr1;
}
fclose(infile);
}
i = 0;
while(i < nrValues - 1)
{
if(maxAscendingSubarray[i]<= maxAscendingSubarray[i + 1])
{
startIndex = i;
while(maxAscendingSubarray[i] <= maxAscendingSubarray[i + 1])
{
maxLength++;
i++;
}
if( maxLength > auxMaxLength )
{
auxMaxLength = maxLength;
auxStartIndex = startIndex;
}
maxLength = 1;
}
else
{
i++;
}
}
maxLength = auxMaxLength;
minus = 0;
k = startIndex;
startIndex = auxStartIndex;
nrElements = startIndex + maxLength;
while( k < nrElements-1 )
{
if( maxAscendingSubarray[k] == maxAscendingSubarray[k+1] )
{
while(maxAscendingSubarray[k] == maxAscendingSubarray[k+1])
{
minus++;
k++;
}
}
else
{
k++;
}
}
outfile = fopen ("scmax.out","w");
if (outfile != NULL)
{
fprintf(outfile, "%d\n",maxLength- minus);
k = startIndex;
while( k < nrElements )
{
if( maxAscendingSubarray[k] != maxAscendingSubarray[k+1] )
{
fprintf(outfile, "%d ", maxAscendingSubarray[k]);
}
k++;
}
fclose(outfile);
}
return 0;
}