Answer the question
In order to leave comments, you need to log in
How to write it from c++ to c#?
How to rewrite this piece of code from C++ to C# and what is the amount
vector<int>::iterator ItPos = max_element(len.begin(),len.end());
int lastPos = ItPos - len.begin();
int amount = len[lastPos];
// формирование ответа
res.resize(amount);
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int n;
vector<int> mas,len,res;
void input()
{
cin>>n;
mas.resize(n);
len.resize(n);
for (int i=0;i<n;i++)
scanf("%d",&mas[i]);
}
void output()
{
cout<<res.size()<<endl;
for (int i=0;i<res.size();i++)
printf("%d ",res[i]);
}
// сложность O(N*N/2)
void MaxIncSubSeq(vector<int> &mas, vector<int> &res)
{
// формирование массива len
for (int i=0;i<n;i++)
{
int maxLen = 0;
for (int j=i-1;j>=0;j--)
if (mas[j]<mas[i] && len[j]>maxLen)
maxLen = len[j];
len[i] = maxLen + 1;
}
vector<int>::iterator ItPos = max_element(len.begin(),len.end());
int lastPos = ItPos - len.begin();
int amount = len[lastPos];
// формирование ответа
res.resize(amount);
int pos = amount-1;
for (int i=lastPos;i>=0;i--)
if (len[i]==amount)
{
res[pos--] = mas[i];
amount--;
}
}
int main()
{
input();
MaxIncSubSeq(mas,res);
output();
return 0;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question