您的位置:首页 > 其它

hdu5773 The All-purpose Zero(LIS变形)

2016-07-28 17:52 417 查看
题目点我点我点我

题目大意:可以将0替换成任意interger(包括负数),在此基础上求最长递增子序列。

解题思路:无疑LIS,将所有的0全部提取出来,求出此时序列的LIS(不含0的),这是针对0在子序列的外面的情况,如0,1,2,3,0.那么如果0在子序列中间怎么办?

很简单,把读入的非0的数的值减去这个数前面0的个数即可,

如1,2,0,3,4。在提取出0后序列其实为1,2,2,3,LIS的长度为3,加上0的个数则为答案。

/* ***********************************************
┆ ┏┓   ┏┓ ┆
┆┏┛┻━━━┛┻┓ ┆
┆┃       ┃ ┆
┆┃   ━   ┃ ┆
┆┃ ┳┛ ┗┳ ┃ ┆
┆┃       ┃ ┆
┆┃   ┻   ┃ ┆
┆┗━┓ 马 ┏━┛ ┆
┆  ┃ 勒 ┃  ┆      
┆  ┃ 戈 ┗━━━┓ ┆
┆  ┃ 壁     ┣┓┆
┆  ┃ 的草泥马  ┏┛┆
┆  ┗┓┓┏━┳┓┏┛ ┆
┆   ┃┫┫ ┃┫┫ ┆
┆   ┗┻┛ ┗┻┛ ┆
************************************************ */

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
using namespace std;

int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}
//#pragma comment(linker, "/STACK:102400000,102400000")

int n;

int MaxV[100100]; /* 存储长度i+1(len)的子序列最大元素的最小值 */
int a[100100];

int LIS(int * arr,int k)
{
MaxV[0] = arr[0]; /* 初始化 */
int len = 1;
for(int i = 1; i < k; ++i) /* 寻找arr[i]属于哪个长度LIS的最大元素 */
{
if(arr[i] > MaxV[len-1]) /* 大于最大的自然无需查找,否则二分查其位置 */
{
MaxV[len++] = arr[i];
}
else
{
int pos = lower_bound(MaxV,MaxV+len,arr[i]) - MaxV;
MaxV[pos] = arr[i];
}
}
return len;
}

int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);
int t,cas = 1,x;
t = read();
while(t--)
{
n = read();
int cnt = 0,op = 0,flag = 1;
for(int i=0;i<n;i++)
{
x = read();
if(!x) cnt++;
else x -= cnt,a[op++] = x,flag = 0;
}
int ans = LIS(a,op);
if(flag)ans = 0;
printf("Case #%d: %d\n",cas++,ans+cnt);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: