Pagini recente » Istoria paginii runda/oji_2004/clasament | Cod sursa (job #738505) | Cod sursa (job #2383306) | Cod sursa (job #1241013) | Cod sursa (job #1209350)
#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 auxStartIndex;
int startIndex;//the index from wich the longest subarray starts
int maxLength;
int auxMaxLength;
int *maxAscendingSubarray = (int *)malloc(100 * sizeof(int));// 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;
k = 0;
startIndex = auxStartIndex;
nrElements = startIndex + maxLength;
while( k < nrElements-1 )
{
if( maxAscendingSubarray[k] == maxAscendingSubarray[k+1] )
{
maxLength--;
auxStartIndex--;
for( i = k; i < nrElements-1; i++)
{
maxAscendingSubarray[i] = maxAscendingSubarray[ i + 1 ];
}
nrElements--;
}
k++;
}
outfile = fopen ("scmax.out","w");
if (outfile != NULL)
{
fprintf(outfile, "%d\n", maxLength);
for( i = startIndex; i< nrElements; i++)
{
fprintf(outfile, "%d ", maxAscendingSubarray[i]);
}
fclose(outfile);
}
free(maxAscendingSubarray);
return 0;
}