HDU - 5256 序列变换 【LIS变形】

来源:互联网 发布:普通disco视频软件 编辑:程序博客网 时间:2024/05/20 16:33


传送们:5256


他让求至少可以改变几个数让他们单调递增

我们可以处理一下-.-让每一个数都减去i(这样在后面求出的最长递增子序列的每几个数之间都有相应的空位使他变过来)

然后求最长递增子序列就可以啦-.-

如:

4
2 3 3 4

变为

2 2 1 1

最长递增子序列为2,2 或1 ,1---

我们就可以变为2 3 4 5或1 2 3 4(变2个(4-2))

再如:

4

2 3 3 5

变为:

2 2 1 2

最长递增子序列为2 ,2,2

我们就把那个1也变为2--(变1个(4-3))

然后就是

2 2 2 2

还原为

2 3 4 5


代码:

#include<cstdio>#include<cstring>#include<algorithm> using namespace std;int a[100100],shu[100100],kp;void s(int xx){int n;scanf("%d",&n);for (int i=0;i<n;i++){scanf("%d",&shu[i]);shu[i]-=i;}kp=0;a[kp++]=shu[0];for (int i=1;i<n;i++)if (shu[i]>=a[kp-1])a[kp++]=shu[i];elsea[upper_bound(a,a+kp,shu[i])-a]=shu[i];printf("Case #%d:\n%d\n",xx,n-kp);}int main(){int t;scanf("%d",&t);for (int xx=1;xx<=t;xx++)s(xx);return 0;}


0 0
原创粉丝点击