Pagini recente » Cod sursa (job #674197) | Cod sursa (job #62763) | Cod sursa (job #2111629) | Cod sursa (job #2060354) | Cod sursa (job #443705)
Cod sursa(job #443705)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
typedef unsigned char byte;
typedef unsigned short ushort;
typedef unsigned int uint;
typedef unsigned long ulong;
typedef unsigned long long ulonglong;
int main(int argc, char * argv[])
{
const char * inFile = "scmax.in";
const char * outFile = "scmax.out";
ifstream fin(inFile);
ofstream fout(outFile);
/**
* Read the data in.
*/
ulong n;
fin >> n;
std::vector<ulong> sequence(n);
sequence.clear();
for(std::vector<uint>::size_type i = 0; i < n; i++)
{
ulong number;
fin >> number;
sequence.push_back(number);
}
/**
* Compute the LIS.
*/
std::vector<long> pred(n, -1);
std::vector<ulong> length(n, 1);
ulong maxLength = 0;
long maxIndex = -1;
for(ulong i = 1; i < sequence.size(); i++)
{
for(ulong k = 0; k < i; k++)
{
if(sequence[i] > sequence[k])
{
if(length[k] + 1 > length[i])
{
length[i] = length[k] + 1;
pred[i] = k;
if(length[i] > maxLength)
{
maxLength = length[i];
maxIndex = i;
}
}
}
}
}
/**
* Output the results.
*/
long index = maxIndex, pos = maxLength - 1;
std::vector<ulong> solution(maxLength);
while(index != -1)
{
solution[pos--] = sequence[index];
index = pred[index];
}
fout << maxLength << endl;
for(ulong i = 0; i < solution.size(); i++)
{
fout << solution[i] << " ";
}
fout.close();
fin.close();
return 0;
}