题目地址:​​点击打开链接​

题意:有一队排列好的士兵,长官让一些人除队后,每个人都可以看到左边或者右边的无穷远处

POJ 1836 Alignment(LIS和LDS的结合题)_子序列

思路:题没怎么看懂,但觉得大致意思就是求最长上升子序列,这样就能看到他左边和右边的人了,看了大神的博客才知道是求LIS和LDS(最长下降子序列,估计应该叫这个名字吧)的组合情况,但还是写错了一次,看下面错误代码,

AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>

typedef long long ll;
using namespace std;

const int maxn = 1010;

double a[maxn];
int dp1[maxn],dp2[maxn];

int main()
{
int n,i,j;
while(scanf("%d",&n) != EOF)
{
for(i=1; i<=n; i++)
{
scanf("%lf",&a[i]);
}
memset(dp1,0,sizeof(dp1));
memset(dp2,0,sizeof(dp2));
for(i=1; i<=n; i++)
{
int max1 = 0;//这里是int类型
for(j=1; j<i; j++)
{
if(a[i] > a[j] && dp1[j] > max1)
{
max1 = dp1[j];
}
}
dp1[i] = max1 + 1;
}
for(i=n; i>=1; i--)
{
int max2 = 0;
for(j=n; j>=i+1; j--)
{
if(a[i] > a[j] && dp2[j] > max2)
{
max2 = dp2[j];
}
}
dp2[i] = max2 + 1;
}
int max3 = 0;
for(i=1; i<=n; i++)
{
for(j=i+1; j<=n; j++)
{
if(dp1[i] + dp2[j] > max3)
{
max3 = dp1[i] + dp2[j];
}
}
}
printf("%d\n",n-max3);
}
return 0;
}


正着求一次最长上升子序列,倒着求一次最长上升子序列,怕超时可以用O(nlogn)算法

错误代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>

typedef long long ll;
using namespace std;

const int maxn = 1010;

double a[maxn];
int dp1[maxn],dp2[maxn];

int main()
{
int n,i,j;
while(scanf("%d",&n) != EOF)
{
for(i=1; i<=n; i++)
{
scanf("%lf",&a[i]);
}
memset(dp1,0,sizeof(dp1));
memset(dp2,0,sizeof(dp2));
for(i=1; i<=n; i++)
{
int max1 = 0;//这里是int类型
for(j=1; j<i; j++)
{
if(a[i] > a[j] && dp1[j] > max1)
{
max1 = dp1[j];
}
}
dp1[i] = max1 + 1;
}
for(i=1; i<=n; i++)
{
int max2 = 0;
for(j=1; j<i; j++)
{
if(a[i] < a[j] && dp2[j] > max2)
{
max2 = dp2[j];
}
}
dp2[i] = max2 + 1;
}
int max3 = 0;
for(i=1; i<=n; i++)
{
for(j=i+1; j<=n; j++)
{
if(dp1[i] + dp2[j] > max3)
{
max3 = dp1[i] + dp2[j];
}
}
}
printf("%d\n",n-max3);
}
return 0;
}


正着求了一次最长上升子序列,又倒着求了一次最长下降子序列,结果测试数据就过不了,太逗了,这种低级错误都犯

大神地址:http://cavenkaka.iteye.com/blog/1542421