您的位置:首页 > 其它

dp(第一次尝试)

2016-05-21 19:46 225 查看
题目:点击打开链接

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include <cmath>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<algorithm>
using namespace std;

typedef long long LL;
const int INF = (1<<30)-1;
const int mod=1000000007;
const int maxn=1000005;

int a[maxn],f[maxn];
int main()
{
int T;
scanf("%d",&T);
int kase=0;
while(T--)
{
int n;
scanf("%d",&n);
for(int i=1; i<=n; i++)
{
scanf("%d",&a[i]);
a[i]=a[i]-i;
}

int len=1;
f[1]=a[1];
for(int i=2; i<=n; i++)
{
if(a[i]>=f[len]) f[++len]=a[i];//这里也和求最长上升子序列不同,是大于等于
else
{
int pos=upper_bound(f+1,f+len+1,a[i])-f;//upper_bound返回的是序列中严格大于key值的第一个数
f[pos]=a[i];
}
}
printf("Case #%d:\n",++kase);
printf("%d\n",n-len);
}
return 0;
}
其实这个dp我并不是特别擅长,但是这个代码我看的有点懂,但不是特别的懂,只是能够推出来这是对的,反正就是感觉比较厉害,这里面还有一个函数,就是lower_bound返回的是第一个大于等于该值的位置,而upper_bound是返回第一个大于该值的

的值的位置,其实也不是 特别懂,反正是看了别人的代码,总体上思路也不是特别的明确。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: