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;
}
题目大意:可以将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;
}
相关文章推荐
- N皇后问题
- Mysql小结
- iOS 超出父视图不响应点击事件的解决方法
- [编写高质量iOS代码的52个有效方法](十)Grand Central Dispatch(GCD)
- java-基础-util
- Android AndFix 热修复框架
- Linux 配置脚本 启动服务
- 自动发送日报,骗过你的领导^-^
- Oracle 11g必须开启的服务及服务详细介绍
- jenkins免密码连接
- 0718 -- 0728 webview/博客园资料/多线程/网络请求不同方式/导航栏外观设置
- 写给而立之年的自己
- VM下的Centos7安装ftp服务
- 带滚动条的文本框
- POJ 2251 Dungeon Master ( BFS )
- Android调用WEBSERVER发送信息到服务器
- 商业流程中的traversedpath
- C#对GZIP压缩和解压
- linux删除或隐藏命令历史记录history
- Java中return和finally的执行顺序