# 在数组中删除尽可能少的数，使得数组满足“先由小到大，再由大到小”

void MyDelete(int* a, int n)
{
int* asc = new int[n];    // asc[i]表示以a[i]结尾的最长升序数的个数
int* desc = new int[n];   // desc[i]表示从a[i]开始的最长降序数的个数
int* inc = new int[n];    // 亮点，用辅助数组的巧妙之处
const int max_int = numeric_limits<int>::max();

//求解asc[];
for(int i=0; i<n; ++i)
inc[i] = max_int;
for(int i=0; i<n; ++i)
{
int low = 0, high = i;
while(low <= high)
{
int mid = (low+high)/2;
if(inc[mid] <= a[i])
low = mid + 1;
else
high = mid - 1;
}
asc[i] = high+2;
inc[high+1] = a[i];
}
//求解desc[];
for(int i=0; i<n; ++i)
inc[i] = max_int;
for(int i=n-1; i>=0; --i)
{
int low = 0, high = n-1-i;
while(low <= high)
{
int mid = (low+high)/2;
if(inc[mid] <= a[i])
low = mid + 1;
else
high = mid - 1;
}
desc[i] = high + 2;
inc[high+1] = a[i];
}
// const int max_len = max(asc[i]+desc[i]-1);     0<= i < n
}

    int len = asc[0];
int index = 0;
for(int j=1; j<n; ++j)
if(len < asc[j])
{
len = asc[j];
index = j;
}
vector<int> vect;
for(int j=index; j>=0; --j)
{
if(len == asc[j])
{
vect.push_back(a[j]);
len--;
}
}
copy(vect.rbegin(),vect.rend(), ostream_iterator<int>(cout,"  "));
cout << endl;

• 本文已收录于以下专栏：

举报原因： 您举报文章：在数组中删除尽可能少的数，使得数组满足“先由小到大，再由大到小” 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)